是否有操作系统功能将REFIID转换为有用的名称?

Mor*_*hai 5 c++ windows com windows-shell

没有手动编写函数,可以将一些已知的REFIID转换为名称,例如:

if (riid == IID_IUnknown) return "IUnknown";
if (riid == IID_IShellBrowser) return "IShellBrowser";
...
Run Code Online (Sandbox Code Playgroud)

是否有系统调用会为众所周知的(甚至所有)REFIID返回合理的调试字符串?

Mor*_*hai 6

谢谢你的回复.以下是我根据您的反馈提出的建议 - 非常感谢!

CString ToString(const GUID & guid)
{
    // could use StringFromIID() - but that requires managing an OLE string
    CString str;
    str.Format(_T("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"),
        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]);
    return str;
}

CString GetNameOf(REFIID riid)
{
    CString name(ToString(riid));
    try
    {
        // attempt to lookup the interface name from the registry
        RegistryKey::OpenKey(HKEY_CLASSES_ROOT, "Interface", KEY_READ).OpenSubKey("{"+name+"}", KEY_READ).GetDefaultValue(name);
    }
    catch (...)
    {
        // use simple string representation if no registry entry found
    }
    return name;
}
Run Code Online (Sandbox Code Playgroud)

  • 永远不要使用`catch(...)`.有许多不同的哲学原因,但是如果你想要一个务实的原因:通过"Designed for Vista"验证,你的应用程序需要永远不会捕获访问冲突异常.根据您的编译器设置,`catch(...)`就是这样. (3认同)
  • 您可以使用StringFromGUID2() - 它将很乐意接受堆栈分配的缓冲区,不需要OLE字符串. (2认同)