digitalmars.D.learn - D2 Win API Problem
- A Bothe (28/28) Sep 20 2009 Hello guys,
- Jeremie Pelletier (8/45) Sep 20 2009 You should try to test if hm or tfunc are null before using them. The
- torhu (4/10) Sep 20 2009 Try adding "extern (Windows)" before the function pointer declaration.
- A Bothe (1/1) Sep 21 2009 @torhu: Thank you for your answer, but I also tried that and I also came...
- A Bothe (7/7) Sep 21 2009 I solved the problem!
- torhu (3/10) Sep 21 2009 MessageBoxA is definitely stdcall, so extern (Windows) is correct. So
Hello guys, I got a problem with the following code. I can compile it successfully but when I want to start it, there is just this "object.AccessVialotion"! Even GetLastError() returns 0.... so the problem cannot be found in wrong-written names... Thanks in advance! import std.loader, std.c.windows.windows; int function(HWND hwnd,char* text,char* title, uint style) tfunc=null; class Module { private HXModule hm; public HXModule Handle() {return hm;} this(string name) { hm=ExeModule_Load(name); } Fun GetSymbol(Fun)(ref Fun func,string name) { return func=cast(Fun)ExeModule_GetSymbol(hm,name); } } void main(){ Module m=new Module("user32.dll"); m.GetSymbol(tfunc,"MessageBoxA"); tfunc(null,cast(char*)"Test",cast(char*)"TestTitle",MB_OK); } ------- Check out my D-IDE on http://www.alexanderbothe.com/?id=27
Sep 20 2009
A Bothe wrote:Hello guys, I got a problem with the following code. I can compile it successfully but when I want to start it, there is just this "object.AccessVialotion"! Even GetLastError() returns 0.... so the problem cannot be found in wrong-written names... Thanks in advance! import std.loader, std.c.windows.windows; int function(HWND hwnd,char* text,char* title, uint style) tfunc=null; class Module { private HXModule hm; public HXModule Handle() {return hm;} this(string name) { hm=ExeModule_Load(name); } Fun GetSymbol(Fun)(ref Fun func,string name) { return func=cast(Fun)ExeModule_GetSymbol(hm,name); } } void main(){ Module m=new Module("user32.dll"); m.GetSymbol(tfunc,"MessageBoxA"); tfunc(null,cast(char*)"Test",cast(char*)"TestTitle",MB_OK); } ------- Check out my D-IDE on http://www.alexanderbothe.com/?id=27You should try to test if hm or tfunc are null before using them. The value from GetLastError can be overridden if a valid win32 call is made after the one triggering the error. By the way, you don't need to cast string literals to char*, they can be implicitly converted. Its also safer to use the .ptr property instead of casting an array to its pointer type, since if you change the array type the .ptr property will reflect that new type.
Sep 20 2009
On 20.09.2009 20:11, A Bothe wrote:Hello guys, I got a problem with the following code. I can compile it successfully but when I want to start it, there is just this "object.AccessVialotion"! Even GetLastError() returns 0.... so the problem cannot be found in wrong-written names... Thanks in advance! import std.loader, std.c.windows.windows; int function(HWND hwnd,char* text,char* title, uint style) tfunc=null;Try adding "extern (Windows)" before the function pointer declaration. That'll select the stdcall calling convention, which most of the Windows API uses.
Sep 20 2009
torhu: Thank you for your answer, but I also tried that and I also came to the same result:)
Sep 21 2009
I solved the problem! I've to make the function pointer to be extern(C), so I will have extern(C) { int function(...) tfunc; }
Sep 21 2009
On 21.09.2009 17:11, A Bothe wrote:I solved the problem! I've to make the function pointer to be extern(C), so I will have extern(C) { int function(...) tfunc; }MessageBoxA is definitely stdcall, so extern (Windows) is correct. So the problem has to be something else.
Sep 21 2009