最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - schtasks cannot start application (windows 11) - Stack Overflow

programmeradmin5浏览0评论

Im working on a project to make my own auto startup application, only problem is it doesnt startup, really dont have any more information on the problem, its meant to start the app up after every minute and if its open it shouldnt open it again, instead it does nothing, i have tried "administrator", "Administrators", "everyone" and "MY_USERNAME" and i get the same response:

#include <iostream>
#include <Windows.h>
#include <filesystem>

std::string GetHomePath() {
    FILE* Command = _popen("echo %USERPROFILE%", "r");
    char BrokenHomePath[MAX_PATH];
    std::string HomePath;
    fgets(BrokenHomePath, MAX_PATH, Command);
    HomePath = BrokenHomePath;
    if (HomePath.back() == '\n') {
        HomePath.pop_back();
    }
    return HomePath;
}

int main() {
    std::string ExeLocation = "C:\\Users\\username\\File.exe";
    std::filesystem::create_directories(GetHomePath() + "\\Saved Games");
    if (!std::filesystem::copy_file(ExeLocation, GetHomePath() + "\\Saved Games\\File.exe", std::filesystem::copy_options::overwrite_existing))
        return false;

    std::string Command = "schtasks /tn RunFile /create /tr \"" + GetHomePath() + "\\Saved Games\\File.exe\" /sc minute /mo 1 /ru \"Administrators\"";
    std::cout << "Running Command: " << Command << "\n";

    int result = system(Command.c_str());
    std::cout << "System call result: " << result << std::endl;  // Print the result of the system call

    return 0;
}

my output:

Running Command: schtasks /tn RunFile /create /tr "C:\Users\username\Saved Games\File.exe" /sc minute /mo 1 /ru "Administrators"

WARNING: The task name "RunFile" already exists. Do you want to replace it (Y/N)? y

SUCCESS: The scheduled task "RunFile" has successfully been created.

System call result: 0

Im working on a project to make my own auto startup application, only problem is it doesnt startup, really dont have any more information on the problem, its meant to start the app up after every minute and if its open it shouldnt open it again, instead it does nothing, i have tried "administrator", "Administrators", "everyone" and "MY_USERNAME" and i get the same response:

#include <iostream>
#include <Windows.h>
#include <filesystem>

std::string GetHomePath() {
    FILE* Command = _popen("echo %USERPROFILE%", "r");
    char BrokenHomePath[MAX_PATH];
    std::string HomePath;
    fgets(BrokenHomePath, MAX_PATH, Command);
    HomePath = BrokenHomePath;
    if (HomePath.back() == '\n') {
        HomePath.pop_back();
    }
    return HomePath;
}

int main() {
    std::string ExeLocation = "C:\\Users\\username\\File.exe";
    std::filesystem::create_directories(GetHomePath() + "\\Saved Games");
    if (!std::filesystem::copy_file(ExeLocation, GetHomePath() + "\\Saved Games\\File.exe", std::filesystem::copy_options::overwrite_existing))
        return false;

    std::string Command = "schtasks /tn RunFile /create /tr \"" + GetHomePath() + "\\Saved Games\\File.exe\" /sc minute /mo 1 /ru \"Administrators\"";
    std::cout << "Running Command: " << Command << "\n";

    int result = system(Command.c_str());
    std::cout << "System call result: " << result << std::endl;  // Print the result of the system call

    return 0;
}

my output:

Running Command: schtasks /tn RunFile /create /tr "C:\Users\username\Saved Games\File.exe" /sc minute /mo 1 /ru "Administrators"

WARNING: The task name "RunFile" already exists. Do you want to replace it (Y/N)? y

SUCCESS: The scheduled task "RunFile" has successfully been created.

System call result: 0

Share Improve this question edited Mar 17 at 8:30 Botje 31.5k4 gold badges34 silver badges47 bronze badges asked Mar 17 at 4:29 user29779116user29779116 313 bronze badges 2
  • Side note, using C++ for this is overkill. You're immediately calling system again, so why not just use a powershell script for this. Or at least the schedule command should be part of an installer not your source code. – Pepijn Kramer Commented Mar 17 at 5:56
  • ik its overkill and this pracitcally is the installation script im trying to make, it succeeds, it just fails to switch form ready to running under schtasks despite starting every minute – user29779116 Commented Mar 17 at 6:25
Add a comment  | 

1 Answer 1

Reset to default 0

You cannot run scheduled tasks as administator anymore, only regular user (INTERACTIVE)

发布评论

评论列表(0)

  1. 暂无评论