Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AccessViolationException while calling DLL

Status
Not open for further replies.

calderon

Technical User
Jan 18, 2010
9
ES
I created a simple DLL with the following code:

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

UINT A_AddOne (UINT32 Base)
{
return Base + 1;
}

I created a .def file an from VB.net I can call this DLL without problems. Now I created a second DLL that calls the first:

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

typedef UINT (*A_ADDONE)(UINT32);

UINT B_AddOne (UINT32 uiBase)
{
HINSTANCE hinstLib;
A_ADDONE ProcAdd;
BOOL fFreeResult = FALSE;
UINT32 param;
int Result;
Result = 0;
param = uiBase;
hinstLib = LoadLibrary(TEXT("C:\Visual Studio Projects\ESD\debug\DLL_A.dll"));
if (hinstLib = NULL) return 101;
ProcAdd = (A_ADDONE) GetProcAddress(hinstLib, TEXT("A_AddOne"));
if (ProcAdd = NULL) return 102;
Result = ProcAdd (param);
fFreeResult = FreeLibrary(hinstLib);
return Result;
}

When I try to call this function an AccessViolationException is thrown as I reach the line Result = ProcAdd (param);

Can anyone explain me why?
Thanks
 
Code:
    if (ProcAdd =[COLOR=RED]=[/COLOR] NULL) return 102;
 
Thanks xwb,
This shurely is an important syntax error. Now I don´t get the AccessViolationError anymore.

The problem is that the LoadLirary call fails (resulting in my error code 101). The DLL path is correct and from VB.net I can call DLL_A perfectly using the declare keyword.

Any more hints?
 
It is not a syntax error as such. You can avoid it by switching the check the other way round
Code:
if (NULL == ProcAdd) return 102;
If you'd dropped one of the =, it would be syntax error. Doesn't read as well though.

Very strange that your code worked in VB.net Anyway, you could try changing the declaration to
Code:
__declspec (dllexport) UINT A_AddOne (UINT32 Base)
 
I tried this, but it doesn't work. I think that DLL_A works fine, since I can use it from VB. To export the routine both DLL´s have a .def file. The dllexport apears to be a substitute for the .def file, but is not necasary if you have a .def file. Anyway it seems that DLL_B can not find DLL_A for some reason, so it will not even be able to search for the routine A_AddOne...
 
Presumably you have compiled it as a C library and not a C++ one. Check that you do not have the /TP flag set in compilation. This will compile it as C++ and will require name mangling. Make sure it is /TC.
 
You may want to follow the previous advise since you also have:

if (hinstLib = NULL) return 101;

When I worked at a large institution I got to write the coding rules and the NULL was ALWAYS on the left (unless an assignment was going on).

So rewrite it properly and it should work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top