In Native C++, how can I get the current process Name and date time. I am not a C++ programmer.
In C#, it is very trivial to do it like this:
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?
To get the name of the current process you can use 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.
To get the current time, you can use std::time()
.
GetSystemTime
Syntax
void WINAPI GetSystemTime(
__out LPSYSTEMTIME lpSystemTime
);
Parameters: lpSystemTime [out]
A pointer to a SYSTEMTIME structure to receive the current system date and time. The lpSystemTime parameter must not be NULL. Using NULL will result in an access violation.
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.
Requirements: Minimum supported client Windows 2000 Professional
Minimum supported server: Windows 2000 Server
Header : Winbase.h (include Windows.h)
Library : Kernel32.lib
DLL : Kernel32.dll
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724390(v=vs.85).aspx
And for process name:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.