c++ RegCreateKeyEx LPCWSTR from std::string

427 views Asked by At

I'm writting few operations in registry and I got stuck for two days at this. Really don't know how to solve that. So, its my code:

 HKEY hkey;
 DWORD dwDisposition;
 string address = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
 QString qAddr= ui->networkCard->itemData(ui->networkCard->currentIndex()).toString();
 address += qAddr.toStdString();
 string sAddr = qAddr.toStdString();
cout << address; // here is the value I want to proceed as 2nd arg in RegCreateKeyEx
size_t size = address.size();
wchar_t szBuff[size];
swprintf(szBuff, size, L"%s", address.c_str());
cout << szBuff << endl; // but after conversion I get some hex data like 0x28d172 :(

if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, szBuff, 0, NULL, 0, KEY_WRITE, NULL, &hkey, &dwDisposition) == ERROR_SUCCESS){
  DWORD dwType, dwSize;
  dwType = REG_DWORD;
  ....

RegCreateKeyEx requires a LPCWSTR arg, but I really don't know how to do it from a std::string. Can u help me fixing this? :) Thank you!

1

There are 1 answers

2
ScottMcP-MVP On

RegCreateKeyEx requires LPCWSTR only if your project is set to unicode. If you want a unicode project then use std::wstring instead of std::string. If you don't want a unicode project then change the project Character Set setting to Multi-Byte Character Set, which will let you use std::string.