Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 22:42pm
Comments
CPallini 10-Oct-19 5:22am    
You didn't post the InterDoWithdrawall function code.

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

// Types is an array so needs an array index to be addressed correctly
//
 
Share this answer
 
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.
C++
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:
C++
int Result = fnWithdraw(ptrWithdrawall, ptrWithdrawdata);
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900