如何在.NET EXE和COM EXE之间进行通信?
问题描述:
我有一个.NET EXE和ATL COM EXE Windows Mobile。我需要在他们之间进行沟通。 即我的ATL EXE启动.NET EXE。 .NET EXE应该向ATL EXE发送一条消息,表明处理已完成或退出。我怎样才能做到这一点?如何在.NET EXE和COM EXE之间进行通信?
如何在两个独立的过程之间进行通信?
答
编辑:
错过了的问题是关于移动。不知道这是否有效。
我使用IPC信道与此辅助类:
static class IpcChannelManager<T> {
/// <summary>
/// Make a type available to other processess
/// </summary>
/// <param name="type">
/// Type to register, must derive from MarshalByRefObject and implement <typeparamref name="T"/>
/// </param>
/// <param name="portName">Name of IpcChannel</param>
public static void RegisterType(Type type, string portName) {
if (!type.IsSubclassOf(typeof(MarshalByRefObject)))
throw new ArgumentException("Registered type must derive from MarshalByRefObject");
Dictionary<string, string> ipcproperties = new Dictionary<string, string>();
ipcproperties["portName"] = portName;
// Get the localized name of the "Authenticated users" group
ipcproperties["authorizedGroup"] = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null).Translate(typeof(NTAccount)).ToString();
ChannelServices.RegisterChannel(new IpcServerChannel(ipcproperties, null), false);
RemotingConfiguration.RegisterWellKnownServiceType(type, typeof(T).Name, WellKnownObjectMode.Singleton);
}
/// <summary>
/// Get a reference to a remoting object
/// </summary>
/// <param name="portName">Name of Ipc port</param>
/// <returns>
/// Reference to remote server object</returns>
public static T GetRemoteProxy(string portName) {
ChannelServices.RegisterChannel(new IpcClientChannel(), false);
return (T)Activator.GetObject(typeof(T), "ipc://" + portName + "/" + typeof(T).Name);
}
然后我使用它是这样的:
一个可用的两侧界面
interface IIpcMessage {
int GetSomeData(string foo);
}
在接收端我做
class IpcMessage : MarshalByRefObject, IIpcMessage {
...
}
IpcChannelManager<IIpcMessage>.RegisterType(typeof(IpcMessage), "Someuniqueportname");
而且在发送端:
IIpcMessage ipcMessage = IpcChannelManager<IIpcMessage>.GetRemoteProxy("Someuniqueportname");
int data = ipcMessage.GetSomeDate("blabla");
+0
这不适用于Windows Mobile/Windows CE – ctacke 2010-01-08 19:57:04
嘿我试过使用窗口消息,它的罚款,如果我们有一个特定名称的单个窗口.. 但如果我们有多个相同名称的窗口? – Naruto 2010-01-08 13:27:36