简体   繁体   中英

COM Interop in .NET

I have a C# Visual Studio 2008 project and I want use a COM interop XXXXLib (XXXX.dll).

Of course I have to add reference to %windir%\\system32\\xxxx.dll before, and VS will add Interop.xxxx.dll to the project folder, and now I have to distribute this 200 KB library with my simple 4 KB application.

But, now I know that PCs don't have XXXXLib or don't want use it. And in my application .NET needs check if exists COM in clients.

If COM does not exist on client's PCs, I want my application not to fail. I think I need not to reference the Interop, and use reflection for loading the interop.xxxx.dll, or another solution.

Other secondary issue, how do I check if COM exists in on client computers?

Do you have any good sample code?

I ask for gurus, for the best solution and I will give all my love to gurus.

Instead of using the code generated by Visual Studio (that may always be executed) you should do the same conditionally in your own code. Transform your code to do so.

I have used such an approach to only require one out of three COM components (from three different vendors) to be installed on the client computer (COM components for spectrum display for use with mass spectrometry data).

The code I use can found here , near "Add spectrum control, but only the one corresponding ...". The function addSpectrumControl() can be found here .

You can check for existence of a COM component installed on the client computer by wrapping the call to creating an instance of the COM component in a try-catch block. If it fails an exception will be thrown that will then be catched in the catch block. You then know it is not installed (or not registered or some other error). In any case you then know it can not be used.

You should distribute that dll with your app, make sure to register it properly if necessary. IF your app can work without that dll, than try to load it, and if fails... continue with execution. if not.. you must install it with your app.

The best solution is to add is to add xxxx.dll in your distribuation package and work with it.

If this is an approach that is ok to you - you can try wix to make the installer.

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