2 s110007 s110007 于 2013.10.14 08:49 提问

关于一个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个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.18 23:46
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!