zoukankan      html  css  js  c++  java
  • Windows服务启动进程----Cjwdev.WindowsApi.dll

    windows服务无法启动外部程

    做一个windows服务监听服务,涉及到windows服务启动外部程序的一个过程,但是调试测试发现,无法简单的用process.start()这种方法,

    原因是在vista和win7这样的系统下,服务是运行在session 0,而基本上应用是运行在session 1,所以windows服务下无法启动process.start()。

    网上看到windows服务下无法启动外部程序的原因,解决方案

    1、在WinXP和Win2003环境中,安装服务后,右键单击服务“属性”-“登录”选项卡-选择“本地系统帐户”并勾选“允许服务与桌面交互”即可.

    2、在Win7环境中,由于微软加强了权限管理,将此功能禁用,需要引用第三方dll,即Cjwdev.WindowsApi.dll

    Cjwdev.WindowsApi.xml,dll下载链接:http://pan.baidu.com/share/link?shareid=159544&uk=3288736938

    解决方法:使用这样一个第三方库,Cjwdev.WindowsApi.dll,能够从服务启动外部程序;

    public static void Openlocalexe(string path)
    {

    int _currentAquariusProcessID;
    /*appStartpath设置为全路径地址*/
    string appStartpath = path;
    IntPtr userTokenHandle = IntPtr.Zero;
    ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle);
    ApiDefinitions.PROCESS_INFORMATION procinfo = new ApiDefinitions.PROCESS_INFORMATION();
    ApiDefinitions.STARTUPINFO startinfo = new ApiDefinitions.STARTUPINFO();
    startinfo.cb = (uint)Marshal.SizeOf(startinfo);
    try
    {
    ApiDefinitions.CreateProcessAsUser(userTokenHandle, appStartpath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startinfo, out procinfo);
    if (userTokenHandle != IntPtr.Zero)
    ApiDefinitions.CloseHandle(userTokenHandle);

    _currentAquariusProcessID = (int)procinfo.dwProcessId;
    }
    catch (Exception exc)
    {
    Program.Log.Error(exc.Message);
    //System.Runtime.InteropServices.Interop.ShowMessageBox(exc.Message, "Comfirm");
    }
    }

    注意:要在windows服务里面去调用启动外部程序,并且外部程序不能使控制台程序,建议是winform.exe这样方便测试。

  • 相关阅读:
    Ghost Button制作教程及设计趋势分析
    onhashchange事件--司徒正美
    window.location.hash属性介绍
    优质UI的7条准则(一)
    当在浏览器地址栏输入一个网址的时候,究竟发生了什么?
    全球最快的JS模板引擎
    眨眼登录表单
    DIV+CSS规范命名
    es6--export,import
    es6--class
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/9992701.html
Copyright © 2011-2022 走看看