digitalmars.D - D to DLLS
- firefly fssc.demon.co.uk (28/28) May 03 2004 Dear D Users
 - Unknown W. Brackets (4/44) May 03 2004 Did you try using extern (C)?
 - firefly fssc.demon.co.uk (3/6) May 03 2004 Yes I did, it doesn't work because it appears I can't declare a function...
 - Mike Wynn (19/28) May 06 2004 extern(Windows) works !!
 
Dear D Users
I have now managed to get a dll function called from D, however I get an access
violation when I actually make the call, I presume this is due to incorrect
calling conventions. The code I use at the moment is below, I have taken the
usual C approach in defining a typedef for the C function pointer, then
declaring a variable to that type. However, in D one cannot specify the calling
convention in the typedef. I assume therefore that this is not the way to do it.
An earlier poster gave an example of how he did it, but I didn't quite
understand the use of the alias keyword in the example.
Any suggestions, gratefully accepted!
Herbert Sauro
import std.c.stdio;
import std.c.windows.windows;
typedef int (* ptr_SBWCREATEMODULEIMPL) (char* ModuleName, char* DisplayName,
int SBWModuleManagementType, char* helpStr);
ptr_SBWCREATEMODULEIMPL SBWCreateModuleImpl;
int main ( char [] [] args ) {
HMODULE LibHandle;
printf ("Start Program\n");
LibHandle = LoadLibraryA ("sbw.dll\0");
printf ("LibHandle = %X\n", LibHandle);
SBWCreateModuleImpl = (ptr_SBWCREATEMODULEIMPL) GetProcAddress (LibHandle,
"SBWCreateModuleImpl\0");
printf ("SBWCreateModuleImpl Handle = %X\n", SBWCreateModuleImpl);
SBWCreateModuleImpl ((char *) "dsbw\0", (char*)"D language\0", 0, (char*)"\0");
getchar();
return 0;
}
 May 03 2004
firefly fssc.demon.co.uk wrote:
 Dear D Users
 
 I have now managed to get a dll function called from D, however I get an access
 violation when I actually make the call, I presume this is due to incorrect
 calling conventions. The code I use at the moment is below, I have taken the
 usual C approach in defining a typedef for the C function pointer, then
 declaring a variable to that type. However, in D one cannot specify the calling
 convention in the typedef. I assume therefore that this is not the way to do
it.
 An earlier poster gave an example of how he did it, but I didn't quite
 understand the use of the alias keyword in the example.
 
 Any suggestions, gratefully accepted!
 
 Herbert Sauro
 
 import std.c.stdio;
 import std.c.windows.windows;
 
 typedef int (* ptr_SBWCREATEMODULEIMPL) (char* ModuleName, char* DisplayName,
 int SBWModuleManagementType, char* helpStr);
 
 ptr_SBWCREATEMODULEIMPL SBWCreateModuleImpl;
 
 int main ( char [] [] args ) {
 
 HMODULE LibHandle;
 
 printf ("Start Program\n");
 LibHandle = LoadLibraryA ("sbw.dll\0");
 printf ("LibHandle = %X\n", LibHandle);
 SBWCreateModuleImpl = (ptr_SBWCREATEMODULEIMPL) GetProcAddress (LibHandle,
 "SBWCreateModuleImpl\0");
 printf ("SBWCreateModuleImpl Handle = %X\n", SBWCreateModuleImpl);
 
 SBWCreateModuleImpl ((char *) "dsbw\0", (char*)"D language\0", 0, (char*)"\0");
 getchar();
 return 0;
 }
 
 
Did you try using extern (C)?
news://news.digitalmars.com:119/c76bn6$at5$1 digitaldaemon.com
-[Unknown]
 May 03 2004
Yes I did, it doesn't work because it appears I can't declare a function pointer using extern (C) which is what I need to do when I call GetProcAddress. HerbertDid you try using extern (C)? news://news.digitalmars.com:119/c76bn6$at5$1 digitaldaemon.com -[Unknown]
 May 03 2004
On Tue, 4 May 2004 06:54:06 +0000 (UTC), firefly fssc.demon.co.uk wrote:Yes I did, it doesn't work because it appears I can't declare a function pointer using extern (C) which is what I need to do when I call GetProcAddress. Herbertextern(Windows) works !! see the util8 dynloader/winmm lib on http://www.geocities.com/one_mad_alien/dcode/ I quite happily call timeGetTime from winmm.dll with explict loading (which is extern(Windows) ) extern(C) { alias int function (int, int) cfuncPtr; } should be the C equiv of typedef __cdecl int (*cfuncPtr)(int, int); (__cdecl might be in the wrong place I forget such things! but you know what I mean) then cfuncPtr func = cast(cfuncPtr)GetProcAddress( hmodule, "myfunc" ); int i = func(1, 2); should work ..... Mike.Did you try using extern (C)? news://news.digitalmars.com:119/c76bn6$at5$1 digitaldaemon.com -[Unknown]
 May 06 2004








 
 
 
 Mike Wynn <one_mad_alien hotmail.com>