简体   繁体   中英

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

I'm new to the windows services and visual studio. I am trying to start a.exe file from a wind32 application. The code works fine and there is no error. I am using a CreateProcess() method and checked whether the method is running properly. There is no issues in it. The.exe file which i am calling simply creates text document. When i call that.exe file from console, it works fine, it creates the file. But when I call it from the wind32 app, it does not create any file. I am using Visual studio 2019. This is my code for calling the.exe file. `

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. 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\\" .

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?

In that case, when you open SampleService.exe manually, Success.txt would appear in "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.

As I type this, I'm starting to doubt it, actually. Haven't you checked where it appears in Explorer?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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