简体   繁体   中英

Why can't I make a WinForms app register for COM interop?

I'm using Visual Studio 2015. I have successfully created a class library and selected:

Register form COM Interop
Make Assembly COM-visible

I've then managed to deploy this using a Setup Project to other users and it works great (although still don't understand why Intellisense doesn't work for it)

Now I'm trying to do the same for a WinForms application, but

Register form COM Interop

is not viable to apply, it's grayed out - should it be? And if so, why can't I register an application for COM Interop so I can manipulate it in other apps? (eg in Excel VBA)

The problem is that com registration normally makes only sense for assemblies (.dlls) and your WinForm application is normally a executable (.exe).

So I this case you should create a dll and move all the functions and classes that should be com visible into this assembly. Than make it Com visible and use it also from your executbale WinForms application.

To register a .NET assembly for COM Interop, the assembly has to be a class library , which is intended to be shared by applications.

A WinForms application, on the other hand, runs as a standalone program is not intended to be shared by other applications.

In this case, you can create a class library project, and creates a WinForms UserControl in the project, and expose the UserControl to other apps by making it COM visible.

Here is an old example , strangely it is hard to find more recent examples in my search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM