Thread overview
Access violation when exiting program
Jun 04, 2013
Frank Fuente
Jun 18, 2013
Andrej Mitrovic
Jun 18, 2013
Andrej Mitrovic
June 04, 2013
Hi,

The following program runs, loads the DLL, excutes the function, returns the correct value and unloads the DLL, but on exiting causes an exception:

Unhandled exception at 0x0040f9c0 in USBRelay.exe: 0xC0000005: Access violation writing location 0xffffffea.




The FT_STATUS is declared...
    alias uint FT_STATUS;

The function is declared...
    alias extern (C) FT_STATUS function(uint* lpdwVersion) FT_GetLibraryVersion;

The main program is...

module main;

import	std.stdio,
	ftd2xx,
	core.runtime,
	std.exception,
	std.c.windows.windows;


int main(string[] argv)
{
	string	dllFilename = "FTD2XX.DLL";
	uint	libVer;

	auto hDLL = enforce(cast(HMODULE)Runtime.loadLibrary(dllFilename));

	scope (exit) Runtime.unloadLibrary(hDLL);

	FT_GetLibraryVersion GetLibraryVersion = enforce(cast(FT_GetLibraryVersion)GetProcAddress(hDLL, "FT_GetLibraryVersion"));

	FT_STATUS rv = GetLibraryVersion(&libVer);

	writefln("Version : %0x", libVer);

	return 0;
}

I'm sure its something stupid but nothing leaps out from the screen.
Am I using the loadLibrary and enforce correctly?

Regards, Frank.
June 18, 2013
On Tuesday, 4 June 2013 at 14:06:29 UTC, Frank Fuente wrote:
> The function is declared...
>     alias extern (C) FT_STATUS function(uint* lpdwVersion) FT_GetLibraryVersion;

The calling convention is wrongly declared, it should be:

>     alias extern (Windows) FT_STATUS function(uint* lpdwVersion) FT_GetLibraryVersion;

From the header file[1] I can see it uses the WINAPI macro, which makes the function have the stdcall calling convention (extern(Windows) in D).

ftp://ftp.mn-net.com/Rapid_Tests/Software/USB%20DRIVER/FTDI_USB_32BIT/FTD2XX.H
June 18, 2013
On Tuesday, 18 June 2013 at 00:16:10 UTC, Andrej Mitrovic wrote:
> On Tuesday, 4 June 2013 at 14:06:29 UTC, Frank Fuente wrote:
>> The function is declared...
>>    alias extern (C) FT_STATUS function(uint* lpdwVersion) FT_GetLibraryVersion;
>
> The calling convention is wrongly declared, it should be:
>
>>    alias extern (Windows) FT_STATUS function(uint* lpdwVersion) FT_GetLibraryVersion;
>
> From the header file[1] I can see it uses the WINAPI macro, which makes the function have the stdcall calling convention (extern(Windows) in D).
>
> ftp://ftp.mn-net.com/Rapid_Tests/Software/USB%20DRIVER/FTDI_USB_32BIT/FTD2XX.H

Btw, apologies for the late reply.