开发者

How can I load a string based on resource identifier?

I'm reading an entry from the registry that comes out something like:

@%SystemRoot%\\System32\\wscsvc.dll,-200

I need to ac开发者_StackOverflow社区tually load the string from the file.

I found an article which describes how the number on the end behaves (negative == specific resource ID, positive == the nth resource in the file), but I'm confused as to how one might load the resource. The ExtractIcon function seems to do the resource loading I need, but it returns an HICON, not a string.

How might I load the string from the file?


Load the DLL with LoadLibrary, load the string with LoadString, and then unload the DLL (assuming you don't need anything else from it) with FreeLibrary:

HMODULE hDll = LoadLibrary("C:\\WINDOWS\\System32\\wscsvc.dll");
if(hDll != NULL)
{
    wchar_t *str;
    if(LoadStringW(hDll, +200, (LPWSTR)&str, 0) > 0)
        ;  // success!  str now contains a (read-only) pointer to the desired string
    else
        ;  // handle error
    FreeLibrary(hDll);
}
else
    ;  // handle error

Note that LoadLibrary (and pretty much any other function that takes in a filename) does not understand environment variables like %SystemRoot%. You'll have to use a function such as ExpandEnvironmentStrings to expand the environment variables in the DLL filename before passing it to LoadLibrary.


This kind of string is called an "indirect string". The easiest way to get one is to call the SHLoadIndirectString function that's meant exactly for that.

Extracts a specified text resource when given that resource in the form of an indirect string (a string that begins with the '@' symbol).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜