Link to home
Start Free TrialLog in
Avatar of F-J-K
F-J-KFlag for Canada

asked on

How Can I Edit .exe File by Using C/C++? - Code is Posted, I Need Explanation

I got this code in the internet. I mostly understood the code, but i need clarification on some parts. These spot i need somebody to explain it to me:

CreateFile("Application.exe", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

1. Can you explain what these parameters do?

SetFilePointer(hFile, BYTE_OFFSET, NULL, FILE_BEGIN);

2. I know this points to the offset. Any sugestions on where does this function usually used?

WriteFile(hFile, szBuff, variableSize , &ret, NULL);

3. What is ret return? What is NULL represent?

4. Is patching still used nowadays for updating applications? I never find companies out there giving me patches to update apps i have. I think its done online. Anyhow, any comments will be appreciated.

Please note this code i got in one of the IT websites, i know its not a proper one to use it in a serious application since there are security flaws in it.
#include <windows.h>
#include <stdlib.h>
 
#define variableSize 20
#define BYTE_OFFSET 0x0000FE2A   //assume this is the address of the place we want to edit
 
int main()
{
    LPSTR szBuff = new char[20];   memset(szBuff, 0, 20);
    DWORD ret=0;    
  
    HANDLE hFile = CreateFile("Application.exe", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
     if(hFile == INVALID_HANDLE_VALUE){
       printf("\t\tError opening file.\n");
       delete[] szBuff;
       return 0;  
     }
  
    printf("Enter The Change You Want to Make in Application.exe");
    fgets(szBuff, 19, stdin);
  
    if(strcmp(szBuff, " ") == 0 || strlen(szBuff) == 0){
      printf("Error, you did not enter any data\n");
      delete[] szBuff;
      return 0;
    }
  
     SetFilePointer(hFile, BYTE_OFFSET, NULL, FILE_BEGIN);
     WriteFile(hFile, szBuff, variableSize , &ret, NULL); 
    
      CloseHandle(hFile);
      
      delete []szBuff;
    
     printf("Exe Edited :)\n");
     return 0;
}

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

1, 2, 3 - why not look the functions up in the help files.  If anything is not clear then ask for further clarification.

4 - the online is still a patch, just supplied via the internet rather than disc in the post.  The delivery method is different.
Avatar of F-J-K

ASKER

I forgot to mention. I will google the functions anyway, but your help won't harm. Anyway, i will get back with questions after looking up the functions
The parameters to 'CreateFile()' are decribed at http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

The variables to 'SetFilePointer()' can be found here http://msdn.microsoft.com/en-us/library/aa365541.aspx

That function is used to advance towards the offset into the file, since when you open it for writing, the starting offset is '0', which means writing to the beginning of the ile instead of to the desired position.

In your call to 'WriteFile()' (http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx), 'ret' will contain the number of bytes that were actually written after the function returns. If that number is different from 'szBuf', something went wrong. 'NULL' as the last parameter here means that you are performing a synchronous operation. If that parameter would point to an 'OVERLAPPED' struct (where Windows stores data for async operations), this function would return immediately and you'd get notified later about the result.
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of F-J-K

ASKER

Can't thank you enough
>>>> I never find companies out there giving me patches to update apps i have. I think its done online.

For patches there are 3 variants:

1. A full setup with all executables (.exe and .dll)
2. Exchanging only one or more dlls
3. Exchanging only resource data.

(3) for example is done with anti-virus scanners. They only download and install signatures if new viruses and use still the old versions.
(2) often was done after an installation from CD. The freshly installed application asks whether it should get updates from the internet. If they installed a new dll, you often need to reboot in order to make the changes valid.
(1) is only done if the changes were too big or if it were some basic errors to fix.