windows服务调用窗体(C#)

windows服务调用窗体(C#)

(2010-04-25 22:33:44)
 分类: 各类经验

我在做一个服务,需要调用窗体程序,很郁闷,调出来之后进程显示,不显示窗体,而且用户为system,于是百度谷歌(在天笑的博客中发现)
贴在这里以备日后所用。
这个用到了系统组件,帖代码

[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
        
public class SECURITY_ATTRIBUTES     
        {
            
public int nLength;
            
public string lpSecurityDescriptor;
            
public bool bInheritHandle;
        }
        [StructLayout(LayoutKind.Sequential)]
        
public struct STARTUPINFO
        {
            
public int cb;
            
public string lpReserved;
            
public string lpDesktop;
            
public int lpTitle;
            
public int dwX;
            
public int dwY;
            
public int dwXSize;
            
public int dwYSize;
            
public int dwXCountChars;
            
public int dwYCountChars;
            
public int dwFillAttribute;
            
public int dwFlags;
            
public int wShowWindow;
            
public int cbReserved2;
            
public byte lpReserved2;
            
public IntPtr hStdInput;
            
public IntPtr hStdOutput;
            
public IntPtr hStdError;
        }
        [StructLayout(LayoutKind.Sequential)]
        
public struct PROCESS_INFORMATION
        {
            
public IntPtr hProcess;
            
public IntPtr hThread;
            
public int dwProcessId;
            
public int dwThreadId;
        }
        [DllImport(
"Kernel32.dll", CharSet = CharSet.Ansi)]
        
public static extern bool CreateProcess(
            StringBuilder lpApplicationName, StringBuilder lpCommandLine,
            SECURITY_ATTRIBUTES lpProcessAttributes,
            SECURITY_ATTRIBUTES lpThreadAttributes,
            
bool bInheritHandles,
            
int dwCreationFlags,
            StringBuilder lpEnvironment,
            StringBuilder lpCurrentDirectory,
            
ref STARTUPINFO lpStartupInfo,
            
ref PROCESS_INFORMATION lpProcessInformation
            );
        
#region Win32 Api : WaitForSingleObject
        
//检测一个系统核心对象(线程,事件,信号)的信号状态,当对象执行时间超过dwMilliseconds就返回,否则就一直等待对象返回信号
        [DllImport("Kernel32.dll")]
        
public static extern uint WaitForSingleObject(System.IntPtr hHandle, uint dwMilliseconds);

        
#endregion

        
#region Win32 Api : CloseHandle
        
//关闭一个内核对象,释放对象占有的系统资源。其中包括文件、文件映射、进程、线程、安全和同步对象等
        [DllImport("Kernel32.dll")]
        
public static extern bool CloseHandle(System.IntPtr hObject);

        
#endregion

        
#region Win32 Api : GetExitCodeProcess
        
//获取一个已中断进程的退出代码,非零表示成功,零表示失败。
        
//参数hProcess,想获取退出代码的一个进程的句柄,参数lpExitCode,用于装载进程退出代码的一个长整数变量。
        [DllImport("Kernel32.dll")]
        
static extern bool GetExitCodeProcess(System.IntPtr hProcess, ref uint lpExitCode);
        
#endregion  

/

string argm = "调用的exe程序路径";
        STARTUPINFO sInfo 
= new STARTUPINFO();
        sInfo.lpDesktop 
= "Winsta0\\Default";
         PROCESS_INFORMATION pInfo 
= new PROCESS_INFORMATION();
         
if (!CreateProcess(nullnew StringBuilder(argm), nullnullfalse0nullnullref sInfo, ref pInfo))
         {
                      
throw new Exception("调用失败");
         }
          
uint i = 0;
          WaitForSingleObject(pInfo.hProcess, 
int.MaxValue);
          GetExitCodeProcess(pInfo.hProcess, 
ref i);
          CloseHandle(pInfo.hProcess);
          CloseHandle(pInfo.hThread

转载于:https://www.cnblogs.com/vmyspace/archive/2011/12/07/2278811.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值