Using C# dll in C++ codes without using Regasm - c#

I have created a C++ application which uses a C# dll following the link as shown below.
http://support.microsoft.com/kb/828736
The application works fine on my development PC , but when I tried to run it on another PC , it fails.
The only way to make it work is to recompile the C# dll on that PC and run the Regasm command again on that PC to work .
Is there a way to build my C# dll in such a way so that I do not have to do this every single time on every PC my application is deployed to ?

What regasm does is generate a COM wrapper for your C# dll and then registers it as a COM object.
You do not need to recompile though, once you have a dll you can register it on the target PC using regasm.
The main other way to call a C# dll is to write a C++ .net program using the C++/CLI system from Microsoft. This is basically a way of creating .NET programs using a superset of C++ (hint: its not C++, which is a shame as the old managed extensions it replaces was much more transparent for this task). As its not 'true' C++ you'll end up with a .NET binary instead of a C++ one, so be aware of this. One aspect is that you could write a C++/CLI wrapper to the C# dll and call that from your C++ program, this is probably the preferred way of creating a bridge between the systems.

I did this, and it worked, copy and paste the code on cmd
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\regasm.exe MyAssembly.dll
Kindly vote if it works for you. :D

Related

Calling C# compiled DLL classes and functions from VBA (excel)

I have a large codebase built in C# (classes and functions), compiled to .NET 4.7.2 in VS2019 and need to call to the compiled C# assembly (compiles to .dll) from VBA in excel. What is the path of least resistance to achieve this, considering I need to deploy the final VBA + C# dll (and whatever wrapper layers I may need) to client machines that won't have admin rights.
I can recompile the C#, if necessary.
If I cannot call directly to C#, could I go via a C++ native wrapper, and then on to C#? (or do I need a C++/CLI layer too?).
I have tried the recommendation here but access denied (admin rights required)
I also tried this Side-By-Side COM Interop with C# and VBA. But the .NET framework that excel targets is of an older version than that which I build my C# on. Not sure how I can fix this... Would it require registry edits? because if so, I can't use that method as I don't have admin rights.
Lastly I looked at this option https://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/. But it appears to be going via C++ -> C++/CLR -> C# source. considering I start in VBA, that's two intermediate steps... surely there is a better way?

Using C++ dll object within C#

I have a third party C++ .dll which exports some classes which I wish to use from C#. The dll is a Win32 application and I'm working on Windows 8.1 using VS2013. When I set up the C# program running under 'Any CPU', I get the error "System.BadImageFormatException". I've read other places that this can be caused by combining x86 and x64. So I recompiled the C# program as x86. Now I get "Unable to load DLL 'libsword.dll': The specified module could not be found". I've made sure the dll is in the proper directory.
All of this works just fine on Linux using Mono.
At this point, I've got no idea how to proceed.
Thanks,
Jon
BadImageFormatException is thrown when you treat regular native C++ code in a DLL as if it was .NET code. .NET also puts code in the .DLL files.
You'll need to create C# declarations for the C++ code, so that .NET knows how to transition from managed (.NET/C#) code to native (C++) code. This is fairly complicated, depending on what the code in your 3rd party library looks like. Too much for me to speculate what you need to do.
The problem was in the C++ library, which was originally written and developed for Linux. Export was defined as:
define SWDLLEXPORT _declspec( dllexport )
Which was used as:
const char * SWDLLEXPORT function_name(args)
which worked in Linux but not in Windows. Changing the function signature to:
const char SWDLLEXPORT * function_name(args)
Works in both Linux and Windows.

How to call a dll file from c#

I am trying to call a dll file from c#
The dll file is made from a java application using ikvm and for now all the code does is print hello world.
How do i call the dll file in my c# code and is it possible to create an application in java that will return a boolean value to my c# code?
Thanks for your time.
I'm not sure I understand what you're trying to do, so apologies if I'm misreading. IKVM should translate your java code to a .NET dll or executable. After the "translation" you should be able to use the .dll more or less in the same way as you would with a "native" .NET code.
If your java application has a main method that prints "hello world" on the console, you should have converted it to a .NET executable (.exe) and not to a dll.
After converting it to a .exe (and assuming you're running it on Microsoft .NET on a windows system) you should just execute it.
As for the second part of your question, you can also create a dll (converted from java) that returns a boolean and consume it from a C# application.
See this tutorial for two examples of (pretty much exactly) what you're doing.
using System.Runtime.InteropServices;
You can then use
[DllImport("myjavadll.dll")]
Then add the dll as a reference by right clicking and navigating to it in the reference folder.
EDIT:
Here is a link that calls a C++ dll to C#. You may be able to work it out.
Call another languages DLL
EDIT: I have had issues adding DLLs as references and was forced to add as a resource. I believe this was because I was working with a sys32 dll.
Here is an old post by where I was trying to work out some DLL import errors. Maybe itll be helpful if you encounter some problems.
Old Post

Embedding C# type library for C++ use

I'm writing a program in C++ (unmanaged code) in which I use a C# DLL.
Everything works fine when I'm in a debug mode (the dll is called, the job is done) on my machine
When I run the debug version of the program on a different server, it won't work too.
Do I have to embbed my TLB library in the .exe? If so how to do that?
However, when I "release" it the program act wierdly.
C++ is compiled instead of interperted like C#. So you probably have to recompile your program on the other server.

Generation of .tlb Files in Windows 7 Pro 32-bit

I have a C++ DLL that imports a .tlb file generated in a C# project. The C++ DLL is a wrapper DLL containing functions that call the corresponding C# functions.
When I call the C++ functions on the computer that I built the projects, all works well. But when I copy the DLL's and generated tlb's to another computer with the same exact version of Windows and installed programs andI call the C++ functions, it breaks with a COM error. However, after recompiling the projects on the new computer, everything works again.
I already checked the "Work on All Computers" for both projects but this keeps happening. What else do I need to do for the DLL's to work on all computers?
The HRESULT you get would be crucial to diagnose this. Forced to guess: did you run Regasm.exe on that machine? Required to make the necessary registry entries so COM can find the server. It is automatic when you build in the IDE.

Categories

Resources