Re: problems calling a function contained in a dll



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>


.



Relevant Pages

  • Re: problems calling a function contained in a dll
    ... I'm having difficulties loading a dll in Microsoft Visual Studio 2003, ... If the actual compiler or system is relevant, ... junk.c:19: warning: implicit declaration of function `LoadLibraryA' ... Chuck F (cbfalconer at maineline dot net) ...
    (comp.lang.c)
  • Re: Question about 6.2.4 of C99
    ... compilers to reuse memory in some cases, ... muddying up the semantics and/or making the programmer work harder. ... than it would be if the declaration created and really ... I understand why the VLA rules are the way they are. ...
    (comp.std.c)
  • Re: Question about 6.2.4 of C99
    ... compilers to reuse memory in some cases, ... muddying up the semantics and/or making the programmer work harder. ... the same way it does for VLAs? ... I write an intra-block declaration and every time I read one I ...
    (comp.std.c)
  • Re: C-AUX / C and binary portability
    ... trigraphs, digraphs, and K&R declarations are, in many C compilers, currently at the level where they will cause compiler warnings. ... which is ambiguous, but in this case, it will just assume that the declaration was intended. ... the reason is that, if one does infact include headers and expand macros here, then the generated code will depend on whatever macros/constants/... ...
    (comp.lang.misc)
  • Re: Exposing bool to non-MS, non-C++ callers
    ... the functions in this DLL accept a structure as an argument. ... The compiler doesn't help catch places where I neglect ... the C++ bool type is defined as one byte. ... depends on whether the Exe caller will push a value larger than 1 byte. ...
    (microsoft.public.dotnet.languages.vc)