Question about fwrite under win32 platform
I'm writing a win32 dialog application by using message crack macro, the problem is that I can't store the data into a target file correctly, but I use the same code in a console app, everything is ok. Here is a part of code about file writing.
Code:
void Dlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
HWND add_hwnd;
int i,r1,r2,r3,r4,ret;
LPTSTR t_isp = (LPTSTR)malloc(sizeof(LPTSTR));
LPTSTR t_uid = (LPTSTR)malloc(sizeof(LPTSTR));
LPTSTR t_pws = (LPTSTR)malloc(sizeof(LPTSTR));
LPTSTR t_num = (LPTSTR)malloc(sizeof(LPTSTR));
add_hwnd = GetDlgItem(hwnd,IDB_ADD);
switch(id)
{
case IDCANCEL:
EndDialog(hwnd,id);
break;
case IDB_ADD:
i = 0;
if(GetDlgItemText(hwnd,IDE_ISP,t_isp,32) == 0 ||
GetDlgItemText(hwnd,IDE_ISP,t_isp,32) ==0 ||
GetDlgItemText(hwnd,IDE_PASSWORD,t_pws,32) ==0 ||
GetDlgItemText(hwnd,IDE_NUMBER,t_pws,32) ==0)
{
MessageBox(hwnd,"Some fields are empty!","Server Side",MB_OK);
break;
}
if(count > 0 && count !=0 && i<SIZE)
{
GetDlgItemText(hwnd,IDE_ISP,t_isp,32);
r1 = GetLastError();
GetDlgItemText(hwnd,IDE_USERNAME,t_uid,32);
r2 = GetLastError();
GetDlgItemText(hwnd,IDE_PASSWORD,t_pws,32);
r3 = GetLastError();
GetDlgItemText(hwnd,IDE_NUMBER,t_num,32);
r4 = GetLastError();
if(r1 == 1400 || r2 == 1400 || r3 == 1400 || r4 == 1400)
{
MessageBox(hwnd,"GetLastError","Server Side",MB_OK);
break;
}
info[i].isp = t_isp;
info[i].uid = t_uid;
info[i].pws = t_pws;
info[i].num = t_num;
count = count - 1;
i = i + 1;
SetDlgItemText(hwnd,IDE_ISP,"");
SetDlgItemText(hwnd,IDE_USERNAME,"");
SetDlgItemText(hwnd,IDE_PASSWORD,"");
SetDlgItemText(hwnd,IDE_NUMBER,"");
}
else if(count == 0)
{
EnableWindow(add_hwnd,FALSE);
MessageBox(hwnd,"The limitation of the record is arrived.","Server Side",MB_OK);
}
else
{
MessageBox(hwnd,"The limitation of the record is arrived","Server Side",MB_OK);
}
break;
case IDB_SAVE:
fp = fopen("data.dat","ab");
for(i = 0; i < SIZE; i++)
ret = fwrite(&info[i],sizeof(struct information),1,fp);
if(-1 != ret)
MessageBox(hwnd,"Save is ok!","Server Side",MB_OK);
else
MessageBox(hwnd,"Save is not completed!","Server Side",MB_OK);
fclose(fp);
break;
}
}
I set a break point into
Code:
ret = fwrite(&info[i],sizeof(struct information),1,fp);
the content in info[i] is correct and return value is also correct. Why the content in the target file is incorrect?