Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a LPWSTR to a GUID?

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.

like image 759
Kevin Montrose Avatar asked Dec 29 '22 19:12

Kevin Montrose


1 Answers

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.

like image 102
Jared Oberhaus Avatar answered Jan 24 '23 18:01

Jared Oberhaus