Home > OS >  How to replace the RCData of an executable?
How to replace the RCData of an executable?

Time:10-16

I'm trying to modify the RCData of a compiled AutoHotkey script:

   void ReplaceStringTable() {

    HANDLE hRes = BeginUpdateResource( _T( "C:\\Users\\CAIO\\Documents\\Github\\main\\scripts\\ahkDebug\\Novo(a) AutoHotkey Script.exe" ), FALSE );
    if ( hRes != NULL ) {
        std::wstring data[] = { L"MsgBox Test" };

        std::vector< WORD > buffer;
        for ( size_t index = 0;
              index < sizeof( data ) / sizeof( data[ 0 ] );
                index ) {

            size_t pos = buffer.size();
            buffer.resize( pos   data[ index ].size()   1 );
            buffer[ pos   ] = static_cast< WORD >( data[ index ].size() );
            copy( data[ index ].begin(), data[ index ].end(),
                  buffer.begin()   pos );
        }
        UpdateResource( hRes,
                        RT_RCDATA,
                        L">AUTOHOTKEY SCRIPT<", //MAKEINTRESOURCE( 1 ),
                        1033, //MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
                        reinterpret_cast< void* >( &buffer[ 0 ] ),
                        buffer.size() * sizeof( WORD ) );

        EndUpdateResource( hRes, FALSE );
    }
}

However, this is the result after running the code:

RCData

The code does add an empty blank line (line 1), this line makes the exe not work correctly, how do I get rid of it?

CodePudding user response:

You need to use std::string instead of std::wstring, as AHK is expecting 8bit characters, not 16bit characters. Also, you need to get rid of your vector, as AHK does not expect each line to be prefixed by its length.

Try this instead:

void ReplaceStringTable() {

    HANDLE hRes = BeginUpdateResource( TEXT( "C:\\Users\\CAIO\\Documents\\Github\\main\\scripts\\ahkDebug\\Novo(a) AutoHotkey Script.exe" ), FALSE );
    if ( hRes != NULL ) {
        std::string data = "MsgBox Test";

        UpdateResource( hRes,
            RT_RCDATA,
            L">AUTOHOTKEY SCRIPT<", //MAKEINTRESOURCE( 1 ),
            1033, //MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
            const_cast<char*>(data.c_str()),
            data.size() );

        EndUpdateResource( hRes, FALSE ); 
    }
}
  • Related