How do I convert a LPWSTR to a GUID?

橙三吉。 提交于 2019-12-10 15:23:34

问题


I'm working with the Windows 7 audio APIs, and I've hit a wall.

Basically, I need to take an IAudioSessionControl2* and get an ISimpleAudioVolume* out of it.

Now, it looks like I can call on IAudioSessionManager->GetSimpleAudioVolume() using the value of IAudioSessionControl2->GetSessionInstanceIdentifier(...). 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. Through debugging I've confirmed that the return'd value from GetSessionInstanceIdentifier() at least looks like a GUID.

So, the actual question is how would I convert the LPWSTR I've got into a 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.


Ok, these APIs definitely do not work the way I say they do in the above text dump. However, the basic question of String -> GUID is answered so I'm not going to delete the question.


回答1:


Try CLSIDFromString. A CLSID is actually defined as:

typedef GUID CLSID;

therefore you can use CLSIDFromString to generate a 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. 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. Instead you can use IIDFromString in the exact same way.




回答2:


CLSIDFromString documents now point you to StringFromGUID2(), which is easier to use... and doesn't think the thing is a CLSID.



来源:https://stackoverflow.com/questions/1128188/how-do-i-convert-a-lpwstr-to-a-guid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!