所以,我在 C++ 中将字符串转换为字节,但是当它将它添加到注册表中时,它剥离了 exe 部分但保留了 .,我不知道它有什么问题。
如果您想知道 NXS 是什么,它的值是“noerrorsplease.exe”,类型是 char。
char szFinal[] = "";
strcat(szFinal, (const char *)ExtractDirectory(filepath).c_str());
//Not needed: strcat(szFinal, "");
strcat(szFinal, nxs);
strcat(szFinal, ".exe");
CString str;
str = szFinal;
str += ".exe";
cout << str.GetString() << endl;
const BYTE* pb = reinterpret_cast<const BYTE*>(str.GetString());
cout << pb << endl;
DWORD pathLenInBytes = *szFinal * sizeof(*szFinal);
if(RegSetValueEx(newValue, TEXT("Printing Device"), 0, REG_SZ, (LPBYTE)pb, pathLenInBytes) != ERROR_SUCCESS)
{
RegCloseKey(newValue);
cout << "error" << endl;
}
cout << "Possibly worked." << endl;
RegCloseKey(newValue);
最佳答案
这段代码
char szFinal[] = "";
strcat(szFinal, (const char *)ExtractDirectory(filepath).c_str());
已经无效。您定义的数组 szFina 只有一个字符作为终止零。您不能使用它来复制任何字符串。在这些情况下,您应该使用 std::string 类型的对象。
关于C++ 字符串到字节错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19884488/