access violation With dll?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 16 10:18:32 PDT 2015


On Thursday, 16 July 2015 at 17:04:09 UTC, Taylor Hillegeist 
wrote:
>     void function(ref char[], int) Testf
>           = cast(void function(ref char[], int))
>           GetProcAddress(h, "Test"); //Function Says HELLO WORLD

> void __cdecl Test(char MyOutput[], int32_t len);


Those signatures don't actually match, your call is overwriting 
the stack with the string data, which means main cannot return 
properly; all that stuff has been smashed to bits.

The proper D signature of that C function is:

extern(C) void function(char*, int);


You really shouldn't try to use D arrays or `ref` when 
interacting with C functions, C doesn't understand those D 
features. (You can make it work but it needs cooperation in both 
functions.) Instead, stick to the basic types C supports like 
ints and pointers.

If you fix that, your code should work. I'd say go ahead and 
alias the type for easier use

extern(C) alias dll_func_type = void function(char*, int);

auto Testf = cast(dll_func_type) GetProcAddress(...);




More information about the Digitalmars-d-learn mailing list