Здравствуйте, Evgeny Elmanov, Вы писали:
EE>Кто-нибудь сталкивался с использованием объектов .NET в COM? В MSDN есть очень скудная информация на эту тему и нет примеров. Конечная задача сделать .NET объекты доступными в MSScriptControl.
посмотри инфу в форуме .NET, там много было
например для
PC следующий СОМ-объект:
— синглетон TheOne реализуется через GIT
— WriteObjectType — для проверки какой же реальный тип передается СОМом
с событиями все немного сложнее, там есть Диспатчовые клиенты(Delphi, JScript), а есть IUnknown(ATL) потому реализуешь два интерфейса событий (и соотвествующие делегаты) и остальное можно найти в том форуме
в данном случае будут полезные свойства для СОМ, в МСДНе найдешь, что за что они отвечают
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Diagnostics;
namespace Simple
{
[ComImport]
[InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
[Guid( "00000146-0000-0000-C000-000000000046" )]
public interface IGlobalInterfaceTable
{
int RegisterInterfaceInGlobal( IntPtr pUnk, ref Guid riid, ref int cookie);
int RevokeInterfaceFromGlobal( int cookie);
int GetInterfaceFromGlobal( int cookie, ref Guid riid, ref IntPtr pu);
}
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid( "7AB2D089-6200-4833-A038-98E56A61882C" )]
public interface ISimpleObject
{
string Id { get; }
ISimpleObject TheOne { get; }
void WriteObjectType( object obj );
}
[ComVisible(true)]
[Guid( "F66873C5-4F1E-4294-AC7E-B373ADDE1046" )]
[ComDefaultInterface( typeof( ISimpleObject ) )]
[ClassInterface( ClassInterfaceType.None )]
public class SimpleObject : ISimpleObject
{
static readonly Guid
CLSID_StdGlobalInterfaceTable = new Guid( "{00000323-0000-0000-C000-000000000046}"),
IID_GlobalInterfaceTable = typeof(IGlobalInterfaceTable).GUID,
IID_ISimpleObject = new Guid( "{7AB2D089-6200-4833-A038-98E56A61882C}" );
static readonly object
SyncRoot = "sync-root";
public SimpleObject()
{
Debug.WriteLine( "GIT: SimpleObject" );
}
static IGlobalInterfaceTable getGit()
{
object o = Activator.CreateInstance( Type.GetTypeFromCLSID( CLSID_StdGlobalInterfaceTable ) );
Debug.WriteLine( string.Format( "GIT: obj={0}", o ) );
return (IGlobalInterfaceTable)o;
}
#region ISimpleObject Members
string
_id = Guid.NewGuid().ToString();
public string Id
{
get { return _id; }
}
static int
s_cookie = 0;
public ISimpleObject TheOne
{
get
{
lock (SyncRoot)
{
try
{
Debug.WriteLine( string.Format( "GIT: cookie = {0}", s_cookie ) );
if (s_cookie != 0)
{
IGlobalInterfaceTable git = getGit();
IntPtr pu = IntPtr.Zero;
Debug.WriteLine( "GIT: HERE" );
Guid riid = IID_ISimpleObject;
int herr = git.GetInterfaceFromGlobal( s_cookie, ref riid, ref pu );
Marshal.ReleaseComObject( git );
Debug.WriteLine( string.Format( "GIT: GetInterfaceFromGlobal herr = {0:X00000000}", herr ) );
if (herr < 0)
s_cookie = 0;
else
return (ISimpleObject)Marshal.GetTypedObjectForIUnknown( pu, typeof( ISimpleObject ) );
}
if (s_cookie == 0)
{
IGlobalInterfaceTable git = getGit();
Guid riid = IID_ISimpleObject;
int herr = git.RegisterInterfaceInGlobal( Marshal.GetIUnknownForObject( this ), ref riid, ref s_cookie );
Marshal.ReleaseComObject( git );
Debug.WriteLine( string.Format( "GIT: RegisterInterfaceInGlobal herr={0:X00000000} cookie={1}", herr, s_cookie ) );
if (herr < 0)
{
s_cookie = 0;
Marshal.ThrowExceptionForHR( herr );
}
return this;
}
}
catch (Exception err)
{
Debug.WriteLine( string.Format( "GIT: ERROR {0}", err ) );
throw;
}
}
return null;
}
}
public void WriteObjectType( object obj )
{
Console.WriteLine( obj.GetType().ToString() );
}
#endregion
}
}