How to make .dll available for C# and Excel VBA - c#

I have a DLL that I want to use in C# projects as well as Excel VBA. I used regasm.exe to register the DLL and create a TLB . I was now able to reference it in VBA. My problem is, that I would need to my C# apps still can't find the DLL. I would have to add either a copy of the DLL or a .config - file in the folder of each C# app for them to work. I can see the reference in Visual Studio under COM, but get an error when trying to use that reference (something Active-X related, I can look it up if it's important).
So I tried using gacutil.exe (unregistered first with regasm.exe) and registered my DLL in GAC. Now I don't need the copy or .config, my C# apps work again. Excel VBA still needs the registration with regasm.exe though, I can't add a refrence after only using gacutil.exe.
Here my question: How do you register a DLL when you want to use it for C# apps AND Excel VBA? Is it possible to use both regasm and gacutil without having any problems?

After running regasm you can then use gacutil /i to install the assembly into the GAC. Once this is done, add a reference to the assembly to your .NET project using the Browse tab. Once the reference is added, locate the reference in the project and view the properties for that reference. Change "Copy Local" to "False".
At run time your .NET project will look for the assembly locally and, when it cannot find it, will then check the GAC. Assuming you've run the gacutil command it will then locate it there.

A somewhat different approach is to incorporate your C# library in an Excel-DNA .xll add-in. This allows you to make the library available to VBA in Excel without registration or requiring admin rights, and the same add-in can include worksheet UDF functions, ribbon customization etc..
A detailed write-up of exposing C# libraries to Excel VBA via Excel-DNA by Mikael Katajamäki can be found here:
http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna-no.html and
http://mikejuniperhill.blogspot.com/2014/03/interfacing-c-and-vba-with-exceldna_16.html.

Related

Regasm is not working / ActiveX component can't create object

I wrote a C# .dll in Visual Studio which I want to call from excel VBA. (I used this tutorial)
On my Development PC where I set the settings in the Project properties for "Make COM-Visible", "Register for COM interop" and "sign the assembly" it all works fine. The dll is getting registered automatically by Visual Studio and I can select and set it in Excel in VBA Window > Tools > References
The use case now is that I can use that dll file on another PC which does not have Visual Studio installed.
As told in the tutorial and all around the internet the way to go is by using the command line tool RegAsm.exe, what I did (if its useful: I used the one in the following folder: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe)
The command to register the dll is:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe D:\Debug\DotNetLibrary.dll /tlb /codebase.
This runs without an error and the command line tells me that the types have been registered correctly and the tlb has been exported and registered.
It seems like everything worked well and there is also an entry in the registry (Computer\HKEY_CLASSES_ROOT\TypeLib) which references my dll but the library doesn't show up in the Reference window in VBA like it did on the other PC.
If I select the tlb, which has been generated by the RegAsm manually, by clicking browse in the Reference Window, it gets added to the list. But if I then start the code I get the Error that the ActiveX component can't create an object.
I'm inspecting this issue since several days now, so it would be very nice if some one could assist me with it.
As #HansPassant mentioned in an comment under my question the simple solution was to use the 32-bit version of regasm (located in C:\Windows\Microsoft.NET\Framework\v4.0.30319)
This needs to be done because Microsoft Office products are still 32-bit.

microsoft.office.interop.word not showing up in add references

I am working on a C# project which requires reading of a word file.
I have microsoft.office.interop.word in my machine's Global Assembly Cache but still it would NOT show up in Visual Studio's Project reference list.
Any thoughts?
There is a browse button in the Add References dialog that lets select an arbitrary .NET dll to add as a reference. Since you have already found the dll in the GAC, you can just copy the path into that dialog and add from there.
While it has been a while since I have worked with the interop with Office via a .NET program, there is also the COM tab in the dialog and I believe that should have a reference to Word. That may be the one that you are supposed to use.

Creating class library in VS2013 and registering the .tlb so it can be referenced from a VB6 app

I created a COM class library using my VS2013. I went into the library properties and Compile -> Register for COM interop is checked. I then built the library and see the .tlb file that it generated. At this point I am wondering if there is anything additional that I have to do with the library properties.
I then used RegAsm sample.dll tbl:/sample.tbl
Would the next step be to simply reference the tbl from my VB6 app? I keep getting an error when trying to use the CreateObject function: 429 ActiveX Component can't create object
If my COM library is created properly with GUIDS/Settings based on tutorials and I registered it with RegAsm is there anything that I could be missing?
I had the same problems registering a COM-Interop-DLL. I finally used the InstallShield limited edition setup project to get it running. It's free and replaces the old MS setup project. You need to register to download the software and get a licence.
In the tab Application Files add the needed files. In the properties of the files / tab COM & .NET Settings set the check box COM Interop (this is what I'done) and then create the output.
This is what worked for me.
Christian

Windows Azure not finding DLL of C++/CLI project

I have a C++/CLI project that wraps around an unmanaged C compression library, and this project is referenced by an MVC3 project that calls the C++ Compress function.
Everything works fine locally, but when I publish the solution to the Azure cloud, I get an error saying it could not find the module/dll:
Could not load file or assembly 'LZGEncoder.DLL' or one of its dependencies. The specified module could not be found.
Why can't it find the DLL file? is it going to the wrong place or being compiled at all? Is there any way I can check? Thanks!
The problem was that the Visual C++ 2010 Runtime libraries were missing from the cloud side.
What I did was add the Visual C++ 2010 Redistributable package to the project, along with a script to silently install it at start up, and now the native dll's work. You also need this if you're using native C dll's.
Steps:
1) Download Visual C++ 2010 Redistributable Package, and add it to your project.
2) Create a new batch file and add this to it:
vcredist_x64.exe /q /norestart
exit /b 0
3) Open the ServiceDefinition.csdef file and add this under the relevant WebRole element:
<Startup>
<Task commandLine="InstallVCRedist.bat" executionContext="elevated" taskType="simple" />
</Startup>
UPDATE:
Visual C++ 2012 is out and the same script works, though everyone should make sure Azure is running atleast Windows Server 2008 R2, otherwise the start-up task will hang and the role will never start (until you kill the vcredist process in the task manager via RDP).
IF you want to verify about what is on Azure VM, just try to unzip your CSPKG file and then again unzip .CSSX file (just rename CSSX to zip) and match that every references is all there. This way you can match what is on VM. Once you verify what DLL is missing in VS, select the Reference DLL and set its property "Copy Local" as "True".
You need to place all dlls in the folder which is exist on live server. If you add refrence from your local and that location is not exist on live than you cannot build code on live. So make sure that all the dlls are exist on the live before deploy code on live.
Make sure the C++ dll and the unmanaged C library (if it's a .dll and not just source) are both included in your service package, and make sure the compiler's dumping the C++ library in the right place. This article has a decent walkthrough.

Adding dlls in VS 2008

I would like to add some external .dll libraries e.g. glut32.dll (but it's only example) in Visual Studio 2008, using C#.
Can you please tell me what should I do step by step?
I am a little bit confused cause I found a lot of solutions to add dll files, but they significantly differ.
Some of them add dll's only using code, some using properties in vs, add references and in other tutorials there is about registering dlls in system.
But how to put it all together?
There are different kinds of DLLs, you'll need to treat them differently when you use them in a C# project. The 3 main kinds are:
DLLs that contain unmanaged code and were designed to be used by a program written in unmanaged code. You cannot use such a DLL directly, there is no way to add a reference to them in a C# project. You must use P/Invoke, the [DllImport] attribute is required to declare the exported functions in the DLL. Glut32.dll is such a DLL. A very basic test you can use to see if you've got such a DLL is to run Dumpbin.exe /exports on that DLL. It lists the names of the functions that are exported.
DLLs that implement an in-process COM server. They are written in unmanaged code as well. .NET has very good support for using such servers, as long as you have a type library for them. The type library is usually embedded in the DLL, Visual Studio expects to find it when you add a project reference, either through the COM tab or the Browse tab. A very basic test is Dumpbin.exe /exports again, an in-process COM server has 4 exported functions. The DllGetClassObject function is the important one. You can view the type library embedded in the DLL with OleView.exe, File + View Typelibrary. A good example is c:\windows\system32\shell32.dll
DLLs that were created by a managed compiler. They don't contain machine code like the other types, they contain IL code and metadata. It is the native kind of DLL for managed code, you simply use Project + Add Reference to add a reference, the compiler automatically knows the types that are available in the DLL.
The first kind is the one you'll encounter a lot for DLLs in the wild. There's a lot of code written for Windows in an unmanaged language. It isn't a kind of DLL that's particularly easy to use from managed code. Glut32.dll for example has a lot of exported functions, writing a P/Invoke declaration for every single one of them is painful.
Tools you can use to help with this are SWIG and PInvoke Interop Assistant. The former is required when the DLL was written in the C++ language. C++ classes are not directly usable from a C# program, they need a wrapper written in the C++/CLI language. The latter tool is useful for DLLs written in C, including the Windows API.
Beware that those tools don't usually give you a clean and guaranteed-to-work interop solution. Declarations in unmanaged code are ambiguous, you'll need to know the exact semantics of the arguments of an unmanaged function to pick the right one. Getting the wrong one can be hard to diagnose, the best place to get help is a forum or Q+A site. Like stackoverflow.com
You can use the add reference dialog under the project menu to do that. Just go to the COM tab and add your library. Now you can verify that the classes appear in teh object browser
Right click on project in solution explorer and select add reference and then browse and select the dll.
If you wish to reference the dlls you
Right Click on References, and select add reference.
Or have a look at
How to: Add and Remove References in Visual Studio (C#)
Adding a Reference to a C# or Visual Basic .NET Project
In Solution Explorer, right-click the project node and click Add Reference.
In the Add Reference dialog box, select the tab indicating the type of component you want to reference.
Select the components you want to reference, and then click OK. Tip.

Categories

Resources