www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: Example Attached

reply Heinz <malagana15 yahoo.es> writes:
torhu Wrote:

 On 10.02.2009 19:51, Heinz wrote:
 Heinz Wrote:

 I attached a rar file with the sources just to see what i'm talking about. The
example is from the DMD site. Included is the extern D (ok) and the extern C
(fails). To compile open "compile.bat" and to run the programs use "run.bat".

Try this: alias extern (C) void function(void*) MyDLL_Initialize_fp; alias extern (C) void function() MyDLL_Terminate_fp; alias extern (C) MyClass function() getMyClass_fp;

Haha! cool man, thanx, it works. I never though it could be the type but adding extern(C), the program treats variables as C function pointers. I had no idea at all that i can create extern(C) variables to be trated as C type. I learned something new today! Thanks again.
Feb 13 2009
parent reply Mike Parker <aldacron gmail.com> writes:
Heinz wrote:
 torhu Wrote:
 
 On 10.02.2009 19:51, Heinz wrote:
 Heinz Wrote:

 I attached a rar file with the sources just to see what i'm talking about. The
example is from the DMD site. Included is the extern D (ok) and the extern C
(fails). To compile open "compile.bat" and to run the programs use "run.bat".

alias extern (C) void function(void*) MyDLL_Initialize_fp; alias extern (C) void function() MyDLL_Terminate_fp; alias extern (C) MyClass function() getMyClass_fp;

Haha! cool man, thanx, it works. I never though it could be the type but adding extern(C), the program treats variables as C function pointers. I had no idea at all that i can create extern(C) variables to be trated as C type. I learned something new today! Thanks again.

It doesn't affect normal variables. Calling a function through a function pointer, the calling convention still must be known in order for parameters to be properly passed. So any time you define a function pointer to point to a C function or to pass to a C API as a callback, it must have the proper extern linkage.
Feb 13 2009
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Mike Parker wrote:
 Heinz wrote:
 torhu Wrote:

 On 10.02.2009 19:51, Heinz wrote:
 Heinz Wrote:

 I attached a rar file with the sources just to see what i'm talking 
 about. The example is from the DMD site. Included is the extern D 
 (ok) and the extern C (fails). To compile open "compile.bat" and to 
 run the programs use "run.bat".

alias extern (C) void function(void*) MyDLL_Initialize_fp; alias extern (C) void function() MyDLL_Terminate_fp; alias extern (C) MyClass function() getMyClass_fp;

Haha! cool man, thanx, it works. I never though it could be the type but adding extern(C), the program treats variables as C function pointers. I had no idea at all that i can create extern(C) variables to be trated as C type. I learned something new today! Thanks again.

It doesn't affect normal variables. Calling a function through a function pointer, the calling convention still must be known in order for parameters to be properly passed. So any time you define a function pointer to point to a C function or to pass to a C API as a callback, it must have the proper extern linkage.

Actually, extern(C) does affect normal variables. In particular, it affects their name mangling. (And if the type of the variable includes a 'raw' function pointer type (not an alias/typedef to one), I'm pretty sure it also modifies that type to use a C calling convention function pointer type) The example above, though, does not directly affect any variables because they're only alias declarations.
Feb 13 2009