CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Posts
    15

    WriteFile cause an exception in kernel

    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?


  2. #2
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: WriteFile cause an exception in kernel

    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.




    --
    Jason Teagle
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured