如何将LPWSTR转换为GUID? [英] How do I convert a LPWSTR to a GUID?

查看:168
本文介绍了如何将LPWSTR转换为GUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 7音频API,我打了墙。

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

基本上,我需要一个 IAudioSessionControl2 *并获取 ISimpleAudioVolume *。

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

现在,它似乎可以调用 IAudioSessionManager- > GetSimpleAudioVolume(),使用 IAudioSessionControl2的值 - > GetSessionInstanceIdentifier(...)。注意,这不是在文档中完全拼写,但它似乎是一个合理的行为。

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.

问题,GetSimpleAudioVolume()需要一个GUID *和GetSessionInstanceIdentifier ()发出一个LPWSTR。通过调试,我已经确认来自GetSessionInstanceIdentifier()的返回值至少看起来像一个GUID

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.

是我如何将LPWSTR转换成GUID?我意识到这是非常微不足道,如果我整理成一些托管代码,并使用内置的 GUID ,但是必须有一个C ++的方式来执行此操作。

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.

推荐答案

尝试

typedef GUID CLSID;

因此,您可以使用 CLSIDFromString GUID。下面是一些示例代码:

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...
    ...
}



警告



不是GUID的事件将作为有效的GUID返回。例如:

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.

短版本:不要使用CLSIDFromString。而是可以使用 IIDFromString

Short version: Do not use CLSIDFromString. Instead you can use IIDFromString in the exact same way.

这篇关于如何将LPWSTR转换为GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆