2015-11-05 52 views
0

我試圖將wchar *轉換爲字符串。首先,我把它當作wstring。當我搜索時,此方法在stackoverflow中指定。但這對我來說不起作用。它出什麼問題了?如何將WCHAR *轉換爲C++中的字符串,反之亦然?

GetProcessImageNameFromPID.cpp

 BOOL GetProcessImageNameFromPID::getProcessNameFromProcessID(DWORD processId, WCHAR**processName) 
     { 
      HANDLE hProcessSnap; 
      HANDLE hProcess; 
      PROCESSENTRY32 pe32; 
      DWORD dwPriorityClass; 

      // Take a snapshot of all processes in the system. 
      hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
      if (hProcessSnap == INVALID_HANDLE_VALUE) 
      { 
       printError(TEXT("CreateToolhelp32Snapshot (of processes)")); 
       return(FALSE); 
      } 

     // Set the size of the structure before using it. 
     pe32.dwSize = sizeof(PROCESSENTRY32); 

     // Retrieve information about the first process, 
     // and exit if unsuccessful 
     if (!Process32First(hProcessSnap, &pe32)) 
     { 
      printError(TEXT("Process32First")); // show cause of failure 
      CloseHandle(hProcessSnap);   // clean the snapshot object 
      return(FALSE); 
     } 

     // Now walk the snapshot of processes, and 
     // display information about each process in turn 
     int i = 0; 
     do 
     { 
      WCHAR*allprocessName = pe32.szExeFile; 
      //_tprintf(TEXT("\n%d)PROCESS NAME: %s"), i, allprocessName); 

      // Retrieve the priority class. 
      dwPriorityClass = 0; 
      hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); 
      if (hProcess == NULL) 
       printError(TEXT("OpenProcess")); 
      else 
      { 
       dwPriorityClass = GetPriorityClass(hProcess); 
       if (!dwPriorityClass) 
        printError(TEXT("GetPriorityClass")); 
       CloseHandle(hProcess); 
      } 
      DWORD pid = pe32.th32ProcessID; 
      //_tprintf(TEXT("\n Process ID  = %d"), pid); 
      if (pid == processId) 
      { 
       *processName = allprocessName; 
       //_tprintf(TEXT("Inside Method:\n")); 
       _tprintf(TEXT("PROCESS NAME: %s\n\n"), *processName); 
       return TRUE; 
      } 
      i++; 
     } while (Process32Next(hProcessSnap, &pe32)); 

     CloseHandle(hProcessSnap); 
     return(FALSE); 
    } 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    WCHAR**processName = (WCHAR**)malloc(sizeof(WCHAR)); 
    GetProcessImageNameFromPID::getProcessNameFromProcessID(4, processName); 
    _tprintf(TEXT("PROCESS NAME: %s\n\n"), *processName); // correct 

      GetProcessImageNameFromPID::getProcessNameFromProcessID(executionProcessID, processName); 
      wstring ws(*processName); 
      string str(ws.begin(), ws.end()); 
      processImageName = str; 
      cout << processImageName << endl; // some wrong characters are printed 
} 
+1

爲什麼要將寬字符轉換爲常規字符?寬字符串中任何不能用窄字符串表示的東西都會給你帶來垃圾。 – NathanOliver

回答

0

有你的代碼的各種問題,最後一個是最嚴重的:

這看起來很奇怪:

WCHAR**processName = (WCHAR**)malloc(sizeof(WCHAR)); 

我想你想指向WCHAR *的指針,爲什麼不是你:

WCHAR* processName; 

然後:

GetProcessImageNameFromPID::getProcessNameFromProcessID(4, &processName); 
                  ^~~~~ !! 

什麼是processImageName類型?過程的名稱是什麼,如果它包含非ASCII字符,那麼你的轉換代碼會給出錯誤的字符。

另一個原因是,代碼:

*processName = allprocessName; 

正在* processName等於指針的函數結束後,其懸空的指針,它指向WCHAR陣列:

PROCESSENTRY32 pe32; 

其中創建上疊加。

你應該做的是讓processName數組:

WCHAR processName[MAX_PATH]; 

,並從PE32到這陣你的函數複製過程名裏的。

+0

我發送processName指針,將其分配到您談論的懸掛指針的方法 – Veena

+0

中。但在主要方法_tprintf(TEXT(「PROCESS NAME:%s \ n \ n」),* processName); //正確 – Veena

+0

@Veena - 在此_tprintf之後,您執行使用堆棧覆蓋以前數據的代碼,其中PROCESSENTRY32 pe32;內容。在main中移動_tprintf更低,你會看到它不再起作用, – marcinj

相關問題