Click here to Skip to main content
14,426,487 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have pass a nested structure into a dll function,
it return "Access Violation from the dll function.

Is my code have any problem ?
Thank you so much

What I have tried:

#define CM24_NUMDEPCASSETTE			24

typedef struct SNOTETYPE
{
	char Notetype[6];
	int NumNote;
}SNOTE;

struct SWITH
{
	int OutNumber;
	SNOTE Types[CM24_NUMDEPCASSETTE * 4]; 
};

// Here is the function, i use LoadLibrary
typedef int (WINAPI*FNPTR) (SWITH RequestBankNot, SWITH WithdrawData);


FNPTR fnWithdraw = (FNPTR)GetProcAddress(Hinst, "InterDoWithdrawall");
if (fnWithdraw)
{
	SWITH vWithdrawall, vWithdrallData;
	vWithdrawall.OutNumber = 0;
	vWithdrawall.Types.NumNote = 1;
	strncpy_s(vWithdrawall.Types.Notetype, sizeof(vWithdrawall.Types.Notetype), "CPEA", sizeof(SNOTETYPE::Notetype));

	vWithdrallData.OutNumber = 0;
	vWithdrallData.Types.NumNote = 0;
	
       
	int Result = fnWithdraw(vWithdrawall, vWithdrallData); // here it prompt me error message
	std::cout << "\nResult:\n", Result;
}
Posted
Updated 13-Oct-19 23:42pm
Comments
CPallini 10-Oct-19 5:22am
   
You didn't post the InterDoWithdrawall function code.
Rate this:
Please Sign up or sign in to vote.

Solution 1

That code will not compile.
vWithdrawall.Types.NumNote = 1;

// Types is an array so needs an array index to be addressed correctly
//
   
Comments
chillichong 10-Oct-19 21:23pm
   
the code will be something like "vWithdrawall.Types[0].NumNote = 1;"

Am i have to fill all array then send to the function or i just send 1 row of data to the function?

Thank you
Richard MacCutchan 11-Oct-19 3:49am
   
How can we know what you have to send? Look at the documentation for the DLL or ask the people who wrote it.
chillichong 14-Oct-19 3:56am
   
i have change something on my code, but it still give same error.
Please give me some advice, Thank you


#define CM24_NUMDEPCASSETTE 24

typedef struct SNOTETYPE
{
char Notetype[6];
int NumNote;
}SNOTE;

typedef struct SWITH
{
int OutNumber;
SNOTE Types[CM24_NUMDEPCASSETTE * 4]; //[CM24_NUMDEPCASSETTE * 4]
}SWITHDRAWALL;
typedef SWITHDRAWALL *PASWITHDRAWALLCASS;

typedef int (WINAPI*FNPTR) (SWITHDRAWALL RequestBankNot, SWITHDRAWALL WithdrawData);

FNPTR fnWithdraw = (FNPTR)GetProcAddress(Hinst, "InterDoWithdrawall");
if (fnWithdraw)
{
struct SWITH *ptrWithdrawall, stWithdrawall;
struct SWITH *ptrWithdrawdata, stWithdrawdata;

ptrWithdrawall = &stWithdrawall;
stWithdrawall.OutNumber = 0;
stWithdrawall.Types[0].NumNote = 1;
strncpy_s(stWithdrawall.Types[0].Notetype, sizeof(stWithdrawall.Types[0].Notetype), "CPEA", sizeof(SNOTETYPE::Notetype));

ptrWithdrawdata = &stWithdrawdata;
stWithdrawall.OutNumber = 0;
stWithdrawall.Types[0].NumNote = 0;
strncpy_s(stWithdrawall.Types[0].Notetype, sizeof(stWithdrawall.Types[0].Notetype), "", sizeof(SNOTETYPE::Notetype));

int Result = fnWithdraw(stWithdrawall, stWithdrawdata);
std::cout << "\nResult:\n", Result;
Richard MacCutchan 14-Oct-19 4:42am
   
See below.
Rate this:
Please Sign up or sign in to vote.

Solution 2

int Result = fnWithdraw(stWithdrawall, stWithdrawdata);

You are passing the complete structures to your dll function instead of the pointers. This should have been shown up by the compiler.
The correct code (I assume) should be:
int Result = fnWithdraw(ptrWithdrawall, ptrWithdrawdata);
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100