我的解决方案
- 如果有人知道更好的方法来做到这一点,我很想知道。
- 我在向我的 COM 接口添加事件时仍然遇到问题。我想添加
public event ComEvent MyApplicationClose;
内部MyApplication
以在关闭时调用。
编辑
我能够使用Managed Event Sinks让事件正常工作。编辑反映在下面的代码中。
助手类
namespace ole32
{
public class Ole32
{
[DllImport( "Ole32.Dll" )]
public static extern int CreateBindCtx( int reserved, out IBindCtx
bindCtx );
[DllImport( "oleaut32.dll" )]
public static extern int RegisterActiveObject( [MarshalAs( UnmanagedType.IUnknown )] object punk,
ref Guid rclsid, uint dwFlags, out int pdwRegister );
[DllImport( "ole32.dll", EntryPoint = "GetRunningObjectTable" )]
public static extern int GetRunningObjectTable( int reserved, out IRunningObjectTable ROT );
[DllImport( "ole32.dll", EntryPoint = "CreateItemMoniker" )]
public static extern int CreateItemMoniker( byte[] lpszDelim, byte[] lpszItem, out IMoniker ppmk );
/// <summary>
/// Get a snapshot of the running object table (ROT).
/// </summary>
/// <returns>A hashtable mapping the name of the object
// in the ROT to the corresponding object</returns>
public static Hashtable GetRunningObjectTable()
{
Hashtable result = new Hashtable();
IntPtr numFetched = IntPtr.Zero;
IRunningObjectTable runningObjectTable;
IEnumMoniker monikerEnumerator;
IMoniker[] monikers = new IMoniker[1];
GetRunningObjectTable( 0, out runningObjectTable );
runningObjectTable.EnumRunning( out monikerEnumerator );
monikerEnumerator.Reset();
while ( monikerEnumerator.Next( 1, monikers, numFetched ) == 0 )
{
IBindCtx ctx;
CreateBindCtx( 0, out ctx );
string runningObjectName;
monikers[0].GetDisplayName( ctx, null, out runningObjectName );
object runningObjectVal;
runningObjectTable.GetObject( monikers[0], out runningObjectVal );
result[runningObjectName] = runningObjectVal;
}
return result;
}
}
}
我在 ROT 中注册的应用程序
我的应用程序将充当数据服务器。它接收和处理来自多个来源的数据。对这些数据的访问是通过 COM 公开的。只有一个 MyApplication 实例减少了与外部数据源和处理的连接的冗余,同时允许多个客户端使用它获得的数据。
namespace MyNamespace
{
[ComVisible( true ),
GuidAttribute( "14C09983-FA4B-44e2-9910-6461728F7883" ),
InterfaceType( ComInterfaceType.InterfaceIsDual )]
public interface ICOMApplication
{
[DispId(1)]
int GetVal();
}
//Events for my com interface. Must be IDispatch
[Guid( "E00FA736-8C24-467a-BEA0-F0AC8E528207" ),
InterfaceType( ComInterfaceType.InterfaceIsIDispatch ),
ComVisible( true )]
public interface ICOMEvents
{
[DispId( 1 )]
void ComAppClose( string s );
}
public delegate void ComEvent( string p );
[ComVisible(true)]
[Guid( "ECE6FD4C-52FD-4D72-9668-1F3696D9A99E" )]
[ComSourceInterfaces( typeof( ICOMWnEvents) )]
[ClassInterface( ClassInterfaceType.None )]
public class MyApplication : ICOMApplication, IDisposable
{
//ICOMEvent
public event ComEvent ComAppClose;
protected MyApplication ()
{
//check if application is registered.
//if not registered then register the application
if (GetApiInstance() == null)
{
Register_COMI();
}
}
// UCOMI-Version to register in the ROT
protected void Register_COMI()
{
int errorcode;
IRunningObjectTable rot;
IMoniker moniker;
int register;
errorcode = Ole32.GetRunningObjectTable( 0, out rot );
Marshal.ThrowExceptionForHR( errorcode );
errorcode = BuildMoniker( out moniker );
Marshal.ThrowExceptionForHR( errorcode );
register = rot.Register( 0, this, moniker );
}
public void Dispose()
{
Close( 0 ); //close and clean up
}
//Will look for an existing instance in the ROT and return it
public static ICOMApplication GetApiInstance()
{
Hashtable runningObjects = Ole32.GetRunningObjectTable();
IDictionaryEnumerator rotEnumerator = runningObjects.GetEnumerator();
while ( rotEnumerator.MoveNext() )
{
string candidateName = (string) rotEnumerator.Key;
if ( !candidateName.Equals( "!MyNamespace.ICOMApplication" ) )
continue;
ICOMApplication wbapi = (ICOMApplication ) rotEnumerator.Value;
if ( wbapi != null )
return wbapi;
//TODO: Start the application so it can be added to com and retrieved for use
}
return null;
}
//Builds the moniker used to register and look up the application in the ROT
private static int BuildMoniker( out IMoniker moniker )
{
UnicodeEncoding enc = new UnicodeEncoding();
string delimname = "!";
byte[] del = enc.GetBytes( delimname );
string itemname = "MyNamespace.ICOMApplication";
byte[] item = enc.GetBytes( itemname );
return Ole32.CreateItemMoniker( del, item, out moniker );
}
protected void Close( int i )
{
//Deregistering from ROT should be automatic
//Additional cleanup
if (ComAppClose != null) ComAppClose("");
}
~MyApplication()
{
Dispose();
}
//implement ICOMApplication interface
private static int i = 0;
public int GetVal()
{
return i++; //test value to return
}
}
}
注意:这包含一个用于注册程序集的 Post-build 事件
C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe $(TargetFileName) /codebase /tlb:$(TargetName)TypeLib.tlb
通过 COM 使用 MyApplication
因为它是通过 COM 的,所以它应该适用于任何 COM 语言,但是我只尝试过 c#。最初,此代码将被放入 RTDserver for excel。这将允许用户构建复杂的工作表,利用我的应用程序中的实时数据。
首先,我在 dot net 中为我的 COM 对象创建一个包装器。
namespace COMTest
{
//extend both the com app and its events and use the event sink to get the events
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public sealed class MyAppDotNetWrapper, ICOMEvents, ICOMApplication
{
private ICOMApplication comapp;
public MyAppDotNetWrapper()
{
StartEventSink()
}
//Manage the sink events
private void StartEventSink()
{
//get the instance of the app;
comapp = MyApplication .GetApiInstance();
if (comapp != null)
{
serverconnected = true;
//Start the event sink
IConnectionPointContainer connectionPointContainer = (IConnectionPointContainer) comapp;
Guid comappEventsInterfaceId = typeof (ICOMApplicationEvents).GUID;
connectionPointContainer.FindConnectionPoint(ref comappEventsInterfaceId, out connectionPoint);
connectionPoint.Advise(this, out cookie);
}
}
private void StopEventSink()
{
if (serverconnected)
{
//unhook the event sink
connectionPoint.Unadvise(cookie);
connectionPoint = null;
}
}
//Implement ICOMApplication methods
public int GetVal()
{
return comapp.GetVal();
}
//receive sink events and forward
public event ComEvent ComAppCloseEvent;
public void ComAppClose(string s)
{
serverconnected = false;
ComAppCloseEvent(s);
}
private ICOMApplication comapp;
IConnectionPoint connectionPoint;
private int cookie;
private bool serverconnected;
}
}
现在我可以在我的 .net 应用程序中使用我的包装器
namespace COMTest
{
class Program
{
private static MyAppDotNetWrapper app;
static void Main( string[] args )
{
//create a new instance of the wrapper
app = new MyAppDotNetWrapper();
//Add the onclose event handler
app.ComAppCloseEvent += OnAppClose;
//call my com interface method
Console.WriteLine("Val = " + app.GetVal().ToString());
Console.WriteLine("Val = " + app.GetVal().ToString());
string s = Console.ReadLine();
}
static voic OnClose(string s)
{
Console.WriteLine("Com Application Closed.");
}
}
}
输出在哪里
Val = 1
Val = 2
关闭 COM 服务器应用程序后,您会看到关闭消息。
Val = 1
Val = 2
Com Application Closed.
参考
运行对象表:.NET 中的提供者,MFC 中的消费者
使用 C# 自动化 Visual Studio .NET 的特定实例
在 ROT 中注册一个对象