Accessing a C# COM control from C++  
Author Message
Metabug





PostPosted: Visual C# General, Accessing a C# COM control from C++ Top

Hi all,

I wrote a very simple user control in C#, and I'm trying to access it from my C++/MFC code. The control contains a single label in it. Here's the control code itself:

namespace HelloCom {

[Guid("F0AE1BB2-A840-4341-B785-D8A54C81E643")]
public interface HelloCom {

}

[Guid("111DCC11-900D-431c-B3A0-4B95C539A47D")]
public partial class HelloComImpl : UserControl, HelloCom {
public HelloComImpl() {
InitializeComponent();
}
}
}

Here's what I've done so far:

* Signed my control with a strong key
* Clicked the "expose assembly to COM interop" and "make visible to COM" options
* regasm HelloCom.dll /tlb:HelloCom.tlb
* gacutil -i HelloCom.dll

However, despite all this, I still cannot see my control in the Toolbox on the C++ side, nor can I insert it via the "Insert ActiveX Control" menu option. What am I doing wrong



Visual C#16  
 
 
jmsigler2





PostPosted: Visual C# General, Accessing a C# COM control from C++ Top

Metabug:

Try using these attributes:

namespace HelloCom {

[System.Runtime.InteropServices.GuidAttribute("F0AE1BB2-A840-4341-B785-D8A54C81E643")]

[System.Runtime.InteropServices.InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)]

[System.Runtime.InteropServices.ComVisibleAttribute(true)]

public interface HelloCom {

}

[System.Runtime.InteropServices.GuidAttribute("111DCC11-900D-431c-B3A0-4B95C539A47D")]

[System.Runtime.InteropServices.ProgIdAttribute("HelloCom.HelloComImpl")]

[System.Runtime.InteropServices.ClassInterfaceAttribute(ClassInterfaceType.None)]

[System.Runtime.InteropServices.ComVisibleAttribute(true)]

public partial class HelloComImpl : UserControl, HelloCom {
public HelloComImpl() {
InitializeComponent();
}
}

Your assembly file contains a global attribute for making COM objects visible. Likely yours is set to false. Above, you see that you can override the gobal setting on a per class or on a per method basis. The per method is a feature similar to the old VB native case where methods starting with an underscore were hidden from intellisense.

I hope this solves your problem.

James Sigler, 11-01-2006

Dallas, TX