1

Possible Duplicate:
updating a string table with UpdateResource

I am trying to update a binaries STRING TABLE via UpdateResources, currently, my code ONLY adds a single string, but I want it to add multiple entries into the string table.

Here is my code:

 HANDLE hRes = BeginUpdateResource("file.exe",TRUE);
    if (hRes)
    {
        char * sNewString = "Line";
        int iCharCount = (strlen(sNewString) + 1);   //sNewString.size() + 1;
        LPWSTR pUnicodeString = new WCHAR[iCharCount];
        if (!pUnicodeString)
        {
            cout << "Error" << endl;
        }
        DWORD dwUnicodeCharCount = MultiByteToWideChar(CP_ACP,NULL,sNewString,-1,pUnicodeString,iCharCount);
        HGLOBAL hGlob = GlobalAlloc(GHND,(dwUnicodeCharCount + 4) * sizeof(WCHAR));
        LPWSTR pBufStart = (LPWSTR)GlobalLock(hGlob);
        LPWSTR pBuf = pBufStart;
        pBuf++;
          // offset to make it string ID=1. Increment by more
            // to change to a different string ID
        //next 2 bytes is string length
        *(pBuf++) = (WCHAR)dwUnicodeCharCount-1;
        for (int i = 0; i < (int)dwUnicodeCharCount-1; i++)
        {
            dwUnicodeCharCount++;
        }
        delete pUnicodeString;
        if (++dwUnicodeCharCount%1) // make sure even number
        {
            dwUnicodeCharCount++;
        }
        if(!UpdateResource(hRes,RT_STRING,MAKEINTRESOURCE(1),MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
                          pBufStart, dwUnicodeCharCount * sizeof(WCHAR)))
        {
            cout << "Function failed!" << endl;
        }
        GlobalUnlock(hGlob);
        GlobalFree(hGlob);
        EndUpdateResource(hRes,FALSE);

I attempted to edit `

char * sNewString = "Line";

to `

char * sNewString = "Line1\nLine2";

as an attempt to mimic the newline character to no avail.

Am I missing something? I looked thoroughly at http://msdn.microsoft.com/en-us/library/windows/desktop/aa381050(v=vs.85).aspx , and it doesn't look like I have missed any crucial points, and no functions are failing.

Community
  • 1
  • 1
James
  • 236
  • 5
  • 18
  • If your question is about windows-specific stuff, you should indicate that with tags. – Zyx 2000 Dec 27 '12 at 12:38
  • Thanks, I am going to do that now. – James Dec 27 '12 at 12:40
  • What do you mean "to add multiple entries"? `\n` does not make it multiple, it is still single value with `\n` char inside. You are updating 16 strings at once, your `UpdateResource` argument does not reflect that and is invalid. – Roman R. Dec 27 '12 at 12:50
  • I am adding a STRINGTABLE to the binaries resource data. Inside that one STRINGTABLE I have only inserted ONE string, but I want to insert MORE than one string into the STRINGTABLE. Sorry about the confusion :) – James Dec 27 '12 at 12:54
  • 1
    A single `UpdateResource` call updates 16 strings at once. And your call provides only `dwUnicodeCharCount` payload characters for a single string element - which is incorrect. – Roman R. Dec 27 '12 at 13:03
  • oh okay, how would I go about fixing this ? Thanks alot for the help by the way. – James Dec 27 '12 at 13:08

0 Answers0