s110007 2013-10-14 00:49 采纳率: 0%
浏览 1843

关于一个windows service的问题

下面一段是winform的代码,能否转换成Windows service的吗?
Main.cs
public partial class Main : Form
{
private readonly InterfaceDll _interface;

    public Main()
    {
        InitializeComponent();
        _interface = new InterfaceDll(Handle, Application.StartupPath);
        _interface.Call_DLL_Net_Init();
        _interface.InitCallBackFunc();
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case 2000:
                switch ((int)(m.WParam))
                {
                    case (int)ENUM_ERROR.ENUM_COMMAND_GET_SYSTEM_STATUS_ENABLE_SEND_BY_NET:

                        break;
                    case (int)ENUM_ERROR.ENUM_COMMAND_SEND_PROGRAM_SUCCESS_BY_NET:

                        break;
                    case (int)ENUM_ERROR.ENUM_COMMAND_STATISTICS_PROGRESS_BY_NET:

                        break;
                }
                break;
        }
        base.WndProc(ref m);
    }
}

InterfaceDll.cs

unsafe class InterfaceDll
{
    public InterfaceDll(IntPtr handle, string strAppPath)
    {
        sIntHandle = handle;
        m_CurAppStartPath = strAppPath;
    }
    public string m_CurAppStartPath = "";

    #region delegate callback function
    //回调函数 处理动态库返回的信息
    //  public delegate void CALLBACK_PROC(int iType, StringBuilder buffer);
    public delegate void CALLBACK_PROC(int iType, char* buffer);
    CALLBACK_PROC myCallbackFunc = new CALLBACK_PROC(ProcessCallBackMessage);
    #endregion


    #region SendMessage to wnd function declaration
    //回调函数中发消息到FORM1窗口
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(int hWnd, int Msg, int wParam, IntPtr lParam);
    [DllImport("User32.dll")]
    private static extern int FindWindow(string strClassName, string strWindowName);

    #endregion

    #region callback function

    public static void ProcessCallBackMessage(int iType, char* buffer)
    {
        int h1 = (int)sIntHandle;//0;

        h1 = FindWindow(null, "WindowsService");
        if (h1 != 0)
            SendMessage(h1, 2000, iType, (IntPtr)buffer);
    }
    #endregion

}

}

  • 写回答

1条回答

  • shiter 人工智能领域优质创作者 2015-01-18 15:46
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?