lundi 13 juin 2016

Why this code runs perfectly in visual studio with multibyte characher set but not with unicode char set?


when I run this code on g++ , it runs smoothly, but when I run this code on visual studio wiith unicode char set option, it doesn't print product id. can you explain me how to fix this problem and why it 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];

        /* Get Value. */
        ReturnStatus = RegQueryValueEx(Registry, pPIDName, 0, &regType, (LPBYTE)pPID, &regSize);


        RegCloseKey(Registry);

        if (pPID[regSize] > 127 || pPID[regSize] < 32)
        {
            pPID[regSize] = '�';
        }

        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;
}

Aucun commentaire:

Enregistrer un commentaire