在任务调度程序中运行C程序时出错

发布于 2024-11-18 19:01:23 字数 351 浏览 4 评论 0原文

我编写了一个 C 程序来离线获取图像并将其保存在使用 fopen/fread/fwrite 和 libcurl 的文件中。一切都编译得非常好,我可以通过双击程序完美地运行它。但是,当我尝试使用任务计划程序在计算机上设置任务每 10 分钟运行一次时,程序将打开,并且当它尝试保存文件时出现错误:

异常::句柄:异常:STATUS_ACCESS_VIOLATION open_stackdumpfile:将堆栈跟踪转储到 Garden.exe.stackdump

我正在运行 Windows Vista,并尝试使用本机 Windows 计划任务功能。我已选中任务属性中标记为“以最高权限运行”的框。

I made a C program to get an image offline and save it in a file using fopen/fread/fwrite and libcurl. Everything compiles perfectly fine and I can run it perfectly fine by double clicking the program. But, when I try and set a task on my computer using task scheduler to run it every 10 minutes, the program opens and when it tries to save the file I get the error:

exception::handle: Exception: STATUS_ACCESS_VIOLATION
open_stackdumpfile: Dumping stack trace to garden.exe.stackdump

I am running Windows Vista, and attempting to use the native Windows Scheduled Tasks feature. I have checked the box marked "Run with highest privileges" in the task properties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阪姬 2024-11-25 19:01:23

我的猜测是,无论您的程序正在运行什么,都是从您没有写入权限的目录中执行的。没有“garden.exe.stackdump”文件的事实给出了这一线索。

将其保存到文件时,是将其保存为 fopen("myfile","w") 还是使用完全限定名称?例如,假设您要保存的文件名为“foobar.png”,并且您想将其保存到您在下面命名的目录中,您会得到类似的内容:

    char fname[256] = "foobar.png";
    char directory[256] = "C:/Users/Joel/Desktop/garden/snaps";
    char path[256];

    memset(path, 0, sizeof(path));
    strcpy(path, directory);
    strcat(path, "/");
    strcat(path, fname);

    if ((fp = fopen(path, "w")) == NULL) {
        fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
        exit(1);
    }

    fwrite(yourdata, yourdata_size, 1, fp);

由于您的程序似乎也将错误转储到文件中同样,您最好在程序开始时使用 chdir("/home/myname") ,以便将任何“.stackdump”文件放置在您有权访问的位置。

您可能需要考虑的另一件事是您的任务计划程序可能以 nobody 或其他一些被剥夺权限的帐户运行您的脚本。如果是这种情况,您需要在 fopenchdir 中使用全局可写区域的完整路径(例如 /tmp)或具有开放权限的工作目录。例如:(

mkdir /home/myname/scratch
chmod a+rwx /home/myname/scratch
chmod a+x   /home/myname

您在主目录上设置执行位,以便无权限程序可以访问其子目录,即使它无法读取其中的任何内容。)

My guess is that whatever is running your program is doing so from a directory to which you don't have write permissions. The fact that there is no "garden.exe.stackdump" file gives that as a clue.

When you save it to a file, are you saving it as fopen("myfile","w") or are you using a fully qualified name? For example, let's say that the file you want to save is called "foobar.png" and you want to save it to the directory you named below, you'd have something like:

    char fname[256] = "foobar.png";
    char directory[256] = "C:/Users/Joel/Desktop/garden/snaps";
    char path[256];

    memset(path, 0, sizeof(path));
    strcpy(path, directory);
    strcat(path, "/");
    strcat(path, fname);

    if ((fp = fopen(path, "w")) == NULL) {
        fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
        exit(1);
    }

    fwrite(yourdata, yourdata_size, 1, fp);

Since your program also seems to dump errors to a file as well, you might do well to chdir("/home/myname") at the start of your program so that any ".stackdump" files get placed where you have access.

The other thing you might want to take into account is that your task scheduler may be running your script as nobody or some other permissions-deprived account. If that's the case, you'll want to use a full path in fopen and chdir to a globally writable area (such as /tmp) or a working directory with open permissions. For example:

mkdir /home/myname/scratch
chmod a+rwx /home/myname/scratch
chmod a+x   /home/myname

(You set the execute bit on your home directory so that the permission-less program can get access to its subdirectory even though it can't read anything in it.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文