Thread overview
D2 Win API Problem
Sep 20, 2009
A Bothe
Sep 20, 2009
Jeremie Pelletier
Sep 20, 2009
torhu
Sep 21, 2009
A Bothe
Sep 21, 2009
A Bothe
Sep 21, 2009
torhu
September 20, 2009
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
September 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=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.
September 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.
September 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;
}
September 21, 2009
@torhu: Thank you for your answer, but I also tried that and I also came to the same result:)

September 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.