英文:
Wide string command line arguments in C
问题
I've translated the code portion you provided:
我正在编写一个程序,首先要做的是检索第一个命令行参数(应该是进程名称),并找到该进程的相应 PID。
以下是我如何实现的:
在 main
中:
DWORD PID = FindProcessId(argv[1]);
这是找到 PID 的函数:
DWORD FindProcessId(PWCHAR processname)
{
NTSTATUS status;
PVOID buffer;
PSYSTEM_PROCESS_INFORMATION spi;
DWORD pid = 0;
buffer = VirtualAlloc(NULL, 1024 * 1024, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
spi = (PSYSTEM_PROCESS_INFORMATION)buffer;
status = NtQuerySystemInformation(SystemProcessInformation, spi, 1024 * 1024, NULL);
while (spi->NextEntryOffset) // Loop over the list until we reach the last entry, or found PID.
{
if (wcsncmp(spi->ImageName.Buffer, processname, spi->ImageName.Length) == 0)
{
pid = spi->UniqueProcessId;
break;
}
spi = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)spi + spi->NextEntryOffset); // Calculate the address of the next entry.
}
return pid;
}
这个问题是,如果我简单地写
DWORD PID = FindProcessId(L"notepad.exe");
,
它完美地运行。
但是当我使用命令行参数运行程序时,如下所示:
find_pid.exe notepad.exe
,FindProcessId 返回 0,表示它没有找到进程,尽管 notepad.exe
仍在运行。
你有任何关于为什么这个不适用于命令行参数的想法吗?
英文:
I'm writing a program that what it does first, is to retrieve the first command line argument (which should be a process name), and find the corresponding PID of the process.
Here's how I do it:
in main
DWORD PID = FindProcessId(argv[1]);
and here's the function that finds PID:
DWORD FindProcessId(PWCHAR processname)
{
NTSTATUS status;
PVOID buffer;
PSYSTEM_PROCESS_INFORMATION spi;
DWORD pid = 0;
buffer = VirtualAlloc(NULL, 1024 * 1024, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
spi = (PSYSTEM_PROCESS_INFORMATION)buffer;
status = NtQuerySystemInformation(SystemProcessInformation, spi, 1024 * 1024, NULL);
while (spi->NextEntryOffset) // Loop over the list until we reach the last entry, or found PID.
{
if (wcsncmp(spi->ImageName.Buffer, processname, spi->ImageName.Length) == 0)
{
pid = spi->UniqueProcessId;
break;
}
spi = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)spi + spi->NextEntryOffset); // Calculate the address of the next entry.
}
return pid;
}
The thing is, it works perfectly if i simply write
DWORD PID = FindProcessId(L"notepad.exe");
,
for example.
But when i use the command line argument and runthe program like so:
find_pid.exe notepad.exe
the FindProcessId returns 0, meaning it didn't find the process. Even though notepad.exe
is running like before.
Any ideas why this doesn't work with the command line argument?
To make the program accept PWCHAR
rather than char *
(Wide strings rather than ascii ones), I had to use main
instead of wmain
答案1
得分: 1
To make the program accept PWCHAR rather than char *(使用宽字符串而不是ASCII字符串),我不得不使用wmain而不是main,像这样:
int wmain(int argc, PWCHAR argv[])
{
....
return 0;
}
或者
只需将char *
转换为宽字符串 - PWCHAR
,像这样:
WCHAR victimProcessName[MAX_PATH];
mbstowcs(victimProcessName, argv[1], MAX_PATH); // 加上空字符
英文:
To make the program accept PWCHAR rather than char * (Wide strings rather than ascii ones), I had to use wmain instead of main, like so:
int wmain(int argc, PWCHAR argv[])
{
....
return 0;
}
OR
Just convert the char *
to a wide string - PWCHAR
, like so:
WCHAR victimProcessName[MAX_PATH];
mbstowcs(victimProcessName, argv[1], MAX_PATH); // Plus null
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论