如何使用C++在注册表中插入变量值
How to insert variables values in the registry with C++?
可能重复:
字符数组初始化时出错
我想在windows注册表中插入一个环境变量,所以我绑定了以下C++代码:
string appDataPath = getenv("appdata");
HKEY hkey;
char value[] = appDataPath.c_str();
RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\Currentversion\Run", 0, KEY_SET_VALUE, &hkey);
RegSetValueEx (hkey, "MyProgram", 0, REG_SZ, (LPBYTE) value, strlen(value) + 1);
RegCloseKey(hkey);
代码块调试输出告诉我:error: initializer fails to determine size of 'value'
我认为这是因为编译器在编译之前需要知道我的变量的大小,但我不知道如何解决这个问题。。。
谢谢!
c_str()
返回const char*
,而不是char[]
。更改
char value[] = appDataPath.c_str();
至
const char* value = appDataPath.c_str();
编译器给出错误是因为数组变量需要一个长度,但没有提供。
使用
const char * value = appDataPath.c_str();
(读取<string>引用以查找c_str()
的返回类型,它会告诉您它确实是const char *
。(
关于如何连接两个字符串的问题:
使用C++字符串而不是char *
执行此操作,并稍后转换它们:
string newstring = appDataPath;
newstring.append("some text");
const char * value = newstring.c_str();
相关文章:
- 使用变量值作为 PlaySound 中的路径
- 通过指针偏移量访问结构变量值
- 变量值在 C++ 中的 glutIdleFunc() 中未递增
- 更新 #ifdef 中的现有变量值?
- 尝试更改 main 函数内的全局变量值时出现编译错误 C++.
- 通过 Rcpp 和 bit64 R 包将最大的int64_t变量值从 C++ 传递到 R
- 如何使用 IRBuilder 更新 LLVM IR 中的全局变量值?
- 更改已在运行的程序的变量值
- 在 Xcode 中使用后对外部变量值进行初始化
- C++整数变量值
- 变量值,在三元运算符之后
- 如何获得对不同类的引用,以便从所述类内部获得变量值
- Java应用程序的JNI-c++变量值
- 如何固定从传感器获得的变量值
- 离开方法时重置变量值
- 打印具有通用特定变量值的类对象
- 在宏函数C++中设置宏变量值
- 为什么每次迭代都会获得相同的变量值? C++
- 如何根据基类对象中的变量值分配派生类中的变量?
- 如何使用C++在注册表中插入变量值