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, &registry);     if (returnstatus == error_success)     {         returnstatus = regqueryvalueex(registry, ppidname, 0, &regtype, 0, &regsize);         ppid = new char[regsize];          /* value. */         returnstatus = regqueryvalueex(registry, ppidname, 0, &regtype, (lpbyte)ppid, &regsize);           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, &regtype, 0, &regsize);     if (returnstatus == error_success)     {         ws = new wchar_t[regsize / sizeof(wchar)];          returnstatus = reggetvaluew(hkey_local_machine, regkey, pvaluename, dwflags, &regtype, ws, &regsize);         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, &regtype, 0, &regsize);     if (returnstatus == error_success)     {         vector<byte> buf;         buf.resize(regsize);          returnstatus = reggetvaluew(hkey_local_machine, regkey, pvaluename, dwflags, &regtype, &buf[0], &regsize);         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

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -