use vc++ class library in c# - c#

i have created a simple "public ref class" in the vc++ project, which is configured to generate a dynamik library.
In the c# Project (simple console application) i added the vc++ project as to the References and doing a "using myVC++library".
But when i try to create an object from the vc++ dll i always get: System.BadImageFormatException was unhandled
any clues or helpfull tutorials on this?
TIA

Is your c# project set to build for "Any CPU" - if it is, and you're on a 64-bit OS, then the load will fail.
The VC++ dll will be 32-bit only, and 64-bit app can't run it.
You could change your c# build to target x86 instead.

Related

How to link a .so file to a managed DLL with mono

I'm writing a program for embedded linux on an ARM processor in .net that uses mono to execute. I have found that I can simply compile to either x86 or x64 architecture on my PC, copy the Debug directory over to linux, and run the program using mono myProgram.exe. The program is working perfectly like this and mono seems to take care of the architecture mismatch. This was true until today when I tried to incorporate an external native DLL.
I have both an x86 and x64 bit architecture of the external DLL and both work on the desktop environment fine. I have a shim class that uses [DllImport] to load the extern functions. However, when I try this on embedded linux with Mono I am getting a BadImageFormat exception. My guess is that Mono somehow transitions the compiled DLLs in the exe at start up but doesn't do the same for the external.
Some other info:
1. I don't have source for the native external library. I have x86, x64, and a .so library.
2. I have tried making a manage C++ shim that accesses the .so but can't get it to compile on the desktop since windows doesn't recognize the .so file.
Some thoughts:
1. Is there a way to embed the native DLL into a managed so that Mono will transition the native DLL too?
2. Can I link the .so file to a managed C++ project?
3. Is there a way to tell Mono to incorporate that Dll during execution?
Ultimately I'm looking for a solution that is wrapped in the exe that allows me to simply run it like I did before and control the native system.
If you have all the .dll and .so files for Windows and Linux, you can probably use dll maps feature of Mono. (https://www.mono-project.com/docs/advanced/pinvoke/dllmap/) which are used to map Windows dll names to Linux so names.
.Net does not recognize this, but Mono does.

Native C++ dll in one solution with C# project

I have a source file written in C++, performing some simulation. I would like to create a user interface for this in C#. How can I have both C++ and C# in a single Visual Studio solution?
What I did so far was I created a .NET C# project and solution, and then a native project as a DLL library. I have marked the C# project as "Depends on" the C++ dll. I have provided the necessary dllexport directives and imported using DllImport. Everything compiles fine, but when I execute the application, the dll is not found:
Unable to load DLL 'xxxx.dll': The specified module could not be found.
It is no surprise the DLL is not found, as the application is run from its output directory and the output directories are different for the C# and C++ projects. I do not want to write an explicit path as my import like `"..\..\Debug\xxxxx.dll".
Is there some common way how to structure a solution containing native a Dll and C# app so that the C# app can call the native Dll?
If you know that after deployment your C++ DLL will be in the same folder as your C# DLL (or executable), then on simple way to solve the problem is to use the PostBuild event (Project properties, Build events).
You can add a copy command that will put your C++ DLL into the C# output folder.
I found a very comfortable way, which might have some unexpected drawbacks, but I do not see any so far: I can enable CLR for the C++ DLL project, making it "mixed", even if it in fact does not contain any managed code, and then the project building it can be made a Reference in the C# .NET project.
To make the project mixed mode set Configuration Properties / General / Common Language Runtime Support to Common Language Runtime Support (/clr).
After this the build system copies the DLL into the application output folder automatically.

C# Projects in CMake-Made VS2010 Solution Must Be Unloaded/Reloaded to Avoid Being Skipped

I'm using CMake to generate a VS2010 solution with both C/C++ projects: native console app, static libs and native DLL; and C# projects: managed console app and managed DLL.
I'm using CMake with the include_external_msproject + CONFIGURE_FILE technique as described e.g. here.
However, the resulting solution keeps skipping the build of the C# projects.
Following this, if I "Unload Project" and then "Reload Project" the projects now build properly.
I must do this every time I re-"Generate" the solution with CMake.
Why is this happening?
Is there a way to avoid this?
Had the same problem here.
Our cmake generated solution created configurations for Win32 and x64 platforms while the external C# project had x86 and x64 platforms. After cmake generated the solution, all projects (internal and external) are set to use platform Win32. (Check the solutions Configuration Manager.)
To force cmake to include the external projects as x86, specify the PLATFORM parameter when calling include_external_msproject.
include_external_msproject(TestApp "/TestApp/TestApp.csproj" PLATFORM x86)
(And of course ensure that you just do this for the x86 version...)

Debug C#.Net DLL Project from VB6 DLL project

I have three projects,
One is a VB6 project which compiles to an EXE.
Another is also a VB6 project, which compile to a DLL.
And the last one is a .Net (2010) project which compiles to DLL.
The DLL from .Net is referenced in my VB6 DLL project.
And the resulting VB6 DLL project is reference in my VB6 EXE project.
In this scenario, how can I debug my .Net DLL project?
It is failing (or not able to debug) in both cases, even if I attach the (VB6 DLL) process to my .Net DLL Project, or I setup the ‘start external program’ to my VB6 DLL project on .Net DLL Project.
Do I have to change the reference (.Net DLL) on my VB6 DLL Project to get it in debug?
All your valid inputs are welcome.
(Please explain the procedures step by step which I need to do)
Thanks in advance.
You should be able to debug the C#.NET dll by setting the start action under Debug to
Start external program
and specify the VB6 executable as the program.
I know this works as I use this to debug a .NET/C# plugin for an old VB6 application.
It shouldn't matter how many other dlls (.NET or otherwise) you have to go through to get to the code you are trying to debug.
ah vb6 :) it's been a long time....
basically, you can't debug .NET code within VB6 IDE.
However, nothing stops you from creating a .NET test project to unit test the .NET dll. And that's what you should have done prior to reference it in VB6.
If you need to track down a specific issue, another way you can use is to write debugging infos to a file/database/event view/... when the dll is in debug mode, like which functions were called, parameters, stack trace...
This might not be correct, but I believe VB 2010 uses what? .NET 4.0, right? VB6 was created in like...1994 I believe, so it would be <= .NET 1.0. The binaries would more than likely be incompatible.

how to call a C++ dll from C# windows application project

I have created a dll in C++ using a Class Library project in Visual Studio. I need to call a method in the dll from a C# application.
I got to know there are 2 approches. One is to add the dll project reference to C# project or use DllExport to export method. However when I tried in both ways it always gives the following error when the dll method is called in runtime.
An unhandled exception of type 'System.BadImageFormatException' occurred in TestClient.exe
Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
Can i know how to avoid this problem ?
Thanks in advance!
This error means that you're trying to load a 32-bit DLL into a 64-bit process or a 64-bit DLL into a 32-bit process. On Windows, the bitness of the DLL must match the bitness of the process in order for it to load correctly.
Is your native DLL 32- or 64-bit? In your C# project build settings, what platform are you targeting? If you go into the C# project properties, you can go to the Build tab and change the "Platform target" to something specific like x86 or x64 to match the platform that your native DLL was built for.
The other alternative would be to build the native DLL to match the platform of your C# application. If the C# application's platform is AnyCPU, though, it will run as 32-bit on 32-bit Windows and 64-bit on 64-bit Windows. Because of this, you would need both a 32- and 64-bit version of your native DLL.
I would recommend setting your C# application's platform to something specific (x86, x64) and then change your native DLL's platform to match.

Categories

Resources