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

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。