Can't find Interop dll in project - c#

I'm developing a C# project using Microsoft Word 2007. I add to my project a reference to the Word component. After compiling I can't find Interop dll in the bin. Where can I find Interop dll? I need to copy it.

In the Word Component, expand your project references, find the Interop dll, highlight it, and in the properties window enable Copy Local. After compiling again, you should now see the dll in your output directory.

In a normal configuration, Office has installed the dll (PIA = Primary Interop Assembly) in the GAC. So there's no need for you to distribute it with your project.
With Office 2007 there's always the caveat that the PIAs are not installed by default, so the user's machine might not have them in the GAC. For this reason, Microsoft has provided a redistributable as a download. Your installation can check for the presence of the PIA(s) used in your project and install them (same as you would the .NET Framework).
It's possible to create your own IA using tlbimp.exe (https://msdn.microsoft.com/en-us/library/tw4zwhbe(v=vs.110).aspx) and there are circumstances when you might want to do so. In this case, you would distribute the dll with your project.
Hans mentions the "Embed interop types" option for more recent versions of the .NET Framework. This can be useful, but thorough testing is necessary as this option sometimes gives "unexpected results" (embeds the wrong or incomplete information from the PIA).

Related

Could not load file or assembly C++ DLL from a .Net add-in

I have a .Net add-in and within this I have referenced a DLL I have made in C++/CLI. The DLL was designed against the OpenCV API - so now my .Net application can take advantage of the cool graphics capabilities offered by OpenCV.
The problem occurs when I deploy my add-in to other computers. When the user enacts a part of the program that specifically calls upon my C++ DLL it complains about missing the reference:
I suspect the code does not actually know where the DLLs are located but within my dev environment everything (obviously) works as I will have my environment set up different to your standard build PC.
What am I missing here ?
How can I successfully call DLLs created in C++ from a C# add-in? Bearing in mind add-ins are supposed to simplify the customisation of software like Office etc. This is very important - I have to be able to roll in non-.Net DLLs into my project and my code be able to find them.
My dll is just a plain dll, not a COM compatible dll (maybe it should be?) or should I be decorating my C++ code with __declspec(dllexport) a la https://learn.microsoft.com/en-us/cpp/build/exporting-from-a-dll-using-declspec-dllexport?view=vs-2017
So 2 things
Use Dependancy Walker to identify any dependancies on your dll and the dlls it uses further down the 'tree' hieracrchy. I found 2 that were missing and it wasn't obvious without this useful tool. Don't be overwhelmed with the results it gives you, just take notice of the missing dlls it's complaining about.
Make sure your dll is referenced within your project and not outside of it in some other folder where you built it.
This fixed my problem - in general just make sure your dlls are on the same path as your executable.

Should I deploy Interop.x.dll files with .NET application?

We have a .NET app that consumes COM-objects in different DLLs, also used in the VB6 part of our app. When referencing a COM library, Visual Studio 2012 creates an Interop.x.DLL and references that instead. Should I be distributing Interop.x.DLL from the build machine or regenerating it using some .NET command-line tool? What tool? What is the best practice for deploying a .NET app that references COM?
No, that is not necessary anymore since VS2010 and .NET 4.0. You simply set the Embed Interop Types property of the interop assembly reference to True. The default setting.
With this option in effect, the interop types get copied into your own assembly, as though you had written the [ComImport] declarations yourself by hand. And only the ones you actually use in your code. The feature pays off most for large ones, the Microsoft.Office.Interop assemblies in particular are very large. But of course always handy as well for small components since you don't have to deploy the interop assembly anymore.

Is Microsoft.Office.Interop.Excel in-built with .net framework 4.0

I have referenced "Microsoft.Office.Interop.Excel" library in my application to generate excel with embed interop type set to "True". Build happens fine in Dev machine which has excel installed. My doubt is whether the same application builds fine in the Build machine which does not have excel installed.
Got to know that these COM libraries are installed in GAC assembly. Does this library come by default with .net framework or comes with installation of MS-Office excel?
I'm building my application in .net framework 4.0.
Please clarify.
No, the office COM libraries are not installed by default in the GAC. They are not part of the .Net framework. Office, (or at least all the appropriate dlls) have to exist on the target machine.
Edit:
see MS OFFICE C#: Primary Interop Assemblies
To Answer your qunestions
My doubt is whether the same application builds fine in the Build
machine which does not have excel installed?
Yes it will build fine but if you have implemented any Excel related functions it will not work in your build machine. Interop Assemblies are a way to work with office application from your managed code, So without having Office installed Interop is useless.
Does this library come by default with .net framework or comes with
installation of MS-Office excel?
The PIAs(Primary Interop Assembly) are installed automatically when you install Office on the development computer

Different versions of Enterprise Library in one process

We are developing an add-in for Autodesk Inventor. Our software is a bunch of dll assemblies loaded into Inventor at runtime. We have decided to use the Microsoft Enterprise Library 5.0 for logging and exception handling.
Now we have a problem, because it turns out Inventor 2013 uses Enterprise Library 4.1. When our add-in is loading, it fails to load the proper version of an assembly, because Inventor already has an older version in its Bin directory.
Options we have considered so far:
During deployment of our product, overwrite the old libraries in Inventor's Bin folder
Use EL 4.1 in our assemblies
Both are bad and I'm running out of ideas, so I'm asking for help.
Option 1 raises this question: is the Enterprise Library backwards compatible and will replacing those DLL's in the Bin folder cause problems? I have tried it, Inventor doesn't complain and works as expected (haven't checked the EL functionality).
Option 2 makes us use the older version and binds us to the version Autodesk is using, so we would have to watch when they upgrade, especially when they release a new version of Inventor.
What is the best practice in this scenario?
UPDATE:
We solved this by just putting the newer version of Enterprise Library in GAC. I think what happened here was that .NET tried loading the older version first (because it was higher in assembly search order) and after failing never went any further to look for the proper version. When in GAC, it correctly resolves.
From what I can see, a reasonable solution would be to embed the assemblies and access them using the ResourceManager class, this would allow you to use the newer versions whilst maintaining the parent projects logging mechanism.
You might find this question useful:
Embedding assemblies inside another assembly

How to statically link libraries for a C# ClassLibrary?

I am creating a Class LLibrary in c# by using microsoft provided Dll's.
Now i want to statically add those Microsoft provided libraries to My Dll.How can i do this.
I have simply added a reference to those Microsoft provided Dlls and creating My Dll? Is it fine or not?
if Microsoft provided dll is not available on other machine then my Dll may fails i need to add the libraries statically??
How can i do this??
There's no such thing as statically linking to another assembly in .NET. There are some third party products such as .NET linker that merge assemblies into one but they are unsupported.
If you have the redistribution license for that library, you can ship a copy along with your assembly. In Visual Studio you can make this happen by setting "Copy Local" to "True" in the properties window for that assembly reference.
See discussion here and read the comments -- Jeff does provide a way.
http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
If the dll is not available at execution time; yes it will fail. However:
many Microsoft dlls are pre-installed with .NET (caveat: "client profile")
many of the Microsoft dlls are redistributable; so you can include them with your package
There isn't a linker provided in the core framework, although ILMerge may be useful.
Its not very clear what you want to achieve but it seems you are concerned that your class lib will work on some other machine or not. The thing is that the .Net framework is a free redistributable which should be installed if not present on the target machine. With the .Net framework already installed on a machine, there should be no problem as such.
Static linking as such does not make sense in .Net other that adding an assembly reference to your project. Hope it helps

Categories