c++ - EnumResourceNames 函数 C++

标签 c++ winapi

所以我尝试使用 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;
}

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

最佳答案

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

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

关于c++ - EnumResourceNames 函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14177286/

相关文章:

c++ - 如何在 Qt Eclipse 中获取 slider 位置的值?

c++ - 结构或类中的数组初始化

c++ - 为什么第一个程序不起作用但第二个程序有效?第二,为什么输出是它给出的?

C++ If then else 条件被跳过

windows - Windows API 函数声明中的 "CALLBACK"是什么意思?

c++ - exe导入dll序号与DLL导出函数名比较?

c++ - RE2 语法 : skip matching if don't share the same prefix

c++ - CallWindowProc 在退出时崩溃

c++ - 如何自定义打开/保存文件对话框?

c++ - 获取卷列表总是说 C :\