Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将WCHAR*转换为C+中的字符串+;反之亦然?_C++ - Fatal编程技术网

C++ 如何将WCHAR*转换为C+中的字符串+;反之亦然?

C++ 如何将WCHAR*转换为C+中的字符串+;反之亦然?,c++,C++,我尝试将wchar*转换为字符串。首先我把它做成了一个戒指。此方法在我搜索时在stackoverflow中指定。但这对我来说不起作用。怎么了 GetProcessImageNameFromPID.cpp BOOL GetProcessImageNameFromPID::getProcessNameFromProcessID(DWORD processId, WCHAR**processName) { HANDLE hProcessSnap;

我尝试将wchar*转换为字符串。首先我把它做成了一个戒指。此方法在我搜索时在stackoverflow中指定。但这对我来说不起作用。怎么了

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
}
BOOL GetProcessImageNameFromPID::getProcessNameFromProcessID(DWORD processId,WCHAR**processName)
{
处理hprocessnap;
处理hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
//拍摄系统中所有进程的快照。
hprocessnap=CreateToolhelp32Snapshot(TH32CS\u SNAPPROCESS,0);
if(hprocessnap==无效的句柄值)
{
打印错误(文本(“CreateToolhelp32Snapshot(进程)”);
返回(假);
}
//在使用之前设置结构的大小。
pe32.dwSize=sizeof(PROCESSENTRY32);
//检索有关第一个进程的信息,
//如果失败则退出
如果(!Process32First(HPProcessSnap和pe32))
{
printError(TEXT(“Process32First”);//显示故障原因
CloseHandle(HPProcessSnap);//清除快照对象
返回(假);
}
//现在浏览流程的快照,然后
//依次显示每个流程的相关信息
int i=0;
做
{
WCHAR*allprocessName=pe32.szExeFile;
//_tprintf(文本(“\n%d)进程名称:%s”)、i、allprocessName);
//检索优先级类。
dwPriorityClass=0;
hProcess=OpenProcess(PROCESS\u ALL\u ACCESS,FALSE,pe32.th32ProcessID);
if(hProcess==NULL)
打印错误(文本(“OpenProcess”);
其他的
{
dwPriorityClass=GetPriorityClass(HPProcess);
如果(!dwPriorityClass)
打印错误(文本(“GetPriorityClass”);
CloseHandle(hProcess);
}
DWORD pid=pe32.th32ProcessID;
//_tprintf(文本(“\n进程ID=%d”),pid);
if(pid==processId)
{
*processName=allprocessName;
//_tprintf(文本(“内部方法:\n”);
_tprintf(文本(“进程名称:%s\n\n”),*进程名称);
返回TRUE;
}
i++;
}while(Process32Next(hprocessnap,&pe32));
CloseHandle(hProcessSnap);
返回(假);
}
int _tmain(int argc,_TCHAR*argv[]
{
WCHAR**processName=(WCHAR**)malloc(sizeof(WCHAR));
GetProcessImageNameFromPID::getProcessNameFromProcessID(4,processName);
_tprintf(TEXT(“进程名称:%s\n\n”),*processName);//正确
GetProcessImageNameFromPID::getProcessNameFromProcessID(executionProcessID,processName);
wstring ws(*processName);
字符串str(ws.begin(),ws.end());
processImageName=str;

cout您的代码有各种各样的问题,最后一个问题是最严重的:

这看起来很奇怪:

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复制到此数组。

为什么要将宽字符转换为常规字符?宽字符串中不能用窄字符串表示的任何内容都会给您带来垃圾。您谈到了悬空指针。但在main方法中,*tprintf(TEXT(“进程名:%s\n\n”),*processName); // correct@Veena-在此_tprintf之后,您执行使用堆栈的代码,该堆栈会覆盖以前的数据以及其他PROCESSENTRY32 pe32;内容。将该_tprintf移到main中较低的位置,您将看到它不再工作,