代码写的有问题,baidu一下,

解决方案 »

  1.   

    理论上,windows service与桌面是不能交互的,这是微软的安全策略。所以建议楼主想想别的办法。以下的解决办法貌似到win7也不能使用了。我也用到了与楼主类似的场景,我使用的是计划任务。winXP的解决办法: 请参见 http://kb.cnblogs.com/page/44767/ 
      

  2.   

    本地同机的程序可采用进程间通信等方法实现数据等的交互,远程的局域网内有MSMQ等可选
      

  3.   

    我的服务安装的代码是:(安装服务)
    string[] cmdLine = { };
    AssemblyInstaller aiService = new AssemblyInstaller(strServicePath, cmdLine);
    TransactedInstaller transactedInstaller = new TransactedInstaller();
    transactedInstaller.Installers.Add(aiService);
    transactedInstaller.Install(new Hashtable());
    transactedInstaller.Commit(null);在ServInstaller_AfterInstall函数中(设置与桌面交互)
    string strServiceName = "MyServName";
    IntPtr iManageHandle = OpenSCManager(null, null, c_iSC_MANAGER_ALL_ACCESS);
    if (iManageHandle != IntPtr.Zero)
    {
        IntPtr iServiceHandle = OpenService(iManageHandle, strServiceName, c_iSERVICE_ALL_ACCESS);
        if (iServiceHandle != IntPtr.Zero)
        {        ChangeServiceConfig(
                iServiceHandle,
                c_iSERVICE_WIN32_OWN_PROCESS | c_iSERVICE_INTERACTIVE_PROCESS,
                c_iSERVICE_NO_CHANGE,
                c_iSERVICE_NO_CHANGE,
                null,
                null,
                IntPtr.Zero,
                null,
                null,
                null,
                null);     CloseServiceHandle(iServiceHandle);
       }
         CloseServiceHandle(iManageHandle);
    }    ServiceController scService = new ServiceController(strServiceName);
        
        scService.Start();
        scService.WaitForStatus(ServiceControllerStatus.Running);服务中消息发送的代码是:
    [DllImport("user32.dll", EntryPoint = "SendNotifyMessage", SetLastError = true)]
            public static extern uint SendNotifyMessage(IntPtr hWnd, uint iMsg, IntPtr hWParam, IntPtr hLParam);SendNotifyMessage(hWindowsHandle, 0x403, (IntPtr)iCommandId, IntPtr.Zero);画面程序接收消息代码是:
    protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case 0x0403:
                        Form2 frm = new Form2();
                        frm.ShowDialog();
                        //Application.Exit();
                        break;
                    default:
                        break;
                        // let the base class deal with it
                        base.WndProc(ref m);
                        break;
                }
            }请帮忙看看,代码问题在哪里?我怀疑是安装时少做了一件什么事情,可是又找不到
    谢谢!
      

  4.   

    找到一个方法,其实以前也试过,但后来没确认
    先调用RegisterWindowMessage("MyMessageName");
    注册消息ID,然后两边使用这个ID通信,
    但是有一个限制,只有第一个登陆的桌面程序能够取得与服务相同的ID,
    其他用户的ID取得不到