0

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!

Paul R
  • 208,748
  • 37
  • 389
  • 560
pablo7890
  • 65
  • 9
  • Replace `L"%s"` format with `L"%S"`. Or use CA2W macro. – Alex F Sep 11 '13 at 15:44
  • Thank you, but it doesn't work at all for me.. – pablo7890 Sep 11 '13 at 16:14
  • OK, just replace RegCreateKeyEx with RegCreateKeyExA, and change szBuff type to char. And if something "doesn't work for you", provide some details. – Alex F Sep 12 '13 at 05:50
  • Thank you so much, now it's working pretty good :) But now I'm stuck at new problem, how to write REG_MULTI_SZ values from std:string? Heh, never without problems.. – pablo7890 Sep 14 '13 at 21:06

1 Answers1

0

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.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • I read about it but I don't know where to change it. I'm not using VS but QT Designer, and here I don't have these options :/ – pablo7890 Sep 11 '13 at 16:06
  • Maybe the best way will be making DLL in VS and then import it in main project in qt designer? I hope not.. – pablo7890 Sep 11 '13 at 16:23