Using UpdateResource() to change a string resource

  • Thread starter Thread starter Yan Yang
  • Start date Start date
Y

Yan Yang

Guest
I am trying to change the string associated to a resouce ID using UpdateResouce(), but the string doesn't change with the code below.

void CMainFrame::UpdateMacroFileName(UINT nID, LPCTSTR szNewName)
{
TCHAR path[_MAX_PATH];
GetModuleFileName(NULL, path, sizeof(path));

HANDLE hRes = BeginUpdateResource(path, FALSE);

CString sNewString = szNewName;
int iCharCount = sNewString.GetLength() + 1;
LPWSTR pUnicodeString = new WCHAR[iCharCount];

wcscpy_s(pUnicodeString, wcslen(pUnicodeString), sNewString);

UpdateResource(hRes, RT_STRING, MAKEINTRESOURCE(nID), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK), (LPVOID*)pUnicodeString, (DWORD)wcslen(pUnicodeString) * sizeof(WCHAR));

EndUpdateResource(hRes, FALSE);

//delete[] pUnicodeString;

// Check if string is updated
LPCTSTR szUpdated = MAKEINTRESOURCE(nID);
CString strUpdated = szUpdated;
}

Could you please let me know any mistake in the code above? By the way I don't know why it crashes at delete[] pUnicodeString.

Thanks in advance.

Continue reading...
 
Back
Top