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 / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Make .NET methods visible to COM

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Janiek Buysrogge - 28 Aug 2006 10:26 GMT
Hello,

I followed the instructions on
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/c
allnetfrcom.asp

to call .NET class methods from the ActiveX Test Container
(tstcon32.exe) and I can load my control, but there aren't any methods
I can invoke.

However, in ActiveXplorer I can view the methods on the component
(screenshot:
http://idaho.cybernited.be/upload/televic/ActiveXplorer.JPG), but this
tool doesn't support invoking them.

Am I forgetting somehting ?

Any help appreciated.

Here's my code:

the interface:

using System;
using System.Collections.Generic;
using System.Text;

namespace COMVisibleMethods
{
   public interface ICOMCallable
   {
       int SumOfTwo(int a, int b);
       string GetCurrentDateTime();
   }
}

the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.Win32;

namespace COMVisibleMethods
{
   public partial class Math : UserControl, ICOMCallable
   {
       public Math()
       {
           InitializeComponent();
       }

       public string GetCurrentDateTime()
       {
           return DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();
       }

       public int SumOfTwo(int a, int b)
       {
           return a + b;
       }

       [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();
       }
   }
}

-
JB
Ignacio Machin ( .NET/ C# MVP ) - 28 Aug 2006 14:06 GMT
Hi,

Usually I decorate my COM's visible classes with:
[ClassInterface(ClassInterfaceType.None)]

[Guid("A0F6A88F-F20E-445f-8F80-95EA780AAB52")]

Not sure if this is what you are missing though.

Signature

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

> Hello,
>
[quoted text clipped - 123 lines]
> -
> JB

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.