c++ - 自动运行程序 c++
问题描述
我正在尝试制作一个可以在计算机启动时启动的 c++ 程序。
当我单击可执行文件时,它的作用是将自身复制到启动文件夹中
int CopyPasteFile(bool overWrite=true)
{
wchar_t this_exe_path[MAX_PATH];
if (!GetModuleFileName(NULL, this_exe_path, MAX_PATH)) // this exe path basically
return -1;
if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_STARTUP, NULL, 0, startup_path))) path to get to, the windows startup folder
return -2;
auto from = experimental::filesystem::path(this_exe_path);
thisFilename = from;
to = experimental::filesystem::path(startup_path) / "program.exe";
if (!CopyFile(from.c_str(), to.c_str(), overWrite))
{
return -3; //copies itself there
}
return 0;
}
在这里,代码可以让我的程序将自己复制到该文件夹中。
接下来发生的事情是,每当计算机启动时,程序都会启动,但无法创建我需要的文本文件
int CheckForMessages()
{
SHELLEXECUTEINFO info = { 0 };
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.fMask = SEE_MASK_NOCLOSEPROCESS;
info.hwnd = NULL;
info.lpVerb = NULL;
info.lpFile = L"cmd.exe ";
info.lpParameters = LR"( /c "FOR /F "tokens=*" %g IN ('curl --get https://api.telegram.org/botXXX:YYY/getupdates') do (SET VAR=%g%) & echo %g>File.txt" )";
info.lpDirectory = NULL;
info.nShow = SW_HIDE;
info.hInstApp = NULL;
ShellExecuteEx(&info); //creates File.txt and stores telegram's response there
Sleep(2000);
fstream f("File.txt", fstream::in);
string s; //reads it and gets it into the string s
getline(f, s, '\0');
string ToFind[2] = { R"("text":"command1")", R"("text":"command2")" };
string MyChatID= "00000000";
if ( (s.find(ToFind[0]) != string::npos) && (s.find(MyChatID) != string::npos) )//command1
{
sendMessage("command1+executed");
f.close();
remove("File.txt");
return -1;
}
那是做什么的,基本上是告诉我计算机何时启动,并让我使用电报执行操作。
它将请求存储在 File.txt 中,然后读取它,然后将其删除。但是会发生什么,当我启动我的电脑时,我从机器人收到消息,但由于某种原因,程序无法创建 txt 文件。
如果我亲自去执行程序,通过单击exe,一切正常
我从我的机器人中得到的基本上是该代码的输出,它位于主函数的顶部
telegram tg;
tg.sendMessage("computer+booted+up");
这是来自机器人的一条消息,上面写着“计算机已启动”
我尝试了很多不同的东西,甚至让程序自己执行了两次,但没有我的输入,什么都没有!
我能做些什么?谢谢大家,我是一个 14 岁的人,正在努力发展,但这对我来说都很难,语言也是如此,因为我不是以英语为母语的人......但无论如何,谢谢你的帮助
编辑:我试图得到错误,它返回 0,好像一切正常。
'''
SHELLEXECUTEINFO info = { 0 };
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.fMask = SEE_MASK_NOCLOSEPROCESS;
info.hwnd = NULL;
info.lpVerb = NULL;
info.lpFile = L"cmd.exe ";
info.lpParameters = LR"( /c "FOR /F "tokens=*" %g IN ('curl --get https://api.telegram.org/botXXX:YYY/getupdates') do (SET VAR=%g%) & echo %g>File.txt" )";
info.lpDirectory = NULL;
info.nShow = SW_HIDE;
info.hInstApp = NULL;
ShellExecuteEx(&info);
int a = GetLastError();
MessageBox(0, to_wstring(a).c_str(), 0, 0);
'''
解决方案
推荐阅读
- java - If 语句的正确输出出现问题
- javascript - 带有 Javascript 类和构造函数的计时器代码
- php - SQL 使用 NOT IN 的子查询结果
- javascript - 转换温度但华氏值不返回
- html - 文件预览显示 Angular 9 中最后选择的文件
- python - apache 服务器正在运行但 django 站点未加载(使用 wsgi)
- python - Numpy中傅里叶变换的可逆性问题
- php - SQL 查询获取主选择查询的行排名或位置
- performance - 单个图表中的两个指标 applicationinsights
- angular - TypeError:无法读取未定义的属性“自动完成”