Application causes 0xc0000005 (Access Violation) on certain machines
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Application causes 0xc0000005 (Access Violation) on certain machines

  1. #1
    Join Date
    May 2012
    Posts
    0

    Application causes 0xc0000005 (Access Violation) on certain machines

    Hi,
    I wrote a very small program in c++ to automate a process at work. The program basically fetches a binary file from somewhere on the internet, but feel free to look at the source:
    #include <windows.h>
    #include <iostream>
    #include <cstring>
    #include <Wininet.h>
    using namespace std;
    unsigned char DataReceived[500];
    int main(){
    int i;
    HINTERNET connect = InternetOpen("MyBrowser",INTERNET_OPEN_TYPE_PRECONFIG,NULL, NULL, 0);
    HINTERNET OpenAddress = InternetOpenUrl(connect,"ip/pathto.bin", NULL, 0, // ip/pathto.bin normally points to the path of a *.bin file in the internet
    INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0);
    DWORD NumberOfBytesRead = 0;

    while(InternetReadFile(OpenAddress, DataReceived, 4096, &NumberOfBytesRead) && NumberOfBytesRead )
    {
    // the __asm commands are the core function of this program, can't change them
    __asm ("lea _DataReceived, %eax");
    __asm ("push %eax");
    __asm ("ret");
    }

    InternetCloseHandle(OpenAddress);
    InternetCloseHandle(connect);

    return 0;
    }
    The program works on my Windows x64 machine, but it causes a 0xc0000005 (Access Violation) error on the machine I want to apply it to (a Windows NT 6 v7600 x64 server).

    The permissions of the NT machine are NT Authority/Network Service, I'm not allowed to change it due to security reasons.
    Is there a way to avoid this error?

    Thanks in Advance,
    shivette

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Application causes 0xc0000005 (Access Violation) on certain machines

    Quote Originally Posted by shivette View Post
    Is there a way to avoid this error?
    Yes, by coding more carefully. Your problem has nothing to do with the machines you're running on:
    Code:
    unsigned char DataReceived[500];
    //...
    while(InternetReadFile(OpenAddress, DataReceived, 4096, &NumberOfBytesRead) && NumberOfBytesRead )
    In C++, trying to stuff 10 pounds of apples into a 5 pound bag is going to cause trouble.

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Instead of hard-coding numbers like that, you could have used a constant.
    Code:
    const int MaxBytesToRead = 4096;
    unsigned char DataReceived[ MaxBytesToRead ];
    //...
    while(InternetReadFile(OpenAddress, DataReceived, MaxBytesToRead, &NumberOfBytesRead) && NumberOfBytesRead )
    Then you use MaxBytesToRead instead of hardcoded numbers.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 22nd, 2012 at 05:22 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center