CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2010
    Posts
    20

    [RESOLVED] store a string in an executable

    hello,

    maybe someone can give me a brief instruction how to achieve this best:

    I would like to program a tool which stores a string in an executable. This string should somehow be created from for example the size of the executable and the checksum or something like that. But that's not that important at the moment.

    I just want to know how to store that string and how to find it again in the exe to verify it. And this exe should of course be still executable.

    I would appreciate any help.

    Kind regards

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: store a string in an executable

    Since you're posting in the WinAPI forum, I'll assume this is a windows exe. The easiest way I could think of is to store the string as a resource the same as an icon or a bitmap.

    Alternatively, you could declare a new section in the PE executable structure with a pragma:

    Code:
    #pragma code_seg(push, rl,".mysection")
    const char MyString[]="Hello Hackers!";
    #pragma code_seg(pop)
    then, after compilation, parse the exe file for that section, but the PE structure is rather complicated and difficult to parse.

  3. #3
    Join Date
    May 2010
    Posts
    20

    Re: store a string in an executable

    thank you for your answer.

    yes, it is a windows exe. Storing it as a resource sounds good. But how can I programmatically add a resource to a different executable? Is that possible?

    I started to read
    http://www.codeguru.com/cpp/w-p/win3...table-File.htm

    and this PE structure really seems to be rather complicated. But the sample projects at the end of the page may be usefull at least.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: store a string in an executable

    You can get a module handle to any EXE with LoadLibrary(). With that handle, you can call FindResource(), LoadResource(), then LockResource() will get you a memory pointer to the raw resource and SizeOfResource() will get you the length.

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: store a string in an executable

    Quote Originally Posted by Makiman View Post
    I would like to program a tool which stores a string in an executable. This string should somehow be created from for example the size of the executable and the checksum or something like that...
    You already have such a tool – linker.
    Check out its /RELEASE option.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    May 2010
    Posts
    20

    Re: store a string in an executable

    i did the following and it seems to be exactly what I need

    for storing:
    Code:
        HGLOBAL hResLoad;     
        HMODULE hExe;        
        HRSRC hRes;         
        HANDLE hUpdateRes;   
        TCHAR *lpResLock;     
        BOOL result; 
    
    
        hUpdateRes = BeginUpdateResource(_T("c:\\Example_1.exe"),false);
        TCHAR* str = _T("abc123");
        result = UpdateResource(hUpdateRes,
                                RT_STRING,
                                _T("KEY"),
                                MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
                                str,
                                wcslen(str)*2);
    
        EndUpdateResource(hUpdateRes,false);
    well, I am not completely happy about that "wcslen(str)*2" to deterimine the needed size. Maybe anyone has a better idea about that.

    for reading:
    Code:
        hExe = LoadLibrary(_T("c:\\Example_1.exe"));
        hRes = FindResource(hExe, _T("KEY"), RT_STRING); 
        hResLoad = LoadResource(hExe, hRes);
        lpResLock = (TCHAR*)LockResource(hResLoad);
    Thanks a lot to all!

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: store a string in an executable

    When you use the "T" macros for ansi/unicode compatability, you need to use them throughout. Use tclen() and sizeof(TCHAR):

    Code:
        result = UpdateResource(hUpdateRes,
                                RT_STRING,
                                _T("KEY"),
                                MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
                                str,
                                tclen(str)*sizeof(TCHAR));

  8. #8
    Join Date
    May 2010
    Posts
    20

    Re: store a string in an executable

    thanks, it works!

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: store a string in an executable

    So now you can modify and read back resource strings. Great!
    But how does it help you with checksum verification?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  10. #10
    Join Date
    May 2010
    Posts
    20

    Re: store a string in an executable

    I still have to think about that algorithm to create that string to store in the exe and this algorithm must be secret. Checksum was only an example.

    I have a COM-API to my application (not the tool to "sign") and I need a way to verify that the calls to API methods come from a "signed" executable. And only people with a specific license can use that signing tool.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured