digitalmars.D.learn - =?ISO-8859-1?Q?DLL_=80rror:_D_calling_D_extern(C)_code?=
- Heinz (7/7) Feb 10 2009 Hello everyone.
- Heinz (2/14) Feb 10 2009 I attached a rar file with the sources just to see what i'm talking abou...
- torhu (5/7) Feb 11 2009 Try this:
- Heinz (4/13) Feb 13 2009 Haha! cool man, thanx, it works.
- Mike Parker (6/21) Feb 13 2009 It doesn't affect normal variables. Calling a function through a
- Frits van Bommel (8/36) Feb 13 2009 Actually, extern(C) does affect normal variables. In particular, it
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
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
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
torhu Wrote:On 10.02.2009 19:51, Heinz wrote: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.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 13 2009
Heinz wrote:torhu Wrote: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.On 10.02.2009 19:51, Heinz wrote: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.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 13 2009
Mike Parker wrote:Heinz wrote: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.torhu Wrote: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.On 10.02.2009 19:51, Heinz wrote: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.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 13 2009