Click to See Complete Forum and Search --> : WriteFile cause an exception in kernel


leo liu
April 16th, 1999, 03:37 AM
Windows NT/98
VC 6.0

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HANDLE m_hFile=CreateFile("c:\\cvl.log",GENERIC_WRITE,0,NULL,
OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
SetFilePointer(m_hFile,0,0,FILE_END);
char sztemp[256]="132.147.70.1,,1999-4-16 15:57:18,1999-4-16 15:57:35";
DWORD dwSize;
for(dwSize=0;sztemp[dwSize];dwSize++);
WriteFile(m_hFile,sztemp,dwSize-1,NULL,NULL);
CloseHandle(m_hFile);
return 0;
}
When this code ,it cause an exception(access violation).The following is the error information.
"First-chance exception in test1.exe (KERNEL32.DLL): 0xC0000005: Access Violation."
Who can help me?

Jason Teagle
April 16th, 1999, 06:45 AM
I think it is because you have supplied NULL as the lpNumberOfBytesWritten parameter to WriteFile(); the help file says that it writes zero here before doing anything. If you supplied NULL, it would try to write 0 to address 0, which certainly counts as an access violation. Try adding a local variable of type DWORD and passing its address for this parameter instead.