60149

write a com control in c# and use it in MFC

Question:

if this possible to write a com control or activex in C# and use it in MFC ?

Answer1:

Yes. First, you need to create COM object. Below is a very simple example.

[Guid("123565C4-C5FA-4512-A560-1D47F9FDFA20")] public interface IDoSomething { [DispId(1)] string Name { get; } [DispId(2)] int DoSomething(); } [ComVisible(true)] [Guid("12AC8095-BD27-4de8-A30B-991940666927")] [ClassInterface(ClassInterfaceType.None)] public sealed class DoSomething: IDoSomething { public DoSomething() { } public string Name { get { return ""; } } public int DoSomething() { return 4; //random number } }

After that you need to regasm your assembly. The regasm tool will add the necessary registry COM entries:

regasm.exe /tlb component.dll

/tlb is necessary to generate the type library to be imported in your MFC application.

Once your assembly is registered, you can call DoSomething in your MFC application like any other COM objects.

Check this <a href="http://www.codeproject.com/KB/COM/com_object_in_c_.aspx" rel="nofollow">link</a> for more information.

Answer2:

This is a bit outside my normal territory, as I don't interact with .NET Interop technology all that much.

It is possible to create what's called a COM Callable Wrapper around your C# control/class to make it accessible to any COM-aware program. I won't duplicate Francis B's answer because it's fairly complete as it stands.

The big question is whether a visual C# control works seamlessly within an MFC window. That's not something I can answer, but my best advice would be to prepare for a bumpy road ahead. Please see <a href="http://bytes.com/topic/net/answers/430618-c-control-mfc-window-frame" rel="nofollow">http://bytes.com/topic/net/answers/430618-c-control-mfc-window-frame</a> for more detail.

Recommend

  • Parsing .txt file in C
  • Adding support for dynamic file loaders in c#
  • Why cannot I open links in an external browser in a WebView (UWP)?
  • How to Mock NamespaceManager Class Methods Using Moq?
  • Multi-key dictionary in c#? [duplicate]
  • Register type library x64 regtlib
  • Uncaught TypeError: Immutable prototype object '#' cannot have their prototype set
  • Is it possible to create an Attached Property on a Sealed XAML control class?
  • How do you upgrade Settings.settings when the stored data type changes?
  • VBA and “Class doesn't support Automation or does not support expected interface”
  • Finding all possible paths
  • Scala really weird Type Mismatch
  • Labeled ComboBox in Windows 10 Universal App
  • Make clickable UI-elements for windows Phone 8.1 apps maps
  • SQLite.Net Won't Create In Win IoT Library
  • I want to handle a clickbutton event from another page
  • Foreign key or null value
  • Join 2 tables on dynamically changing column
  • C# COM Class - DISP_E_UNKNOWNNAME
  • How to register UDF/RTD within VSTO project
  • C# WPF Datagrid doesn't dynamically sort on data update
  • HostingEnvironment.QueueBackgroundWorkItem uses ASP.Net thread pool or another thread pool?
  • import vb.net class library to vb6 project
  • Singleton Alternative - is it equivalent?
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Scrapy recursive link crawler
  • Is there a javascript serializer for JSON.Net?
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Where to put my custom functions in Wordpress?
  • How to handle AllServersUnavailable Exception
  • Buffer size for converting unsigned long to string
  • Java static initializers and reflection
  • unknown Exception android
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • How to push additional view controllers onto NavigationController but keep the TabBar?