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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办