2

因此,我尝试使用 C++ 从给定文件(在我的情况下是桌面上的 .msstyle)中读取资源类型和名称

但不知何故,resinfo 结果有点奇怪而且不准确。它没有写出实际发现的内容。例如,msstyle 给出的结果为:http ://pastebin.com/ZhnkPmUe

#include <windows.h>
#include <strsafe.h>
#include <stdio.h>

HANDLE g_hFile;
BOOL EnumTypesFunc(HMODULE hModule, LPTSTR lpType, LONG lParam);  
BOOL EnumNamesFunc(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam);  
BOOL EnumLangsFunc(HMODULE hModule, LPCTSTR lpType, LPCTSTR lpName, WORD wLang, LONG lParam);

void main(void)
{
    HMODULE hExe; 
    TCHAR szBuffer[80];
    DWORD cbWritten; 
    size_t cbString;
    HRESULT hResult;

// Load the .EXE whose resources you want to list.
    hExe = LoadLibrary(TEXT("C:\\Users\\Kala\\Desktop\\776.msstyles"));
    g_hFile = CreateFile(TEXT("C:\\Users\\Kala\\Desktop\\resinfo.txt"), GENERIC_READ | GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES) NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);  

// Find all of the loaded file's resources.
    hResult = StringCchPrintf(szBuffer, sizeof(szBuffer)/sizeof(TCHAR),TEXT("The file contains the following resources:\r\n\r\n"));
    hResult = StringCchLength(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), &cbString);

    WriteFile(g_hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL); 
//Calls the function to find types
    EnumResourceTypes(hExe, (ENUMRESTYPEPROC)EnumTypesFunc, 0);                             
// Unload the executable file whose resources were
    FreeLibrary(hExe);
    CloseHandle(g_hFile);
}
//    FUNCTION: EnumTypesFunc(HANDLE, LPSTR, LONG)
//
//    PURPOSE:  Resource type callback
BOOL EnumTypesFunc(HMODULE hModule,  LPTSTR lpType, LONG lParam)
{
    TCHAR szBuffer[80];  // print buffer for info file
    DWORD cbWritten;     // number of bytes written to resource info file
    size_t cbString;
    HRESULT hResult;

    // Write the resource type to a resource information file.
    // The type may be a string or an unsigned decimal
    // integer, so test before printing.
    if (!IS_INTRESOURCE(lpType))
    {
        hResult = StringCchPrintf(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), TEXT("Type: %s\r\n"), lpType);
    }
    else
    {
        hResult = StringCchPrintf(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), TEXT("Type: %u\r\n"), (USHORT)lpType);
    }

    hResult = StringCchLength(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), &cbString);
    WriteFile(g_hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL);
    // Find the names of all resources of type lpType.
    EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)EnumNamesFunc, 0);

    return TRUE;
}

//    FUNCTION: EnumNamesFunc(HANDLE, LPSTR, LPSTR, LONG)
//
//    PURPOSE:  Resource name callback
BOOL EnumNamesFunc(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
{
    TCHAR szBuffer[80];  // print buffer for info file
    DWORD cbWritten;     // number of bytes written to resource info file
    size_t cbString;
    HRESULT hResult;

    // Write the resource name to a resource information file.
    // The name may be a string or an unsigned decimal
    // integer, so test before printing.
    if (!IS_INTRESOURCE(lpName))
    {
        hResult = StringCchPrintf(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), TEXT("\tName: %s\r\n"), lpName);
    }
    else
    {
        hResult = StringCchPrintf(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), TEXT("\tName: %u\r\n"), (USHORT)lpName);
    }

    hResult = StringCchLength(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), &cbString);   
    WriteFile(g_hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL);
    return TRUE;
}

我想我一定错过了一些东西,因为我似乎没有得到我想要的合适的字符串,所以如果有人能指出我正确的方向,我将非常感激

4

1 回答 1

0

您的文件采用 UTF-16 编码,因为您使用的是 Unicode 版本的 Win32 API。您的文本编辑器将文件解释为 8 位编码。因此,您只需让您的文本编辑器将文件解释为 UTF-16。可能最简单的方法是将 UTF-16LE BOM 放在文件的开头。

顺便说一句,我建议您停止编码以支持 MBCS 字符集,停止使用TCHAR等等。只需假设您将针对 Win32 API 的 Unicode 版本编写程序。L"..."如果您编写而不是TEXT("...")等等,它将使您的代码更容易阅读。当然,如果您需要支持 Windows 9x,那就忘记我说过的,继续编写可以在 MBCS 和 Unicode 模式下编译的代码。

于 2013-01-05T23:58:28.647 回答