简体   繁体   English

如何将LPWSTR转换为GUID?

[英]How do I convert a LPWSTR to a GUID?

I'm working with the Windows 7 audio APIs, and I've hit a wall. 我正在使用Windows 7音频API,而且我已经碰壁了。

Basically, I need to take an IAudioSessionControl2 * and get an ISimpleAudioVolume * out of it. 基本上,我需要一个IAudioSessionControl2 *并从中获取一个ISimpleAudioVolume *。

Now, it looks like I can call on IAudioSessionManager->GetSimpleAudioVolume() using the value of IAudioSessionControl2->GetSessionInstanceIdentifier(...) . 现在,看起来我可以使用IAudioSessionControl2-> GetSessionInstanceIdentifier(...)的值来调用IAudioSessionManager-> GetSimpleAudioVolume( Note that this isn't exactly spelled out as such in the docs, but it seems like a reasonable behavior. 请注意,这在文档中并未完全拼写出来,但它似乎是一种合理的行为。

The problem, GetSimpleAudioVolume() takes a GUID* and GetSessionInstanceIdentifier() spits out a LPWSTR. 问题,GetSimpleAudioVolume()接受GUID *和GetSessionInstanceIdentifier()吐出LPWSTR。 Through debugging I've confirmed that the return'd value from GetSessionInstanceIdentifier() at least looks like a GUID. 通过调试我已经确认GetSessionInstanceIdentifier()的返回值至少看起来像GUID。

So, the actual question is how would I convert the LPWSTR I've got into a GUID? 那么,实际的问题是我如何将LPWSTR转换为GUID? I realise this is pretty trivial if I marshal across into some managed code and use built-in GUID , but there's got to be a C++ way of doing this. 我意识到如果我编组一些托管代码并使用内置的GUID ,这是非常简单的,但是必须采用C ++方式来实现这一点。


Ok, these APIs definitely do not work the way I say they do in the above text dump. 好吧,这些API肯定不像我在上面的文本转储中所说的那样工作。 However, the basic question of String -> GUID is answered so I'm not going to delete the question. 但是,String - > GUID的基本问题已经回答了,所以我不打算删除这个问题。

Try CLSIDFromString . 尝试CLSIDFromString A CLSID is actually defined as: CLSID实际上定义为:

typedef GUID CLSID;

therefore you can use CLSIDFromString to generate a GUID. 因此,您可以使用CLSIDFromString生成GUID。 Here's some sample code: 这是一些示例代码:

LPWSTR guidstr;
GUID guid;

...

HRESULT hr = CLSIDFromString(guidstr, (LPCLSID)&guid);
if (hr != S_OK) {
    // bad GUID string...
    ...
}

Warning 警告

Things that are not GUIDs will return as valid GUIDs. 不是GUID的东西将作为有效GUID返回。 For example: 例如:

| String              | Returned Clsid                         |
|---------------------|----------------------------------------|
| "file"              | {00000303-0000-0000-C000-000000000046} | FileMoniker
| "AccessControlList" | {b85ea052-9bdd-11d0-852c-00c04fd8d503} |
| "ADODB.Record"      | {00000560-0000-0010-8000-00AA006D2EA4} |
| "m"                 | {4ED063C9-4A0B-4B44-A9DC-23AFF424A0D3} | Toolbar.MySearchDial

This means that in addition to returning results you do not expect, the function hits the registry every time it is run. 这意味着除了返回您不期望的结果之外,该函数每次运行时都会命中注册表。

Short version: Do not use CLSIDFromString. 简短版本:不要使用CLSIDFromString。 Instead you can use IIDFromString in the exact same way. 相反,您可以以完全相同的方式使用IIDFromString

CLSIDFromString文档现在指向StringFromGUID2(),它更易于使用......并且不认为该东西是CLSID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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