www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D2 Win API Problem

reply A Bothe <info alexanderbothe.com> writes:
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
next sibling parent Jeremie Pelletier <jeremiep gmail.com> writes:
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=27
You 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
prev sibling next sibling parent reply torhu <no spam.invalid> writes:
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
parent A Bothe <info alexanderbothe.com> writes:
 torhu: Thank you for your answer, but I also tried that and I also came to the
same result:)
Sep 21 2009
prev sibling parent reply A Bothe <info alexanderbothe.com> writes:
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
parent torhu <no spam.invalid> writes:
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