使用包括Shell32.dll在内的GUIDFromString需求:我该怎么做 [英] Using GUIDFromString requries including Shell32.dll: How do I do that

查看:186
本文介绍了使用包括Shell32.dll在内的GUIDFromString需求:我该怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WinAPI函数 GUIDFromString(),但需要一些费力才能将其包含在我的项目中。

I am attempting to use the WinAPI function GUIDFromString() but it requires some finagling to include it in my project.

根据 msdn 文档:


此函数未在标题中声明或导出通过
.dll文件的名称。它必须从Shell32.dll加载为
GUIDFromStringA的序号703,GUIDFromStringW的序号704。

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.

它也可以从Shlwapi.dll作为序号269对于
GUIDFromStringA和GUIDFromStringW的序数270。

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

我从来没有加载过DLL,所以我不知道我应该做和我不确定如果加载DLL是足够的,我还必须包括一个ordinal与数字703?任何人都可以提供任何建议,我需要做什么来使用这个功能,甚至一个例子?

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?

我下面的尝试不起作用(我使用VC ++ 2010 Express):

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?


推荐答案

如果您阅读 GUIDFromString()的文档 ,它说:

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


GUIDFromString可以通过带有Service Pack 2(SP2)或Windows Vista的Windows XP。在随后的版本中可能会更改或不可用。应用程序应使用CLSIDFromString或IIDFromString代替此功能。

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() IIDFromString() 都由...导出从Ole32.dll的名称,所以你可以像任何其他DLL函数一样使用它们。

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

如果你仍然想使用 GUIDFromString()然后使用 LoadLibrary() 加载shell32.dll,然后使用 GetProcAddress() 访问该函数。 MSDN文档演示了如何做到这一点。要按顺序加载函数,可以使用 GetProcAddress()的MAKEINTRESOURCE() 宏。

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

所以,例如:

// MAKEINTRESOURCE() returns an LPTSTR, but GetProcAddress()
// expects LPSTR even in UNICODE, so using MAKEINTRESOURCEA()...
#ifdef UNICODE
#define MAKEINTRESOURCEA_T(a, u) MAKEINTRESOURCEA(u)
#else
#define MAKEINTRESOURCEA_T(a, u) MAKEINTRESOURCEA(a)
#endif

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

    typedef BOOL (WINAPI *LPFN_GUIDFromString)(LPCTSTR, LPGUID);
    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);
        FreeLibrary(hInst);
    }

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

    return bRet;
}

这篇关于使用包括Shell32.dll在内的GUIDFromString需求:我该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆