problems calling a function contained in a dll

A

Alan

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
 
B

Bill Medland

Alan said:
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 == NULL)
{
fprintf(stderr, "ERROR: Unable to find the _ANT_Init function in the
DLL\n");
return 1;
}
if (ANT_Init(0, 50000) == true)

Beware. Seeing this sort of test always scares me. I would suggest that
if (ANT_Init(0, 50000) != false) would be safer although I would use
if (ANT_Init(0, 50000)) myself.
{
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
If this does not show that the problem is not finding the funtion then I
expect the error is within ANT_Init and I would use the debugger to prove
it and pass the problem back to the third party.
 
A

Alan

Hi Bill,

Thank you for your post. I don't think my problem is related to the
test of the value returned by ANT_Init, as I've tried to call this
function by itself, without testing the value returned and it did the
same thing.

Within the debugger, I'm able to place a breakpoint next to this line
of code, but whenever I ask it, to "step into" the function, the error
message appears.

Any other suggestions?

Thanks again,

Alan


Bill said:
Alan said:
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 == NULL)
{
fprintf(stderr, "ERROR: Unable to find the _ANT_Init function in the
DLL\n");
return 1;
}
if (ANT_Init(0, 50000) == true)

Beware. Seeing this sort of test always scares me. I would suggest that
if (ANT_Init(0, 50000) != false) would be safer although I would use
if (ANT_Init(0, 50000)) myself.
{
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
If this does not show that the problem is not finding the funtion then I
expect the error is within ANT_Init and I would use the debugger to prove
it and pass the problem back to the third party.
 
K

Keith Thompson

Alan said:
I'm having difficulties loading a dll in Microsoft Visual Studio 2003,
[snip]

Sorry, you're in the wrong place. Try a Windows-related newsgroup.
comp.os.ms-windows.programmer.win32 *might* be the right place.
 
A

Ancient_Hacker

Alan said:
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:

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


Urp, I looked up __fastcall to refresh my memory. It's one of the
flakier "standards". In bold face text the doc states "this method may
change in future compilers".

What likely happened is your vendor compiled the DLL with a compiler
that followed the __fastcall convention, as it was understood in 1998
or thereabouts by Borland.

Now your relatively new 2003 compiler from their arch-competitor may
have changed the rules for __stdcall, so the parameter expected in edx
register may be being passed somehow else.

I'd go and find the exact same compiler your vendor used and that will
probably work.
meanwhile suggest to your vendor they move to a more modern compiler.
 
J

jacob navia

Alan said:
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");

NOWHERE you test if GetProcAddress returns NULL (meaning it did not find
the function)

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."

You have tried to execute a NULL pointer. Look at this:
> "Unhandled exception at 0x00000000 in program.exe

This means that you have a NULL function pointer and you try to execute
it. See above.

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


HINT
Are you sure you should put the underscore in the function name???
ANT_Init = (_ANT_Init)GetProcAddress(hDLLInstance, "_ANT_Init");
if (ANT_Init == NULL) {
// Try without underscore
ANT_Init = (_ANT_Init)GetProcAddress(hDLLInstance, "ANT_Init");
if (ANT_Init == NULL) {
printf("Error. Unable to find ANT_Init in the dll\n");
exit(-1);
}

}
 
J

jacob navia

Ancient_Hacker said:
Urp, I looked up __fastcall to refresh my memory. It's one of the
flakier "standards". In bold face text the doc states "this method may
change in future compilers".

What likely happened is your vendor compiled the DLL with a compiler
that followed the __fastcall convention, as it was understood in 1998
or thereabouts by Borland.

Now your relatively new 2003 compiler from their arch-competitor may
have changed the rules for __stdcall, so the parameter expected in edx
register may be being passed somehow else.

I'd go and find the exact same compiler your vendor used and that will
probably work.
meanwhile suggest to your vendor they move to a more modern compiler.

This could be an error, but I think it is more likely he has a
NULL result from GetProcAddress since

Unhandled exception at 0x00000000 in program.exe.

This means the program counter is zero, i;e. the typicall result when
you execute a NULL pointer as a function pointer
 
A

Ancient_Hacker

good eyes!

Yep, you should always check to see that the function was found before
trying to call it.

Often there's some glitch having to do with underscores extra or
missing, or name capitalization. The name has to exactly match the
name exported by the DLL.

Use something like dumpbin /exports vendor.DLL to see the exact
exported names.
 
C

CBFalconer

Alan said:
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.
 
A

Alan

Thank you for your post. I think you've come up with a good reason why
my program doesn't work. They told me today, that they used Borland C++
5. I downloaded an evaluation version of Borland C++ 6 and it didn't
work either.

Thanks again,

Alan
 
A

Alan

You don't have the dll, which I use or the header files, which is why
it doesn't compile...

Alan said:
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.
 
M

Mark McIntyre

You don't have the dll, which I use or the header files, which is why
it doesn't compile...

It doesn't compile because it uses nonstandard extensions to C, which,
as CBF mentioned, are offtopic here. You need to ask in a microsoft
programming grupo (despite using Borland, its still an MS
environment).

Also please don't top-post in CLC.

CBF Wrote
<nit>
This is incorrect. Since the idenfiier names in question are provided
by the compiler implementor, there is no UB.
</nit>

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

CBFalconer

Alan said:
You don't have the dll, which I use or the header files, which is
why it doesn't compile...

Don't top-post here. Of course I don't have the dll, this
newsgroup deals with standard C, which has no knowledge of dlls or
arbitrary headers. One reason 'it' doesn't compile is that it is
not written in standard C.

For things to do with dll's seek a newsgroup with 'Mickeysoft' or
something like that in its name.
 
K

Kenny McCormack

You don't have the dll, which I use or the header files, which is why
it doesn't compile...

Um, Alan, you're being trolled. They pretend that your code is
defective, because they can't compile it with their crippled compilers
(aka, development environments) - crippled, quite often, by their using
goofy compiler options (like -ansi and/or -pedantic) that turn off most
of the functionality of the compiler (aka, development environment).

Pay them no mind. Just move on.
 
K

Kenny McCormack

Don't top-post here. Of course I don't have the dll, this
newsgroup deals with standard C, which has no knowledge of dlls or
arbitrary headers. One reason 'it' doesn't compile is that it is
not written in standard C.

Most likely, the reason you couldn't compile it, besides not having the
DLL, is that you've hobbled your compiler with some weird command line
options.
 
Joined
Apr 25, 2009
Messages
1
Reaction score
0
help

Hello Alan... do you have the original project sorce code..
i read that is on Borland c++ (builder)
I work with this edition..
and I'm looking for that sample project...


can yu share it with me please??

thanx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top