c++ - Why this code runs perfectly in visual studio with multibyte characher set but not with unicode char set? -
when run code on g++ , runs smoothly, when run code on visual studio wiith unicode char set option, doesn't print product id. can explain me how fix problem , why happens?
#include <windows.h> #include <stdio.h> #include <iostream> using namespace std; wchar_t* getregistrykeyvalue(const char* regkey, const char* ppidname) { hkey registry; long returnstatus; dword regtype = 0; dword regsize = 0; char* ppid = 0; returnstatus = regopenkeyex(hkey_local_machine, regkey, 0, key_query_value | key_wow64_64key, ®istry); if (returnstatus == error_success) { returnstatus = regqueryvalueex(registry, ppidname, 0, ®type, 0, ®size); ppid = new char[regsize]; /* value. */ returnstatus = regqueryvalueex(registry, ppidname, 0, ®type, (lpbyte)ppid, ®size); regclosekey(registry); if (ppid[regsize] > 127 || ppid[regsize] < 32) { ppid[regsize] = '\0'; } if (regsize > 1) { int s = 0; int i=0; while (ppid[i] != null) { s++; i++; } const size_t csize = s ; wchar_t* wc = new wchar_t[csize]; mbstowcs(wc, ppid, csize); return wc; } else { printf("size not > 1 (%d)\n", regsize); return null; } } else { regclosekey(registry); return null; } } int main() { wchar_t * resultdata=null; resultdata = getregistrykeyvalue("software\\microsoft\\windows nt\\currentversion", "productid"); wcout << resultdata; cout << endl; delete resultdata; system("pause"); return 0; }
you using tchar
-based apis rely on char*
data when compiling mbcs , wchar_t*
data when compiling unicode. not taking difference account correctly. code should not compile when set unicode, because passing ansi parameters unicode functions. since want return unicode string, should using unicode api functions begin with.
there other logic errors in code, such leaking memory, not allocating right number of bytes wc
buffer, insufficient error handling, etc.
try more instead:
#include <windows.h> #include <stdio.h> #include <iostream> using namespace std; wchar_t* getregistrykeyvalue(const wchar_t* regkey, const wchar_t* pvaluename) { long returnstatus; dword regtype = 0; dword regsize = 0; dword dwflags = rrf_rt_reg_sz | rrf_rt_reg_multi_sz | rrf_rt_reg_expand_sz | rrf_subkey_wow6464key; wchar_t* ws = 0; returnstatus = reggetvaluew(hkey_local_machine, regkey, pvaluename, dwflags, ®type, 0, ®size); if (returnstatus == error_success) { ws = new wchar_t[regsize / sizeof(wchar)]; returnstatus = reggetvaluew(hkey_local_machine, regkey, pvaluename, dwflags, ®type, ws, ®size); if (returnstatus != error_success) { delete[] ws; ws = null; } } return ws; } int main() { wchar_t* resultdata = getregistrykeyvalue(l"software\\microsoft\\windows nt\\currentversion", l"productid"); wcout << resultdata << endl; delete[] resultdata; system("pause"); return 0; }
alternatively:
#include <windows.h> #include <stdio.h> #include <iostream> #include <string> #include <vector> using namespace std; wstring getregistrykeyvalue(const wchar_t* regkey, const wchar_t* pvaluename) { long returnstatus; dword regtype = 0; dword regsize = 0; dword dwflags = rrf_rt_reg_sz | rrf_rt_reg_multi_sz | rrf_rt_reg_expand_sz | rrf_subkey_wow6464key; returnstatus = reggetvaluew(hkey_local_machine, regkey, pvaluename, dwflags, ®type, 0, ®size); if (returnstatus == error_success) { vector<byte> buf; buf.resize(regsize); returnstatus = reggetvaluew(hkey_local_machine, regkey, pvaluename, dwflags, ®type, &buf[0], ®size); if (returnstatus == error_success) return wstring((wchar_t*)&buf[0], (regsize / sizeof(wchar)) - 1); } return wstring(); } int main() { wcout << getregistrykeyvalue(l"software\\microsoft\\windows nt\\currentversion", l"productid") << endl; system("pause"); return 0; }
Comments
Post a Comment