将std :: string转换为const BYTE * for RegSetValueEx() [英] convert std::string to const BYTE* for RegSetValueEx()

查看:784
本文介绍了将std :: string转换为const BYTE * for RegSetValueEx()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,获取一个std :: string。该函数调用

I have a function that gets a std::string. That function calls

RegSetValueEx

第五个参数是注册表值的值,并且期望一个类型为const BYTE *的变量。
所以我必须将std :: string转换为const BYTE *,并将结果数组的长度作为第6个参数。

the 5th parameter is the value of the registry value and expects a variable of type const BYTE*. So I have to convert the std::string to const BYTE* and also give the length of the resulting array as the 6th parameter.

一种方式做到这一点,但它感觉丑陋,我不真正明白发生了什么。下面是这个函数的简化版本:

I have found a way to do it, but it feels ugly and I don't really understand what is going on. Here is a slimmed down version of that function:

void function(const std::string& newValue)
{
    HKEY keyHandle;
    if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("some key"),0,KEY_ALL_ACCESS,&keyHandle) == ERROR_SUCCESS)
    {
    	std::wstring wNewValue;
    	wNewValue.assign(newValue.begin(),newValue.end());

    	if (RegSetValueEx(keyHandle, TEXT("some value"), NULL, REG_SZ, (const BYTE*)(LPCTSTR)(wNewValue.c_str()), wNewValue.size()*2)==ERROR_SUCCESS)
    	{
    		//do something
    	}
    	RegCloseKey(keyHandle);
    }
}

正如你所看到的, (UNICODE被定义),然后使用双转型,长度我必须做* 2,否则它将只设置输入字符串的一半。

As you can see, i first make a wide string (UNICODE is defined), then use a double cast, and for the length i have to do *2, else it will only set half of the input string.

这种形式的正常/最好的方式来做呢?

Is this form of cast the normal/best way to do it?

为什么* 2,什么会是更好的方法?

Why the * 2, what would be a better way?

推荐答案

void function(const std::string& newValue)
{
    HKEY keyHandle;
    if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("some key"),0,KEY_ALL_ACCESS,&keyHandle) == ERROR_SUCCESS)
    {

        if (RegSetValueExA(keyHandle, "some value", NULL, REG_SZ, (const BYTE*)newValue.c_str(), newValue.size() + 1)==ERROR_SUCCESS)
        {
                //do something
        }
        RegCloseKey(keyHandle);
    }
}

我删除了将字符串转换为

I removed the part where you convert your string to a wstring, instead you'll be using the ANSI version of RegSetValueEx explicitly.

引用来自RegSetValueEx在MSDN中的注释:

quote from RegSetValueEx remarks in MSDN:

如果dwType是REG_SZ,REG_MULTI_SZ,
或REG_EXPAND_SZ类型,并且此函数的ANSI
版本使用
(通过显式调用
RegSetValueExA或通过在包含Windows.h
文件之前不定义
UNICODE),
指向的数据必须是ANSI
字符串。

If dwType is the REG_SZ, REG_MULTI_SZ, or REG_EXPAND_SZ type and the ANSI version of this function is used (either by explicitly calling RegSetValueExA or by not defining UNICODE before including the Windows.h file), the data pointed to by the lpData parameter must be an ANSI character string. The string is converted to Unicode before it is stored in the registry.

还要注意,cbData参数应包括空终止的大小aswell。

Also note that the cbData parameter should include the size of the null termination aswell.

这篇关于将std :: string转换为const BYTE * for RegSetValueEx()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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