2

这是我第一次尝试使用资源文件。我已经看到很多适用于 C# 但不适用于 C 的答案。有什么建议吗?

4

1 回答 1

5

假设您的意思是 Sysinternals 和其他人用来在程序的资源部分(例如 Sysinternals 的 Process Explorer)中携带驱动程序或所需 DLL(甚至程序本身的 x64 版本)的方法,您可以使用 Microsoft Visual C这段代码:

BOOL ExtractResTo(HINSTANCE Instance, LPCTSTR BinResName, LPCTSTR NewPath, LPCTSTR ResType)
{
    BOOL bResult = FALSE;
    HRSRC hRsrc;
    if(hRsrc = FindResource(HMODULE(Instance), BinResName, ResType))
    {
        HGLOBAL hGlob
        if(HGLOBAL hGlob = LoadResource(Instance, hRsrc))
        {
            DWORD dwResSize = SizeofResource(Instance, hRsrc);
            HANDLE hFileWrite = CreateFile(NewPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0);
            if(hFileWrite != INVALID_HANDLE_VALUE)
            __try
            {
                DWORD dwSizeWritten = 0;
                bResult = (WriteFile(hFileWrite, LockResource(hGlob), dwResSize, &dwSizeWritten, NULL) && (dwSizeWritten == dwResSize));
            }
            __finally
            {
                CloseHandle(hFileWrite);
            }
        }
    }
    return bResult;
}

这会将BinResName资源类型 ( ) 的给定资源 ( ResType) 从模块(例如 DLL)保存Instance到文件NewPath。显然,如果您的 C 不理解__try__finally您将不得不相应地调整代码。

取自这里(in SIDT.rar) 并针对 C 进行了调整。根据网站,代码在自由 BSD 许可下。

现在,如果您想获取指向数据 ( ppRes) 及其大小 ( pwdResSize) 的指针:

BOOL GetResourcePointer(HINSTANCE Instance, LPCTSTR ResName, LPCTSTR ResType, LPVOID* ppRes, DWORD* pdwResSize)
{
    // Check the pointers to which we want to write
    if(ppRes && pdwResSize)
    {
        HRSRC hRsrc;
        // Find the resource ResName of type ResType in the DLL/EXE described by Instance
        if(hRsrc = FindResource((HMODULE)Instance, ResName, ResType))
        {
            HGLOBAL hGlob;
            // Make sure it's in memory ...
            if(hGlob = LoadResource(Instance, hRsrc))
            {
                // Now lock it to get a pointer
                *ppRes = LockResource(hGlob);
                // Also retrieve the size of the resource
                *pdwResSize = SizeofResource(Instance, hRsrc);
                // Return TRUE only if both succeeded
                return (*ppRes && *pdwResSize);
            }
        }
    }
    // Failure means don't use the values in *ppRes and *pdwResSize
    return FALSE;
}

像这样调用:

LPVOID pResource;
DWORD pResourceSize;
if(GetResourcePointer(hInstance, _T("somename"), _T("sometype"), &pResource, &pResourceSize))
{
    // use pResource and pResourceSize
    // e.g. store into a string buffer or whatever you want to do with it ...
}

请注意,这也适用于具有整数 ID 的资源。您可以使用宏来检测这些IS_INTRESOURCE

资源脚本,例如 myresources.rc,本身很简单:

#include <winnt.rh>
"somename" "sometype" "path\to\file\to\embed"

RCDATA而不是 sometype 是一个合理的选择

#include <winnt.rh> // for RCDATA to be known to rc.exe
"somename" RCDATA "path\to\file\to\embed"

...在这种情况下,您可以将上述调用调整为:

GetResourcePointer(hInstance, _T("somename"), RT_RCDATA, &pResource, &pResourceSize)

GetResourcePointer使用上面的完整示例。假设您有一个指针变量char* buf,并且您知道您的资源是实际文本,那么您需要确保它在用作字符串时以零结尾,仅此而已。

资源脚本:

#include <winnt.rh>

"test" RCDATA "Test.txt"

访问它的代码

char* buf = NULL;
LPVOID pResource;
DWORD pResourceSize;
if(GetResourcePointer(hInstance, _T("test"), RT_RCDATA, &pResource, &pResourceSize))
{
    if(buf = calloc(pResourceSize+1, sizeof(char)))
    {
        memcpy(buf, pResource, pResourceSize);
        // Now use buf
        free(buf);
    }
}

当然,除非您打算简单地链接一个.res通过将其传递给链接器命令行来工作的文件。

于 2012-06-04T22:30:01.697 回答