开发者

C++ Windows process ID in binary

I wou开发者_如何学JAVAld like to write in C++ on Windows the prcoess ID of the program (by the program itself) in a file (binary is preferred). I saw the use of the function: GetProcessId, but i didn't manage to work with it.

1) How to use it?

2) how to tranfer the value into binary and write to the file?

Thanks


You can use GetCurrentProcessId() to get the process id of the current process. Then you can use ultoa to convert that number to a string using base 2 (and a buffer of size sizeof(DWORD) * 8 + 1), then you can use ofstream or fwrite to write it to a file.

Example:

DWORD id = GetCurrentProcessId();

char buf[sizeof(DWORD) * 8 + 1];
ultoa(id, buf, 2);

ofstream f("file.txt");
f << id;

I assume that by "transfer the value to binary" you mean turn it into a representation so that when you open the file, it looks like 1001011110101 or something. If you just want to see it as a number, then don't use itoa but do the rest.


Get the process ID by calling GetCurrentProcessId(). You don't need to convert that into a binary representation since integral types are already stored in binary. Just write it to the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜