简体   繁体   English

如何在本机C ++中获取进程名称和日期时间戳

[英]how to get process name and date time stamp in native C++

In Native C++, how can I get the current process Name and date time. 在Native C ++中,如何获取当前进程的名称和日期时间。 I am not a C++ programmer. 我不是C ++程序员。

In C#, it is very trivial to do it like this: 在C#中,这样做很简单:

Process name: 流程名称:

Process.GetCurrentProcess().ProcessName

Date Time: 约会时间:

DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss.ff")

How can they be retrieved in C++ native? 如何在C ++本机中检索它们?

To get the name of the current process you can use GetModuleFileName() : 要获取当前进程的名称,可以使用GetModuleFileName()

char exe_path[MAX_PATH];
GetModuleFileName(NULL, exe_path, MAX_PATH);

You can then extract the part of exe_path after the last \\ character. 然后,您可以提取exe_path中最后一个\\字符之后的部分。

To get the current time, you can use std::time() . 要获取当前时间,可以使用std::time()

GetSystemTime GetSystemTime

Syntax 句法

void WINAPI GetSystemTime(
  __out  LPSYSTEMTIME lpSystemTime
);

Parameters: lpSystemTime [out] 参数: lpSystemTime [输出]

A pointer to a SYSTEMTIME structure to receive the current system date and time. 指向SYSTEMTIME结构的指针,以接收当前系统日期和时间。 The lpSystemTime parameter must not be NULL. lpSystemTime参数不能为NULL。 Using NULL will result in an access violation. 使用NULL将导致访问冲突。

Return value: This function does not return a value or provide extended error information. 返回值:该函数不返回值或提供扩展的错误信息。

Remarks: To set the current system date and time, use the SetSystemTime function. 备注:要设置当前系统日期和时间,请使用SetSystemTime函数。

Requirements: Minimum supported client Windows 2000 Professional 要求:支持的最低客户端Windows 2000 Professional

Minimum supported server: Windows 2000 Server 支持的最低服务器: Windows 2000 Server

Header : Winbase.h (include Windows.h) 标题: Winbase.h(包括Windows.h)

Library : Kernel32.lib 程式库: Kernel32.lib

DLL : Kernel32.dll DLL文件: Kernel32.dll

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724390(v=vs.85).aspx http://msdn.microsoft.com/zh-CN/library/windows/desktop/ms724390(v=vs.85).aspx

And for process name: 对于进程名称:

QueryFullProcessImageName function QueryFullProcessImageName函数

GetModuleFileNameEx function GetModuleFileNameEx函数

Process and Thread Functions can be found here 进程和线程函数可以在这里找到

Process name: 流程名称:

char name[256];
GetProcessImageFileName(GetCurrentProcess(),name,256);

You can also get it from the command-line arguments 您也可以从命令行参数获取它

And for the date use localtime() from time.h, example here 对于日期,请使用time.h中的localtime(),例如此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM