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.
Related
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.
I have a WPF app that works with local SQLite and Entity Framework Core. I want to distribute my app using ClickOnce.
Everything works fine when I run it using Visual Studio 2017. But app had a crash on a start when I used ClickOnce. The crash was so early so I couldn't even log the exception. After some time I found that in publish folder some dlls were missed. For example System.Runtime. All references are NuGet packages. Then I found that despite of I have a reference to the dlls in my project ClickOnce application files list doesn't contains it.
The only solution I have found for now is to add missed dlls as files in root of my project and set build action to Content. In ClickOnce application file dialog I set publish status to Include.
It works now but it looks like not the best way to do it. The question is why they are missed in the first place. More likely I missed something or I am not understand the root of the problem.
UPD:
You can find an example code here.
It works from Visual Studio but crashes on a start when you try to install it as ClickOnce application.
UPD: The problem was fixed in Visual Studio Professional version 15.6.1
There are two ways solving solve this.
At first, you have to go Properties → Publish → Application Files, and at this place, make your DLL files include in your project
But if it does not work, go to References and make the DLL file CopyLocal = False. You add a DLL file like Existing Item in your project and make them Copy Always.
It will work correctly.
But for your sample I watch this and I solved it. Look at this image:
You have four DLL files. You need do this for them:
System.Diagnostics.Tracing.dll
System.Reflection.dll
System.Runtime.dll
System.Runtime.Extensions.dll
At first go to your reference and make them Copy Local False:
Then go to the path of each DLL file and add them like Existing Item, and then make all of them Copy To Output Directory CopyAlways
Then Publish it and run it like in this picture. I run it from publish file and it works.
Even if you set the dll to copy local, the dll will not get copied over unless you actually use the dll in your code. You might try adding System.Runtime in your code somewhere like this maybe?
var dummytest = System.Runtime.GCSettings.IsServerGC;
Based on Issue 9 on https://blogs.msdn.microsoft.com/bclteam/p/asynctargetingpackkb/ I was able to do the following:
Symptom
ClickOnce applications targeting .NET Framework 4.0 that reference the Microsoft.Bcl or Microsoft.Bcl.Async packages may experience a TypeLoadException or other errors after being installed.
Resolution
This occurs because ClickOnce fails to deploy certain required assemblies. As a workaround, do the following:
Right-click on the project and choose Add Existing Item
Browse to the folder where the System.Runtime.dll lives
In the File name text box paste in the path of the file
Click the down-arrow next to the Add button and choose Add as Link
In Solution Explorer, holding CTRL select System.Runtime.dll
Right-click the selection, choose Properties and change Copy to Output Directory to Copy always
Republish
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.
I come from a long Java background and very new to C#. I was trying to run one of the starter kit applications from Windows phone and I am getting this error.
A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.WindowsPhone.dll
I am finding it hard to debug this because I am directly referring to the dll file under References pointing to Newtonsoft.Json.WindowsPhone. In Java, I would have searched for the library and downloaded the source to step into the exact location during debugging.
If that is possible, how would I do it in C# (as in how would i refer the library source for my project to pick up at run-time)? I am using the Visual studio 2010 Express Edition as the IDE.
Download the third party library you are dealing with
Add this library's project into your solution
In your project, remove the reference to the 3rd party library
Add a project dependency into your project, make it depend on the 3rd party library project you have just added to your solution
Compile and run, happy debugging
My best guess is you should download last release of Json.NET, remove the compiled library from your project's references and add reference to the source code project. (Add Reference... > Projects > Browse...)
Once you have stopped your program in the debugger, you can use the modules window to load the symbols for Json.NET. Obviously, you need to have the symbols on your machine so you can browse to them.
Failing that, you can switch to a project reference and include Json.NET in your solution as Dan suggested.
The easiest way is to download their latest build then inside visual studio right click your solution and under add menu select existing project point to project file of the library and click open button. after that u will be able to set brakepoint wherever u want.
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.