Problems when using DLL-Functions

Marc Müller dont at spam.me
Mon Jul 9 03:52:42 PDT 2007


Hello,

I wrote a small program, which is using a function from a Windows 
system-dll.
So I used the implib-tool to get a corresponding library-file:

implib.exe /s wintab32.lib C:\WINDOWS\system32\Wintab32.dll

I can call functions from the dll correctly and also get the expectet 
return values. But under some circumstances the application crashes - 
and I just don't know why...


=========================================================
import std.stdio;
import std.c.windows.windows;

extern(C)
UINT WTInfoA(UINT, UINT, LPVOID);

version = A;

void main(char[][] args)
{
	version (A)
	{
		writefln("Getting WTInfo...");
		WTInfoA(0, 0, null);
		writefln("Received WTInfo");
	}

	version (B)
	{
		writefln("Getting WTInfo...");
		int value = WTInfoA(0, 0, null);
		writefln("Received WTInfo");
	}

	version (C)
	{
		try
		{
			writefln("Getting WTInfo...");
			WTInfoA(0, 0, null);
			writefln("Received WTInfo");
		}
		catch (Exception e)
		{
			writefln("An error has occured:");
			writefln(e.msg);
		}
	}

	version (D)
	{
		if (WTInfoA(0, 0, null)==0)
			writefln("No graphic tablet available");
	}
}
=========================================================


Now the funny part:

As you can see all versions A-D are doing more or less the same.

Version A crashes with the following output:
# Getting WTInfo...
# Received WTInfo
# Error: Access Violation

Version B works fine.
# Getting WTInfo...
# Received WTInfo

Version C (which is identical to version A - just surrounded by a 
try-catch-block) does neither crash nor it throws an exception.
# Getting WTInfo...
# Received WTInfo

Version D, which also evaluates the return-Value from the function call 
like version B crashes.
# Error: Access Violation


I think anything is scrambling my stack - but it is absolutely 
unpredictable when this occurs - and I have no clue what I can do :-(

The errors are reproducable - Version A always crashes while version B 
never crashes.

Do you have an Idea?

Kind regards,
	-Marc-


More information about the Digitalmars-d-learn mailing list