Jake M Jake M - 9 months ago 61
C++ Question

Using GUIDFromString requries including Shell32.dll: How do I do that

I am attempting to use the WinAPI function

but it requires some finagling to include it in my project.

According to the msdn documentation:

This function is not declared in a header or exported by name from a
.dll file. It must be loaded from Shell32.dll as ordinal 703 for
GUIDFromStringA and ordinal 704 for GUIDFromStringW.

It can also be accessed from Shlwapi.dll as ordinal 269 for
GUIDFromStringA and ordinal 270 for GUIDFromStringW.

I have never loaded a DLL before so I am not sure what I should do and I am unsure if loading the DLL is enough, do I also have to include an 'ordinal' with the number 703? Would anyone be able to provide any advice on what I need to do to use this function and even an example?

My attempt below does not work(I am using VC++ 2010 Express):

#pragma comment(lib, "shell32.lib") // if I am including the dll do I need to include the lib aswell?

// I've heard that the dll location differs across versions of windows
// Does anyone know of a Cross-Windows-Version way to include Shell32.dll no matter where it is? Maybe use a keyword like "%SYSTEM%/Shell32.dll"
HINSTANCE shell32DLL = LoadLibary("C:/System/Shell32.dll");

// Now do I include 'Ordinal 703' as described in msdn? And how do I do that?

Answer Source

If you read the documentation for GUIDFromString(), it says:

GUIDFromString is available through Windows XP with Service Pack 2 (SP2) or Windows Vista. It might be altered or unavailable in subsequent versions. Applications should use CLSIDFromString or IIDFromString in place of this function.

CLSIDFromString() and IIDFromString() are both exported by name from Ole32.dll, so you can use them like you would any other DLL function.

With that said, if you still want to use GUIDFromString() then use LoadLibrary() to load shell32.dll and then use GetProcAddress() to access the function. MSDN documentation demonstrates how to do that. To load a function by ordinal, you can use the MAKEINTRESOURCE() macro when calling GetProcAddress().

So, for example:

// MAKEINTRESOURCE() returns an LPTSTR, but GetProcAddress()
// expects LPSTR even in UNICODE, so using MAKEINTRESOURCEA()...
#ifdef UNICODE

BOOL myGUIDFromString(LPCTSTR psz, LPGUID pguid)
    BOOL bRet = FALSE;

    LPFN_GUIDFromString pGUIDFromString = NULL;

    HINSTANCE hInst = LoadLibrary(TEXT("shell32.dll"));
    if (hInst)
        pGUIDFromString = (LPFN_GUIDFromString) GetProcAddress(hInst, MAKEINTRESOURCEA_T(703, 704));
        if (pGUIDFromString)
            bRet = pGUIDFromString(psz, pguid);

    if (!pGUIDFromString)
        hInst = LoadLibrary(TEXT("Shlwapi.dll"));
        if (hInst)
            pGUIDFromString = (LPFN_GUIDFromString) GetProcAddress(hInst, MAKEINTRESOURCEA_T(269, 270));
            if (pGUIDFromString)
                bRet = pGUIDFromString(psz, pguid);

    return bRet;