D2 Win API Problem

Jeremie Pelletier jeremiep at gmail.com
Sun Sep 20 12:14:26 PDT 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.


More information about the Digitalmars-d-learn mailing list