“在C中的宽字符命令行参数”

huangapple go评论52阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月13日 23:22:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定