www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - =?ISO-8859-1?Q?DLL_=80rror:_D_calling_D_extern(C)_code?=

reply Heinz <malagana15 yahoo.es> writes:
Hello everyone.
I've been using DLL's in D for a long time but just recently i'm having a
problem. Lets see if someone can help me a bit.

I have several lines of code but i'll use an example from the DMD site wich is
almost the same come.

Read the DMD site about DLL's here: http://www.digitalmars.com/d/1.0/dll.html

Check for the section "D code calling D code in DLLs".

If you extern(C) all exports in mydll.d and change all symbols in test.d to
their C respectives, when running you'll get access violation.

Any ideas?
Feb 10 2009
parent reply Heinz <malagana15 yahoo.es> writes:
Heinz Wrote:

 Hello everyone.
 I've been using DLL's in D for a long time but just recently i'm having a
problem. Lets see if someone can help me a bit.
 
 I have several lines of code but i'll use an example from the DMD site wich is
almost the same come.
 
 Read the DMD site about DLL's here: http://www.digitalmars.com/d/1.0/dll.html
 
 Check for the section "D code calling D code in DLLs".
 
 If you extern(C) all exports in mydll.d and change all symbols in test.d to
their C respectives, when running you'll get access violation.
 
 Any ideas?
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".
Feb 10 2009
next sibling parent torhu <no spam.invalid> writes:
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;
Feb 11 2009
prev sibling parent 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".
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.
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".
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.
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