I have a structure like this
#define MAX_NAME_LENGTH 40
typedef struct _tagMYPERSON
{
WCHAR FName[MAX_NAME_LENGTH];
WCHAR LName[MAX_NAME_LENGTH];
int Age;
double Salary;
}MYPERSON;
then I have a function in a DLL like this
extern "C" MARSHALDLL_API void _stdcall ModifyPersonStructArray(int nCount, MYPERSON * pPersonArr)
{
for ( int nI = 0; nI < nCount; nI++ )
{
wcscat_s(pPersonArr[nI].FName, MAX_NAME_LENGTH, L"-Modified");
wcscat_s(pPersonArr[nI].LName, MAX_NAME_LENGTH, L"-Modified");
pPersonArr[nI].Age = pPersonArr[nI].Age+ 5;
pPersonArr[nI].Salary = pPersonArr[nI].Salary + 1000 ;
}
}
and This is how I call it.
int nCount = 4;
MYPERSON* prsnArr = new MYPERSON [nCount];
CString strTmp;
m_lstOutput.ResetContent ();
for (int nI=0; nI<nCount ; nI++)
{
strTmp.Format(_T("First Name %d"), nI+1);
StringCchCopyW(prsnArr[nI].FName, MAX_NAME_LENGTH, strTmp);
strTmp.Format(_T("Last Name %d"), nI+1);
StringCchCopyW(prsnArr[nI].LName, MAX_NAME_LENGTH, strTmp);
prsnArr [nI].Age = 20 + (nI+ 1)*2 ;
prsnArr [nI].Salary = 20000 + (nI+ 1) * 1000;
strTmp.Format(_T("Person %d=[(%s %s, Age=[%d], Salary=[%.2lf])]"), nI, prsnArr [nI].FName, prsnArr [nI].LName, prsnArr [nI].Age, prsnArr [nI].Salary);
m_lstOutput.AddString (strTmp);
}
ModifyPersonStructArray(nCount, prsnArr );
m_lstOutput.AddString(_T("After Call ==>"));
for (int nI=0; nI<nCount ; nI++)
{
strTmp.Format(_T("Person %d=[(%s %s, Age=[%d], Salary=[%.2lf])]"), nI, prsnArr [nI].FName, prsnArr [nI].LName, prsnArr [nI].Age, prsnArr [nI].Salary);
m_lstOutput.AddString (strTmp);
}
Note : m_lstOutput is a control variable (ListBox) in a MFC Dialog.
When I run in debug mode I get some error message like this,
Windows has triggered a breakpoint in MFCClient.exe.
This may be due to a corruption of the heap, which indicates a bug in MFCClient.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while MFCClient.exe has focus.
The output window may have more diagnostic information.
What I am doing wrong here ?
What I have tried:
I transferred the DLL function in the Dialog class itself. like.
void CMFCClientDlg::ModifyPersonStructArray(int nCount, MYPERSON * pPersonArr)
{
for ( int nI = 0; nI < nCount; nI++ )
{
wcscat_s(pPersonArr[nI].FName, MAX_NAME_LENGTH, L"-Modified");
wcscat_s(pPersonArr[nI].LName, MAX_NAME_LENGTH, L"-Modified");
pPersonArr[nI].Age = pPersonArr[nI].Age+ 5;
pPersonArr[nI].Salary = pPersonArr[nI].Salary + 1000 ;
}
}
and i have no issues. it works.
please suggest.