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:
Quote:
#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
Re: Application causes 0xc0000005 (Access Violation) on certain machines
Quote:
Originally Posted by
shivette
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