Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Scripting / December 2004

Tip: Looking for answers? Try searching our database.

IDispatch implementation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dmitry Medvedev - 15 Dec 2004 15:11 GMT
Hello,

I need to write C# class, that would implement IDispatch interface to be
used in ActiveScript.
I can't use IReflect interface here due to design issues, I need to handle
GetIDsOfNames and Invoke manually.

I've tried the following code:

[Guid("00020400-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDispatch
{
 [PreserveSig]
 int GetTypeInfoCount(out int Count);
 [PreserveSig]
 int GetTypeInfo( [MarshalAs(UnmanagedType.U4)] int iTInfo,
  [MarshalAs(UnmanagedType.U4)] int lcid,  out UCOMITypeInfo typeInfo);
 [PreserveSig]
 int GetIDsOfNames( ref Guid riid,
  [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)]
string[] rgsNames,
  int cNames,   int lcid,   [MarshalAs(UnmanagedType.LPArray)] int[]
rgDispId);
 [PreserveSig]
 int Invoke(int dispIdMember, ref Guid riid, uint lcid, ushort wFlags,
  ref DISPPARAMS pDispParams, out object pVarResult, ref EXCEPINFO
pExcepInfo, IntPtr[] pArgErr);

[ClassInterface(ClassInterfaceType.None)]
public class ActiveScriptWrapper : IDispatch
....

and
Marshal.GetIDispatchForObject(new ActiveScriptWrapper())  results in
exception.

Looks like it's not possible to implement IUnknown and IDispatch interfaces
in .NET classes.

Could anyone help me with this task?

Regards,
Dmitry
Jayme Pechan - 22 Dec 2004 17:18 GMT
We just add the following code to our component class to expose the thing as
a COM object.  We don't modify it at all.  Just use it as is.  I'm not
certain if this is what you are looking for but it might.

#region COM registration code

[ComRegisterFunction()]

public static void RegisterClass ( string key )

{

// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it

StringBuilder sb = new StringBuilder ( key ) ;

sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open the CLSID\{guid} key for write access

RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);

// And create the 'Control' key - this allows it to show up in

// the ActiveX control container

RegistryKey ctrl = k.CreateSubKey ( "Control" ) ;

ctrl.Close ( ) ;

// Next create the CodeBase entry - needed if not string named and GACced.

RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ;

inprocServer32.SetValue ( "CodeBase" ,
Assembly.GetExecutingAssembly().CodeBase ) ;

inprocServer32.Close ( ) ;

// Finally close the main key

k.Close ( ) ;

}

[ComUnregisterFunction()]

public static void UnregisterClass ( string key )

{

StringBuilder sb = new StringBuilder ( key ) ;

sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open HKCR\CLSID\{guid} for write access

RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);

// Delete the 'Control' key, but don't throw an exception if it does not
exist

k.DeleteSubKey ( "Control" , false ) ;

// Next open up InprocServer32

RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ;

// And delete the CodeBase key, again not throwing if missing

k.DeleteSubKey ( "CodeBase" , false ) ;

// Finally close the main key

k.Close ( ) ;

}

#endregion

> Hello,
>
[quoted text clipped - 41 lines]
> Regards,
> Dmitry

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.