Writing C# managed code in native C++ - c#

I am developing a managed lib (using Microsoft Web Services) and I am
including it into a c++ project. The project doesn't use /clr option,
so when I include my library's header file VS2005 show me an error
saying I have to use /clr option. Doing this I have a incompatibility
with /EHs command line option (error D8016), but changing from EHs to
no exception handling not solving problem and keep showing me same error .
Any suggestion is welcome.
Thank you in advance.

If you have unmanaged C++ code and want to use managed code, you have a few options:
Change your unmanaged code to C++/CLI, by use of the /clr switch.
Write a C++/CLI wrapper library. It could DLL-export unmanaged functions which you call in your unmanaged code.
Skip the wrapper library and directly DLL-export unmanaged functions via this library.

You can't use a managed lib from an unmanaged c++ application. Since you add the /clr option, your c++ application becomes managed too (just for the record :) )
Here's what might help you: http://msdn.microsoft.com/en-us/library/ffkc918h.aspx - the restrictions of the /clr option.

It is possible to write managed c++ adapter, that will call the C# library, and call this adapter from unmanaged c++ program as you would usually call a normal c++ library. You will compile your adapter library with /clr and your main c++ program without /clr if for whatever reason you want to keep it unmanaged.

You can embed a mono environment and start an AppDomain. mono's runtime API will allow you to instantiate classes and call members on them. It will be clumsy, but is will work
http://www.mono-project.com/Embedding_Mono
Note that Mono is a full .Net 4.0 compliant CLR and it can work with the Microsoft core libraries on Windows.
On windows and Unix it can work with the Mono corlib/class libraries. There are areas not covered in Mono, but they seem to get sparse. You can use the MoMa tool to spot whether your application uses incompatible/incomplete APIs.
Or you can just use the Microsoft .NET framework, assuming you're on windows anyway!

Related

How to Inject a .NET managed DLL into another .NET process

Well, what i want to do is Injecting a C# DLL into another C# process,
then from outside (lets call it "the injector") call a method of that dll, all of this from c#.
Is it possible?
How to do it?
Thanks
EasyHook is your friend.
You can use the remote hooking functionality it provides to load a managed dll into target process, and invoke methods with .NET remoting.
I think that you mean .net libraries' dlls. Because c# is programming language not a process. In .net projects, dll's comes from .net runtime libraries. In order to load dll's in runtime you can use:
Assembly.LoadFrom("example.dll");

COM Interop in Mono 2.0

I am trying to use this code in a Unity project, but it seems the implementations of COM Interop in Mono/.NET differs, which causes the code to fail or crash. Running the code in .NET works fine, but running it with Mono 2.0 (outside of Unity) fails in the same way as in Unity, suggesting it is a problem with Mono in general and not Unity.
If I compile and run the code as-is, it fails because the type cast from MMDeviceEnumerator to IIMMDeviceEnumerator fails. When decorating all interfaces with [ComInterop], the cast succeeds, but the call to GetDefaultAudioEndpoint crashes Unity/Mono with an Access Violation.
It is hard to find good documentation of COM Interop on Mono in general - and particularly so regarding such an old version. Is it at all possible to get this running?
Wrap the COM functions in C funtions and call the C functions via P/Invoke instead. This can be done in two steps:
Create a VC++ project that wraps the functions you needed in wasapi. Expose them via a module define file or __declspec(dllexport). Build the code into a dll that exposes the functions you need.
In your Unity3D project, access them via P/Invoke.
Here is an example. In your case, just use the COM code in the C/C++ part to do what you want.
Mono 1.0 and Mono 1.1.xx do not have support for COM.
Stop trying with Mono, Mono is for platform independence and COM Interop is Microsoft only. Use open source SDKs for Video Playing or better invoke apps from command line like vlc to play, encode etc.

Compile C# .dll and produce .dll and .lib file [duplicate]

I need to integrate this C# dll
in my C++ code. I want to call some functions written in C# from dll and the rest of code write in C++. What is the easiest and quickest way to do it? The program will be executed only on Windows.
There are basically two cases to call a .NET DLL from unmanaged code:
The .NET DLL exposes a COM interface. In this case, you can use COM from your C++ code.
The .NET DLL does not expose a COM interface. In this case, you have two possibilities (to make it simple):
2.a. host the CLR as described here: Loading the Common Language Runtime into a Process
2.b. write a piece of managed C++ code (another DLL - written in C++/CLI) to wrap the .NET DLL and expose 'old way' DLL exports to unmanaged clients.
I don't specifically know the sharpbox system, but it looks like it's pure .NET and does not expose COM interfaces, so 2.b might be the best way to do it (not so easy...). Maybe it has a REST/Web easier API you could use.
PS: you can also add exports to a .NET DLL. This is described here: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.

How to access mono native code using php

Ahead of Time Compilation or AOT is a feature of the Mono runtime code generator.
mono --aot program.exe
This will generate a file called "program.exe.so"
How can i load this shared object file in php script and access the class objects and methods. ?
Thanks
The native library still needs to be loaded inside an AppDomain (i.e. the Mono VM/runtime) in order to run, it is not a native library as such.
If you must I'd suggest looking at
whether php supports COM interop (I don't use php, but I'd reckon the chance exists). This would be good since you could use that and profit from OO interface exposure
Use Swig which has support for C# some time now
Alternatively, use mkbundle, and/or create a native shared library that embeds a Mono VM. The shared library wrrap around the C# interface using a "C" native API's.
The Phalanger project should be able to do this. You can compile your php code with mono and also integrate with .net from php.

Auto wrap c++ dll into c#

I want to use c++ library in a c# project. Is there any wrapper tool to import all classes automatically?
SWIG can help create a wrapper consisting of two parts, one C++ sided, and one C# sided.
It needs a bit of work to set up the correct generation files though.
An alternative that requires more manual coding is C++/CLI.
For pure c apis I prefer p/invoke over either of them. There is a program to automate conversion of c headers. If I recall correctly it's called something like "P/Invoke Interop Assistant" or "Interop Signature Toolkit".
There is also mono/cxxi which looks pretty cool.
The procedure of using native .dll's in .Net is called P/Invoke. Look at http://pinvoke.net/ for some examples.
Note that you must match the build target with the version of the .dll. So for x86 .dll's you need to lock your project to x86, same with x64.
Note2 that you only need to lock the executing project (.EXE), not any additional projects loaded from the .EXE. .Net will automatically match .Net .dll's to CPU target type if they are set to ANY.
From http://social.msdn.microsoft.com/forums/en-US/clr/thread/c957959e-0f0c-422e-a5be-4ccfdd12e63d: You can use "dumpbin /exports <name_of_your_dll>" or dependency walker (depends.exe) to look at the exported symbols. They are both included in Visual Studio.
Additional comment on C++: While it is relatively simple to use native .dll's written in C from .Net, using C++ binaries that make use of objects is not as trivial. One way to solve that is to use a C++ CLI project a binding between managed .Net code and unmanaged C++ library.
If this is unmanaged code then you could use P/Invoke. Another possibility is to use the C++/CLI extensions to compile the code into a managed assembly that you could directly use.

Categories

Resources