..
UNICODE_STRING ProcessName;
...
PSYSTEM_PROCESS_INFORMATION pInfo;
...
wstring str = pInfo->ProcessName.Buffer; //错误
请问如何把pInfo->ProcessName.Buffer内容给一个wstring变量?
8 个解决方案
#1
wstring str( pInfo->ProcessName.Buffer );
#2
反向转换:
wstring wstr = L"Test wstring";
const wchar_t *pwstr = wstr.c_str();
wstring wstr = L"Test wstring";
const wchar_t *pwstr = wstr.c_str();
#3
你这种写法和我这种wstring str = pInfo->ProcessName.Buffer;是一样的啊。
编译都可以通过,程序运行就报错。
错误提示:
Debug Assertion Failed!
Program: test.exe
File: ...\xstring
Line:930
Expression:invaild null pointer
...
#4
检查pInfo是否为空,不是转换有问题,是指针为空
Expression:invaild null pointer
#5
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <ntsecapi.h>
#include <string>
#include <locale.h>
using namespace std;
#define SystemProcessesAndThreadsInformation 5
typedef DWORD (WINAPI *ZWQUERYSYSTEMINFORMATION) (DWORD, PVOID, DWORD, PDWORD);
typedef struct _SYSTEM_PROCESS_INFORMATION{
DWORD NextEntryDelta;
DWORD ThreadCount;
DWORD Reserved1[6];
FILETIME ftCreateTime;
FILETIME ftUserTime;
FILETIME ftKernelTime;
UNICODE_STRING ProcessName;
DWORD BasePriority;
DWORD ProcessId;
DWORD InheritedFromProcessId;
DWORD HandleCount;
DWORD Reserved2[2];
DWORD VmCounters;
DWORD dCommitCharge;
PVOID ThreadInfos[1];
}SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;
BOOL EnableDebugPriv();
void show();
int main()
{
setlocale(LC_ALL, "");
EnableDebugPriv();
show();
return 0;
}
BOOL EnableDebugPriv()
{
typedef int (__stdcall * type_RtlAdjustPrivilege)(int, bool, bool, int*);
type_RtlAdjustPrivilege RtlAdjustPrivilege = (type_RtlAdjustPrivilege)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlAdjustPrivilege");
if(RtlAdjustPrivilege == NULL)
{
return FALSE;
}
int nEn = 0;
RtlAdjustPrivilege(0x14,TRUE,FALSE,&nEn);
return TRUE;
}
void show()
{
ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = (ZWQUERYSYSTEMINFORMATION)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "ZwQuerySystemInformation");
LPVOID pBuffer = NULL;
ULONG cbBuffer = 0x10000;
pBuffer = malloc(cbBuffer);
if(pBuffer == NULL)
{
return;
}
ZwQuerySystemInformation(SystemProcessesAndThreadsInformation, pBuffer, cbBuffer, NULL);
PSYSTEM_PROCESS_INFORMATION pInfo = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
for(;;)
{
//wprintf(L"PID:%d\t%s\n", pInfo->ProcessId, pInfo->ProcessName.Buffer);
DWORD PID = pInfo->ProcessId;
wstring szPath = pInfo->ProcessName.Buffer; //请教这里如何写?
wprintf(L"%d\t%s\n", PID, szPath);
if(pInfo->NextEntryDelta == 0)
{
break;
}
pInfo = (PSYSTEM_PROCESS_INFORMATION)(((PUCHAR)pInfo) + pInfo->NextEntryDelta);
}
free(pBuffer);
}
代码发出来,大家帮忙看看要如何改,谢谢!
#6
WCHAR szPath[1024] = L"";
memcpy(szPath, pInfo->ProcessName.Buffer, pInfo->ProcessName.Length);
用memcpy成功了。
不知道还有没有更多的解决方法?
#7
WCHAR szPath[1024] = L"";
这个。。。很像JAVA里的写法,还是改成:WCHAR szPath[1024] = {0};吧。。
另外,为什么memcpy就成功了,wstring就失败了,你是不是 pInfo->ProcessName.Buffer 没有结束符呢。
memcpy你是显式的规定了长度,所以可能没错。
这个。。。很像JAVA里的写法,还是改成:WCHAR szPath[1024] = {0};吧。。
另外,为什么memcpy就成功了,wstring就失败了,你是不是 pInfo->ProcessName.Buffer 没有结束符呢。
memcpy你是显式的规定了长度,所以可能没错。
#8
UNICODE_STRING的Buffer不需要以0结束
#1
wstring str( pInfo->ProcessName.Buffer );
#2
反向转换:
wstring wstr = L"Test wstring";
const wchar_t *pwstr = wstr.c_str();
wstring wstr = L"Test wstring";
const wchar_t *pwstr = wstr.c_str();
#3
你这种写法和我这种wstring str = pInfo->ProcessName.Buffer;是一样的啊。
编译都可以通过,程序运行就报错。
错误提示:
Debug Assertion Failed!
Program: test.exe
File: ...\xstring
Line:930
Expression:invaild null pointer
...
#4
检查pInfo是否为空,不是转换有问题,是指针为空
Expression:invaild null pointer
#5
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <ntsecapi.h>
#include <string>
#include <locale.h>
using namespace std;
#define SystemProcessesAndThreadsInformation 5
typedef DWORD (WINAPI *ZWQUERYSYSTEMINFORMATION) (DWORD, PVOID, DWORD, PDWORD);
typedef struct _SYSTEM_PROCESS_INFORMATION{
DWORD NextEntryDelta;
DWORD ThreadCount;
DWORD Reserved1[6];
FILETIME ftCreateTime;
FILETIME ftUserTime;
FILETIME ftKernelTime;
UNICODE_STRING ProcessName;
DWORD BasePriority;
DWORD ProcessId;
DWORD InheritedFromProcessId;
DWORD HandleCount;
DWORD Reserved2[2];
DWORD VmCounters;
DWORD dCommitCharge;
PVOID ThreadInfos[1];
}SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;
BOOL EnableDebugPriv();
void show();
int main()
{
setlocale(LC_ALL, "");
EnableDebugPriv();
show();
return 0;
}
BOOL EnableDebugPriv()
{
typedef int (__stdcall * type_RtlAdjustPrivilege)(int, bool, bool, int*);
type_RtlAdjustPrivilege RtlAdjustPrivilege = (type_RtlAdjustPrivilege)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlAdjustPrivilege");
if(RtlAdjustPrivilege == NULL)
{
return FALSE;
}
int nEn = 0;
RtlAdjustPrivilege(0x14,TRUE,FALSE,&nEn);
return TRUE;
}
void show()
{
ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = (ZWQUERYSYSTEMINFORMATION)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "ZwQuerySystemInformation");
LPVOID pBuffer = NULL;
ULONG cbBuffer = 0x10000;
pBuffer = malloc(cbBuffer);
if(pBuffer == NULL)
{
return;
}
ZwQuerySystemInformation(SystemProcessesAndThreadsInformation, pBuffer, cbBuffer, NULL);
PSYSTEM_PROCESS_INFORMATION pInfo = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
for(;;)
{
//wprintf(L"PID:%d\t%s\n", pInfo->ProcessId, pInfo->ProcessName.Buffer);
DWORD PID = pInfo->ProcessId;
wstring szPath = pInfo->ProcessName.Buffer; //请教这里如何写?
wprintf(L"%d\t%s\n", PID, szPath);
if(pInfo->NextEntryDelta == 0)
{
break;
}
pInfo = (PSYSTEM_PROCESS_INFORMATION)(((PUCHAR)pInfo) + pInfo->NextEntryDelta);
}
free(pBuffer);
}
代码发出来,大家帮忙看看要如何改,谢谢!
#6
WCHAR szPath[1024] = L"";
memcpy(szPath, pInfo->ProcessName.Buffer, pInfo->ProcessName.Length);
用memcpy成功了。
不知道还有没有更多的解决方法?
#7
WCHAR szPath[1024] = L"";
这个。。。很像JAVA里的写法,还是改成:WCHAR szPath[1024] = {0};吧。。
另外,为什么memcpy就成功了,wstring就失败了,你是不是 pInfo->ProcessName.Buffer 没有结束符呢。
memcpy你是显式的规定了长度,所以可能没错。
这个。。。很像JAVA里的写法,还是改成:WCHAR szPath[1024] = {0};吧。。
另外,为什么memcpy就成功了,wstring就失败了,你是不是 pInfo->ProcessName.Buffer 没有结束符呢。
memcpy你是显式的规定了长度,所以可能没错。
#8
UNICODE_STRING的Buffer不需要以0结束