Trouble with DLL address
dennis luehring
dl.soluz at gmx.net
Mon Jan 14 01:19:27 PST 2013
http://dlang.org/type.html
int => signed 32 bits
but don't you need long in D
Am 14.01.2013 10:13, schrieb dnewbie:
> I have a DLL which exports a function GetFunction. GetFunction
> returns a pointer to RealFunction. Now I want to run RealFunction
> from my D program, but for some reason I get the wrong address.
> Here is the code.
>
> ---- dll64.c -----
> #define WIN32_LEAN_AND_MEAN
> #include <windows.h>
>
> int __stdcall RealFunction(int a)
> {
> return a + 1;
> }
>
> typedef int (__stdcall *FuncPtr)(int);
>
> FuncPtr __stdcall GetFunction()
> {
> return &RealFunction;
> }
>
> ---- mydll64.def ----
> LIBRARY mydll64.dll
> EXPORTS
> GetFunction
> ---------------------
>
> build with MSVC64:
> cl -c dll64.c -Fomydll64.obj
> link /DLL /OUT:mydll64.dll mydll64.obj /DEF:mydll64.def
>
> And this is the D program.
>
> ---- testdll64d.d ----
> // dmd -m64 -c testdll64d.d -oftestdll64d.obj
> // link /OUT:testdll64d.exe testdll64d.obj
>
> import core.runtime;
> import std.c.windows.windows;
> import std.stdio;
>
> alias extern(Windows) int function(int) FuncPtr;
>
> int main(string[] args)
> {
> HMODULE dll = LoadLibraryA("mydll64.DLL");
> FARPROC getFunction = GetProcAddress(dll, "GetFunction");
> FuncPtr realFunction = cast(FuncPtr) getFunction();
>
> writefln("dll address: %08x", dll);
> writefln("GetFunction address: %08x", getFunction);
> writefln("RealFunction address: %08x", realFunction);
> writefln("RealFunction result: %d", realFunction(7));//<--
> CRASH
>
> return 0;
> }
> ----------------------
>
> Output:
> dll address: 180000000
> GetFunction address: 180001020
> RealFunction address: ffffffff80001000
> <CRASH>
>
> BTW, this works:
> FuncPtr realFunction = cast(FuncPtr) (getFunction() &
> 0x00000000ffffffff | cast(size_t) dll);
>
More information about the Digitalmars-d-learn
mailing list