Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable reading itself

Tags:

c++

file

winapi

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.

like image 756
Martin Beckett Avatar asked Sep 05 '25 16:09

Martin Beckett


2 Answers

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 a built .exe

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

like image 103
Lou Franco Avatar answered Sep 07 '25 09:09

Lou Franco


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.

like image 28
Head Geek Avatar answered Sep 07 '25 09:09

Head Geek