C++获取当前文件名并在 WinExec 上使用它

C++ get current file name and use it on WinExec

本文关键字:WinExec 获取 文件名 C++      更新时间:2023-10-16

如何获取没有路径的当前文件名,我想在WinExec上使用它,

我试图做什么的例子,

WinExec("Do something to <mycurrentfilename.exe>", SW_HIDE);

在通常情况下,你必须使用GetModuleFileName函数。

例:

#include <Windows.h>
#include <sstream>
int main(void) {
char myexepath[MAX_PATH] = { 0 };
DWORD returnCode = GetModuleFileNameA(NULL, myexepath, MAX_PATH);
if (returnCode != 0 && returnCode < MAX_PATH)
{
std::string filepath(myexepath);
filepath = filepath.substr(filepath.find_last_of('') + 1);
std::ostringstream ss;
ss << "Do something to "" << filepath << """;
WinExec(ss.str().c_str(), SW_HIDE);
}
else
{
// process GetModuleFileName error.
}
return 0;
}

示例使用char编码作为文件名,但可以更改为wchar_t或通用TCHAR