c++ - 将文件系统路径转换为 ​​const BYTE* 的问题

标签 c++ winapi filesystems c++17 registry

我想要实现的是使用注册表在 Windows 启动时执行我的程序。当我尝试放置文件位置时,编译器提示它无法从 filesystem::path 转换至const BYTE* .我不知道如何解决这个问题,因为我是 C++ 的初学者。我提供了以下代码:

HKEY newValue;
RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", &newValue);
RegSetValueEx(newValue, "myprogram", 0, REG_SZ, fs::temp_directory_path().append(filename), sizeof("tes3t")); // This line is the issue. fs::temp_directory_path().append(filename)
RegCloseKey(newValue);
return 0;

EXCEPTION: No suitable conversion function from "std:filesystem::path" to "const BYTE *" exists

最佳答案

根据 RegSetValueExA() documentation , 该函数不接受 std::filesystem::path目的。这就是错误消息所提示的。

LSTATUS RegSetValueExA(
  HKEY       hKey,
  LPCSTR     lpValueName,
  DWORD      Reserved,
  DWORD      dwType,
  const BYTE *lpData, // <-- here
  DWORD      cbData
);
第 5 个参数采用 const BYTE*指向 的指针以 null 结尾的 C 样式字符串 .第 6 个参数取字符串中的字符数,包括空终止符:

lpData

The data to be stored.

For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters.

Note lpData indicating a null value is valid, however, if this is the case, cbData must be set to '0'.

cbData

The size of the information pointed to by the lpData parameter, in bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters.

std::filesystem::path没有到 const BYTE* 的隐式转换,因此编译器错误。您需要显式转换 pathstd::stringstd::wstring首先(更喜欢后者,因为注册表在内部将字符串存储为 Unicode),然后您可以将该字符串值保存到注册表,例如:
// using std::string...

HKEY newValue;

// don't use RegOpenKey()! It is provided only for backwards compatibility with 16bit apps...
if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &newValue) == 0)
{
    // this may lose data for non-ASCII characters!
    std::string s = fs::temp_directory_path().append(filename).string();

    // this will convert the ANSI string to Unicode for you...
    RegSetValueExA(newValue, "myprogram", 0, REG_SZ, reinterpret_cast<LPCBYTE>(s.c_str()), s.size()+1);

    RegCloseKey(newValue);
}

return 0;
// using std::wstring...

HKEY newValue;

// don't use RegOpenKey()! It is provided only for backwards compatibility with 16bit apps...
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &newValue) == 0)
{
    // no data loss here!
    std::wstring s = fs::temp_directory_path().append(filename).wstring();

    // no ANSI->Unicode conversion is performed here...
    RegSetValueExW(newValue, L"myprogram", 0, REG_SZ, reinterpret_cast<LPCBYTE>(s.c_str()), (s.size()+1) * sizeof(WCHAR));

    RegCloseKey(newValue);
}

return 0;

关于c++ - 将文件系统路径转换为 ​​const BYTE* 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64652565/

相关文章:

c++ - 我的 qmake 构建在调试构建中的速度是 cmake 的两倍多,为什么?

c# - 在 C# 中无限/定期执行代码的最佳实践

python - 将元数据/标识符数据添加到 CSV 文件?

Linux -> 自动删除超过 2 天的文件

c++ - 如何在 C++ 中使用列表初始化程序解决大括号省略的歧义?

c++ - string.length() 造成麻烦

c++ - 蹩脚的mp3改变比特率cpp

C++ ListView 隐藏项

c++ - TCHAR 和 WCHAR 有什么区别?

windows - 如何在 Python/Go/C/Batch 中获取 Windows 中的扩展文件属性?