Good afternoon,

I'm having difficulties loading a dll in Microsoft Visual Studio 2003,
that was supplied to me by another company. They have supplied me with
test code, which causes my program to crash, see below:

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <windows.h>



typedef bool (__fastcall* _ANT_Init)(unsigned char ucUSB_, unsigned
short usBaud_);

int _tmain(int argc, _TCHAR* argv[])
{

HINSTANCE hDLLInstance;

_ANT_Init ANT_Init;

printf("Attempting to load DLL\n");
hDLLInstance = LoadLibraryA("ANT_DLL.dll");

if (hDLLInstance == NULL)
{

fprintf(stderr, "ERROR: Unable to load DLL\n");
return 1;

}

else
{

printf("Success: Loaded ANT_DLL.dll\n");

}


ANT_Init = (_ANT_Init)GetProcAddress(hDLLInstance, "_ANT_Init");
if (ANT_Init(0, 50000) == true)
{
printf("Success: ANT Initialized Properly\n");
}
else
{
printf("Fail: ANT Interface not found\n");
}



FreeLibrary(hDLLInstance);

printf("ANT DLL Freed\n");

return 0;

}


Whenever my program crashes, I get the following error message
"Unhandled exception at 0x00000000 in program.exe: 0xC0000005: Access
violation reading location 0x00000000."

The company that supplied me, with the dll, say that they compiled it,
with Borland C++. They also told me that to get my program to link the
windows.h file in properly, that I need to have Windows PSDK installed
. I've downloaded the latter of the Microsoft web site, but I still get
the same error. From the following screen shot, you can see, that SDK
seems to be installed correctly on my computer.

Do you have any suggestions? Thanks in advance for your help,

Regards,

Alan

Re: problems calling a function contained in a dll by Scott

Scott
Thu Oct 26 13:44:22 CDT 2006

Alan wrote:
> ANT_Init = (_ANT_Init)GetProcAddress(hDLLInstance, "_ANT_Init");
> if (ANT_Init(0, 50000) == true)
> {
> printf("Success: ANT Initialized Properly\n");
> }
> else
> {
> printf("Fail: ANT Interface not found\n");
> }
> Whenever my program crashes, I get the following error message
> "Unhandled exception at 0x00000000 in program.exe: 0xC0000005: Access
> violation reading location 0x00000000."

Have you checked the return from GetProcAddress?

if (ANT_Init == NULL)
{ printf("Function not found.\n");
return 1;
}

If this fails it might be a case of name decoration. I.e. compiling a C
function declaration with C++ assumed by the compiler. It can be fixed
like this:

extern "C"
{
typedef bool (__fastcall* _ANT_Init)(unsigned char ucUSB_, unsigned
short usBaud_);
}

--
Scott McPhillips [VC++ MVP]


Re: problems calling a function contained in a dll by Igor

Igor
Thu Oct 26 13:56:32 CDT 2006

Alan <alan_ball007@yahoo.co.uk> wrote:
> ANT_Init = (_ANT_Init)GetProcAddress(hDLLInstance, "_ANT_Init");
> if (ANT_Init(0, 50000) == true)
> {
> printf("Success: ANT Initialized Properly\n");
> }
> else
> {
> printf("Fail: ANT Interface not found\n");
> }
>
> Whenever my program crashes, I get the following error message
> "Unhandled exception at 0x00000000 in program.exe: 0xC0000005: Access
> violation reading location 0x00000000."

Most likely, GetProcAddress failed and returned NULL. You are not
checking for this but just trying to call the function.

Use Dependency Walker (www.dependencywalker.com) to check whether the
DLL does indeed export a function named "_ANT_Init".
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925