Runtime error when accessing static data in a DLL

Phil Deets pjdeets2 at gmail.com
Thu Dec 24 10:11:00 PST 2009


On Thu, 24 Dec 2009 10:49:01 -0500, Phil Deets <pjdeets2 at gmail.com> wrote:

> I'll work on reproducing it in a smaller scale

Reduced test case:

test.c (compiled with Visual C++ 2008 Express Edition)

// Adapted from sample code at  
http://msdn.microsoft.com/en-us/library/ms680582(VS.85).aspx
#include <windows.h>
#include <strsafe.h>

void main()
{
	if(!LoadLibraryA("d.dll"))
	{
		LPVOID lpMsgBuf;
		DWORD dw = GetLastError();
		FormatMessageA(
			FORMAT_MESSAGE_ALLOCATE_BUFFER |
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			dw,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPSTR) &lpMsgBuf,
			0, NULL );
		printf("LoadLibrary failed with error %d: %s\n", dw, lpMsgBuf);
		LocalFree(lpMsgBuf);
	}
}

dll.d:

import std.c.windows.windows;
int x;
extern (Windows) BOOL DllMain(HINSTANCE, ULONG, LPVOID) {
	x=0;
	return true;
}

dll.def:

LIBRARY         "d.dll"
EXETYPE         NT
SUBSYSTEM       WINDOWS
CODE            SHARED EXECUTE
DATA            WRITE

Compiled with:

dmd dll.d dll.def -ofd.dll

When I run the C app, I get the output "LoadLibrary failed with error 998:  
Invalid access to memory location."


More information about the Digitalmars-d-learn mailing list