下面一段是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
}
}