简体   繁体   English

如何使用 c++ 从 windows 服务运行 a.exe 文件

[英]How can I run a .exe file from a windows service using c++

I'm new to the windows services and visual studio.我是 windows 服务和视觉工作室的新手。 I am trying to start a.exe file from a wind32 application.我正在尝试从 wind32 应用程序启动 a.exe 文件。 The code works fine and there is no error.代码工作正常,没有错误。 I am using a CreateProcess() method and checked whether the method is running properly.我正在使用 CreateProcess() 方法并检查该方法是否正常运行。 There is no issues in it.它没有任何问题。 The.exe file which i am calling simply creates text document.我调用的.exe 文件只是创建文本文档。 When i call that.exe file from console, it works fine, it creates the file.当我从控制台调用 that.exe 文件时,它工作正常,它会创建文件。 But when I call it from the wind32 app, it does not create any file.但是当我从 wind32 应用程序调用它时,它不会创建任何文件。 I am using Visual studio 2019. This is my code for calling the.exe file.我正在使用 Visual Studio 2019。这是我调用 .exe 文件的代码。 ` `

STARTUPINFO info;
PROCESS_INFORMATION processInfo;
ZeroMemory(&info, sizeof(info));
info.cb = sizeof(info);
ZeroMemory(&processInfo, sizeof(processInfo));

LPCWSTR path = L"C:\\HP\\...(pathofexe).exe";
bool bSuccess = CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo);

if (bSuccess)
{
    cout << "Success";
}
else
{
    cout << "Error : " << GetLastError() << endl;
}`

Rgarding the working directory mismatch that was discussed in the comments, The file may not be there because it's simply somewhere else.关于评论中讨论的工作目录不匹配,该文件可能不存在,因为它只是在其他地方。

You call:你打电话:

CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo);

Assuming you've read the documentation , you know the 8th parameter specifies the directory where the new process will run.假设您已阅读文档,您知道第 8 个参数指定了新进程将运行的目录。 Because you use NULL , this directory will be the same as that of the caller process, I assume "C:\\Users\\HP\\source\\repos\\ThisThingsName\\Debug\\" .因为您使用NULL ,所以这个目录将与调用者进程的目录相同,我假设"C:\\Users\\HP\\source\\repos\\ThisThingsName\\Debug\\"

That alone is not a problem, but given the context I believe the callee ( SampleService.exe ) calls the file function from its relative path, "\\Success.txt" , rather than the full path, is that right?仅此一点不是问题,但鉴于上下文我相信被调用者SampleService.exe )从其相对路径"\\Success.txt"而不是完整路径调用文件 function ,对吗?

In that case, when you open SampleService.exe manually, Success.txt would appear in "C:\\Users\\HP\\source\\repos\\SampleService\\Debug\\" .在这种情况下,当您手动打开SampleService.exe时, Success.txt将出现在"C:\\Users\\HP\\source\\repos\\SampleService\\Debug\\"中。 However, when you open it using CreateProcess , it runs in "C:\\Users\\HP\\source\\repos\\ThisThingsName\\Debug\\" (because you did not specify otherwise), which is also where Success.txt would appear.但是,当您使用CreateProcess打开它时,它会在"C:\\Users\\HP\\source\\repos\\ThisThingsName\\Debug\\"中运行(因为您没有另外指定),这也是Success.txt会出现。

As I type this, I'm starting to doubt it, actually.实际上,当我键入此内容时,我开始怀疑它。 Haven't you checked where it appears in Explorer?您没有检查它在资源管理器中的显示位置吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM