本文介绍了Visual c ++ 2008中Windows服务中的CreateProcess问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经在Visual C ++ 2008中创建了Windows服务,并且能够启动/停止该服务.我面临的问题是我在OnStart方法中编写的CreateProcess.

我的OnStart方法是:

Hi,
I have created a windows service in Visual C++ 2008, and I am able to start/stop the service. The problem I am facing is in the CreateProcess which I have written in OnStart method.

My OnStart method is:

virtual void OnStart(array<String^>^ args) override
{
   STARTUPINFO siStartupInfo;
   PROCESS_INFORMATION piProcessInfo;
   memset(&siStartupInfo, 0, sizeof(siStartupInfo));
   memset(&piProcessInfo, 0, sizeof(piProcessInfo));
   siStartupInfo.cb = sizeof(siStartupInfo);
   std::ofstream o("Hello.txt");

   if(CreateProcess(L"C:\\windows\\notepad.exe",
                    L"C:\\windows\\test.txt",
                    0,
                    0,
                    TRUE,
                    CREATE_NO_WINDOW,
                    0,
                    0,
                    &siStartupInfo,
                    &piProcessInfo) == FALSE)
    {
        o << "FALSE\n" << std::endl;
    }
    else
    {
        o << "TRUE \n" << std::endl;
    }
}


当我安装并启动该服务时,它将成功启动并创建文件Hello.txt,并将CreateProcess的结果写入文件"TRUE",但未打开记事本.启动服务时,能否请您帮我打开记事本?

谢谢.


When I install and start the service, it starts successfully and the file Hello.txt is created and result of CreateProcess is written to file as "TRUE", but the notepad is not opened. Can you please help me in getting the notepad open when I start the service?

Thanks.

推荐答案


WIN32_FIND_DATA fd;
HANDLE          hf = FindFirstFile(L"C:\\windows\\notepad.exe",&fd);
ASSERT(INVALID_HANDLE_VALUE!=hf); FindClose(hf);
if(0==CreateProcess(L"C:\\windows\\notepad.exe",
                    L" C:\\windows\\test.txt",
// -------------------^ this is a bug
                    0,
                    0,
                    TRUE,
                    CREATE_NO_WINDOW,
                    0,
                    0,
                    &siStartupInfo,
                    &piProcessInfo))
 {
     o << "FALSE\n" << std::endl;
 }
 else
 {
     o << "TRUE \n" << std::endl;
 }


祝你好运.


good luck.



这篇关于Visual c ++ 2008中Windows服务中的CreateProcess问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 06:39