简体   繁体   中英

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 actually 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM