【发布时间】:2014-01-02 15:29:26
【问题描述】:
我有这个大问题,我无法解决它。
我创建了一个 ActiveX dll 组件。它在控制台应用程序中使用,它创建它的一个实例并在托盘中运行。代码如下:
class Program
{
public static string MSG;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static NotifyIcon tray = new NotifyIcon();
static void Main(string[] args)
{
Console.Title = "ActiveXConsole";
Program program = new Program();
IntPtr hWnd = FindWindow(null, "ActiveXConsole");
if (hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, 0);
}
tray.Icon = new Icon("App.ico");
tray.Visible = true;
tray.Text = "ActiveXConsole";
tray.ShowBalloonTip(3000, "ActiveXConsole", "ActiveXConsole working...", ToolTipIcon.None);
program.ActivateActiveX();
}
public void ActivateActiveX()
{
ActiveXdll.Class1 activex = new ActiveXdll.Class1();
activex.OnMessage += new EventHandler<NewMessageArgs>(ProcessMessage);
//this activates the StartListen method that is listening for a UDP message.
//the event is created in it
activex.StartListen();
}
public void ProcessMessage(object sender, NewMessageArgs args)
{
IntPtr hWnd = FindWindow(null, "ActiveXConsole");
MSG = args.Message;
tray.ShowBalloonTip(3000, "ActiveXConsole", "UDP Message has arrived", ToolTipIcon.None);
}
}
}
DLL 实际上在某个端口上接收 UDP 字符串并将其“翻译”为事件,该事件在控制台应用程序中进行处理,如上面的代码所示。
到目前为止效果很好,但我被堆叠了。现在我需要制作监听这些事件的 Web 应用程序。我不能直接在 Web 应用程序中使用 DLL 的原因是,如果激活它的选项卡被打开多次(因此将多次激活它),UDP 套接字侦听器将产生一些冲突,因为不超过同一个端口可以同时打开一个socket。
我知道有一种方法可以做到这一点,因为我在其他软件中看到过它,但是怎么做呢?最好的方法是什么?基本上,控制台应用程序创建 ActiveX DLL 的实例,并且在那里引发的事件由 Web 应用程序侦听,该应用程序在触发时会执行某些操作。
我们正在使用 .NET 2.0 框架,公司政策。不要问我为什么我们不迁移到更新的东西。
【问题讨论】:
-
我不知道为什么你会为此使用 ActiveX。
-
不是我的选择,我被命令这样做。由于 ActiveX 快要死了,我想用 Web 服务或任何其他方式来实现它,但如果他们告诉你必须这样做,那么......
标签: c# events visual-studio-2005 activex .net-2.0