tags:

views:

315

answers:

3

I need to read data added to the end of an executable from within that executable .
On win32 I have a problem that I cannot open the .exe for reading. I have tried CreateFile and std::ifstream.
Is there a way of specifying non-exclusive read access to a file that wasn't initially opened with sharing.

EDIT- Great thing about stackoverflow, you ask the wrong question and get the right answer.

+3  A: 

Why not just use resources which are designed for this functionality. It won't be at the end, but it will be in the executable.

If you are adding to the .exe after it is built -- you don't have to add to the end, you can update resources on an built .exe

http://msdn.microsoft.com/en-us/library/ms648049(VS.85).aspx

Lou Franco
It's not my design - somebody else generates the file by appending data to a stub exe.
Martin Beckett
Thanks for the link - I didn't know you could programmatically update resources without relinking.
Martin Beckett
+1  A: 

We do this in one of our projects. What's the problem with it? If the EXE is running, then it's already held open for reading, and you can continue to open it read-only multiple times. I just checked our code, we just use:

HANDLE file=CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);

This works without problem on all versions of 32- and 64-bit Windows to date.

Head Geek
+1  A: 

I have no problem opening the executable image of a process using either of these statements:

FILE* f = fopen( fname, "rb");

hFile = CreateFile( fname, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);

What's your code?

Michael Burr