Re: problems calling a function contained in a dll
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Thu, 26 Oct 2006 16:49:34 -0400
Alan wrote:
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:
If the actual compiler or system is relevant, the whole question is
off-topic here. In particular the C standard makes no mention of
'dll'. In addition, most identifier names beginning with '_' are
reserved for the compiler implementor. Use yields undefined
behaviour, which includes crashes.
#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.
If you ever included a binary attachment, such as a screen shot,
luckily your ISP was smart enough to delete it, so we don't have to
yell at you.
Compilation yields the following errors:
[1] c:\c\junk>cc -c -o junk.o junk.c
junk.c:1:20: stdafx.h: No such file or directory (ENOENT)
junk.c:4:21: windows.h: No such file or directory (ENOENT)
junk.c:8: parse error before '*' token
junk.c:9: warning: type defaults to `int' in declaration of `bool'
junk.c:9: `bool' declared as function returning a function
junk.c:11: parse error before "_TCHAR"
junk.c: In function `_tmain':
junk.c:14: `HINSTANCE' undeclared (first use in this function)
junk.c:14: (Each undeclared identifier is reported only once
junk.c:14: for each function it appears in.)
junk.c:14: parse error before "hDLLInstance"
junk.c:16: `_ANT_Init' undeclared (first use in this function)
junk.c:19: `hDLLInstance' undeclared (first use in this function)
junk.c:19: warning: implicit declaration of function `LoadLibraryA'
junk.c:37: `ANT_Init' undeclared (first use in this function)
junk.c:37: parse error before "GetProcAddress"
junk.c:38: `ANT_Init' used prior to declaration
junk.c:38: warning: implicit declaration of function `ANT_Init'
junk.c:38: `true' undeclared (first use in this function)
junk.c:49: warning: implicit declaration of function `FreeLibrary'
When you submit properly standard C code in a compilable form,
complete with a 'main', someone will be happy to critique it.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
.
- Follow-Ups:
- References:
- Prev by Date: Re: define a struct that has its own type as a field somehow?
- Next by Date: Re: va_arg()
- Previous by thread: Re: problems calling a function contained in a dll
- Next by thread: Re: problems calling a function contained in a dll
- Index(es):
Relevant Pages
|