Visual c ++ 2008中Windows服务中的CreateProcess问题 [英] CreateProcess problem in Windows service in Visual c++ 2008

查看:60
本文介绍了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.

推荐答案

尝试在服务属性对话框中启用与桌面进行交互"选项.这可能会有所帮助.
Try enabling ''Allo interact with desktop'' option in the service property dialog. This might help.


尝试一下:
Try this:
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.


如果您尝试捕获最后的错误代码,您会得到什么?

同样基于此问题,我还必须使其运行,因为Vista和UAC变得异常复杂,就像疯狂一样(考虑切换到Linux,不要开玩笑!)以使用CreateProcess.
不再有任何默认设置可以提供帮助.
特别是如果您启动的过程本身需要特殊的访问权限.
可能发生的情况是,运行Notepad.exe的过程比目标过程要复杂.因此,不再是快速尝试"的明显选择

另外,您还需要在创建过程中使用CREATE_BREAKAWAY_FROM_JOB标志(只是为了帮助对其进行调试)
并创建用于UAC管理的清单

参见> http://stackoverflow.com/questions/89588 /assignprocesstojobobject-fails-with-access-denied-error-when-running-under-the [ ^ ]
下运行时被拒绝错误
不知道服务"案.
祝你好运.
If you try to catch last error code what do you get ?

Also based on issue I also got to make it running, Since Vista and UAC it become incredibly more complex, like crazy, (consider switching to Linux, no joke!) to use CreateProcess.
There is no longer any kind of default that can help.
Specially if the process itself that you start need special access.
It may happens that running Notepad.exe is more complex than your targeted process. So no longer the obvious choice for a "quick try"

Also you need to use CREATE_BREAKAWAY_FROM_JOB flag during creation (just to help debugging it)
and create a manifest for UAC management

see http://stackoverflow.com/questions/89588/assignprocesstojobobject-fails-with-access-denied-error-when-running-under-the[^]

No idea about the "service" case.
Good luck.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆