使用GUIDFromString请求包括Shell32.dll:我该怎么做

时间:2013-01-28 04:46:56

标签: c++ winapi dll

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

根据msdn文档:

  

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

     

它也可以从Shlwapi.dll作为序号269访问   GUIDFromStringA和GUIDFromStringW的序号270。

我之前从未加载过DLL,因此我不确定应该做什么,我不确定加载DLL是否足够,我是否还必须包含数字为703的'序数'?是否有人能够提供有关使用此功能需要做什么的建议甚至是一个例子?

我在下面的尝试不起作用(我正在使用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?

3 个答案:

答案 0 :(得分:5)

如果GUIDFromString()GUIDFromString(),则表示:

  

GUIDFromString可通过Windows XP Service Pack 2(SP2)或Windows Vista获得。在后续版本中可能会更改或不可用。应用程序应使用CLSIDFromString或IIDFromString代替此函数。

read the documentationCLSIDFromString()都是从Ole32.dll按名称导出的,因此您可以像使用任何其他DLL函数一样使用它们。

话虽如此,如果您仍想使用GetProcAddress(),请使用IIDFromString()加载shell32.dll,然后使用LoadLibrary()访问该功能。 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; } 时使用MSDN documentation宏。

所以,例如:

{{1}}

答案 1 :(得分:1)

这会出现错误“语法错误”('“:

typedef BOOL WINAPI (*LPFN_GUIDFromString)(LPCTSTR, LPGUID);

正确的版本是:

typedef BOOL (WINAPI *LPFN_GUIDFromString)(LPCTSTR, LPGUID);

答案 2 :(得分:1)

将以下行保存为SHLWAPIX.DEF:

LIBRARY SHLWAPI
VERSION 6.0
EXPORTS
 GUIDFromStringA @269
 GUIDFromStringW @270

将以下行保存为SHLWAPIX.C:

// https://msdn.microsoft.com/en-us/library/bb776431.aspx

__declspec(dllexport)
int __stdcall GUIDFromStringA(void *_1, void *_2)
{ return 0; }

__declspec(dllexport)
int __stdcall GUIDFromStringW(void *_1, void *_2)
{ return 0; }

运行 CL.EXE /LD /Zl SHLWAPIX.C /link /DEF:SHLWAPIX.DEF /NOENTRY 创建导入库SHLWAPIX.LIB,然后删除SHLWAPIX.OBJ,SHLWAPIX.EXP和SHLWAPIX.DLL

将以下行保存为SHLWAPIX.H:

#pragma once
#pragma comment(linker, "/DEFAULTLIB:SHLWAPIX.LIB")

__declspec(dllimport)
BOOL WINAPI GUIDFromStringA(LPCSTR psz, LPGUID pguid);

__declspec(dllimport)
BOOL WINAPI GUIDFromStringW(LPCWSTR psz, LPGUID pguid);

最后将以下行保存为SHLWAPIX.C:

#pragma comment(lib, "SHLWAPIX.LIB")
#pragma comment(lib, "USER32.LIB")

#pragma comment(linker, "/ENTRY:wWinMainCRTStartup")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS,5.0")
#pragma comment(linker, "/VERSION:1.0")

#define STRICT
#define UNICODE
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "shlwapix.h"

VOID wWinMainCRTStartup()
{
    GUID guid = {0};
    WCHAR szBuffer[1025] = L"";

    if (GUIDFromStringA("{00112233-4455-6677-8899-AABBCCDDEEFF}", &guid))
        if (wsprintf(szBuffer, L"GUID = {%08lX-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}\n",`
                     guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]) > 0)
            MessageBox((HWND) NULL, szBuffer, L"GUIDFromStringA()", MB_OK);

    if (GUIDFromStringW(L"{FFEEDDCC-BBAA-9988-7766-554433221100}", &guid))
        if (wsprintf(szBuffer, L"GUID = {%08lX-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}\n",
                     guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]) > 0)
            MessageBox((HWND) NULL, szBuffer, L"GUIDFromStringW()", MB_OK);
}

最后运行CL.EXE /GS- SHLWAPIX.C创建SHLWAPIX.EXE,然后运行后者。