I have a small C# class (Gram.cs) and it is working with a 3rd party dll to operate a device. Now, I need to call some functions in this C# class using C++.
How can I do it?
I am using MS Visual Studio 2010 professional edition.
If C# class is small, and it deals with native dll, it might by simpler rewrite that class in C++, rather then integrate .Net into your application. In addition, integrating .Net will force whole .Net virtual machine to start just for processing your simple class.
Otherwise,
You could use COM interop: build an assembly based on your C# class and make it COM visible. Then you could use class as a COM-object.
You can use Managed C++ to make a wrapper between managed and unmanaged code.
You can reverse control flow, so C# code will call unmanaged functions. In this case you can export it from your binary and import from C# code with DllImport attribute.
Normally you should make your c# code as COM visible from your c# project settings and use c# IJW regasm tool.
Look into http://msdn.microsoft.com/en-IN/library/ms173185.aspx
I had integrated c# into c++ using this approach few years ago.
You will be able to load your c# assembly as a COM component in your c++ code.
This might be what you're looking for (I've answered similiar question here before: how to : use c++ projects for windows phone (C#))
a) Load symbols directly from C library, for example:
using System;
using System.Runtime.InteropServices;
static class win32
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}
(this is taken from: http://www.andyrushton.co.uk/csharp-dynamic-loading-of-a-c-dll-at-run-time/ after brief googling)
You need to export the C++ interface methods as "C" for that, e.g.:
extern "C" __declspec( dllexport ) int MyFunc(long parm1);
(from MSDN: http://msdn.microsoft.com/en-us/library/wf2w9f6x.aspx)
b) Use a wrapper in C++/CLI to connect unmanaged C++ to managed C#:
here's a good example: http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes
Do note that the syntax is somewhat weird at first look, and not eveything can be used. However, what you gain is ability to use the C++ classes - something that exporting "C" prohibits you from doing.
Related
I'm trying to find a tutorial on creating a C++ class in a DLL that can be called from C#. I've found a few tutorials but they either do not compile in VS 2013 Community or are not on point. The C++ object has only a few exported functions (i.e., that are called from the consuming C# app like ptr->StartAction()). It has about 70 procedures that are not exposed, in assembly language which is why I need to use C++. Currently I expose some functions in the DLL via dllimport etc. But C# requires using fixed(...) (to prevent garbage collection) which slows everything down quite a bit (and they are called 1,000,000s of times). Hence the need to move all that into the DLL. So I need a class that I can instantiate in the C# calling program and then call the public methods in the dll. I'm using .NET 4.0. The DLL will only be used by this one C# app.
You can achieve it through the help of COM component and IDL with an output of Interoperability dll where you can use in your c# project. For more details follow this link.
You can use the DllImport attribute of C#. Here is a sample signature:
[DllImport("path to dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string methodName(string parameterName);
C# will call the method with the same name in the DLL.
Its impossible to say what you really need, because we don't have your method declarations.
I want to create a DLL using C#, which can be then called from VBA in the following manner -
Private Declare Function invokePath Lib "\\shared_computer\Projects\somedll\myDLL.dll" (ByVal strUName As String, ByVal strSFile As String) As String
The idea is, if the path of the DLL is changed I need to only update the path in the Private Declare function line....
I have searched a lot for it, and also not sure whether this arrangement is possible - where we can call a DLL function from a network path without referencing or registering.
Actually you can.
Check out unmanagedexports
It is a complete walk-through and you'll be able to create entry points like this:
[DllExport("GenerateSomehting", CallingConvention = CallingConvention.Cdecl)]
public static int GenerateSomehting(int somevalue, ref IntPtr pointer)
and call it just like any other unmanaged dll.
[DllImport("MyGenerator.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int GenerateSomehting(int somevalue, ref IntPtr pointer);
You can not create unmanaged DLLs with C#, as C# is a .NET only language. Use C++.
The only way to use a .NET DLL from unmanaged code (for example VBA) is to use COM interop.
Check P/Invoke here, The article contains a useful table of how to translate managed to unmanaged types, short code examples (in C#), tips and a list of resources.
Essential P/Invoke
C# Dll cannot export functions that can be called by any other non-CLR code.
This is not possible.
I want to use rasterbar libtorrent in a C# application. It's written in unmanaged C++.
I'm new at using DLLs and found this article online: How to Marshal a C++ Class
It describes that it's not possible to marshal/invoke a C++ class directly and you have to write a bridge in C (or managed C++) in order to use the library in managed C# code.
The article is from 2007 and I wonder if there is a better solution to use C++ DLLs by now. I'm looking for solutions that also work on the Mono platform. libtorrent is cross plattform so it should be possible.
First you should have the libtorrent dll built for windows. This link here can help you. Then for calling the un-managed code (C++ in this case) from the managed code (C# in this case) you can use Platform Invocation Services (PInvoke). It allows managed code to call unmanaged functions that are implemented in a DLL. For example have a look on this MSDN code
// PInvokeTest.cs
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest
{
[DllImport("msvcrt.dll")]
public static extern int puts(string c);
[DllImport("msvcrt.dll")]
internal static extern int _flushall();
public static void Main()
{
puts("Test");
_flushall();
}
}
The better approach will be to write a wrapper class in C# for libtorrent dll, wrap it's methods using PInvoke and use the wrapper throughout your project.
I have a c++ library file (.lib). How can I access the functions within it from C#? I have read that I could wrap the library file in a c++ dll and expose the functions that way. Is that the only way? I am not the owner of the code, so my options are limited.
Wrap the C++ lib with a C++/CLI assembly.
C++/CLI allows you to mix managed and unmanaged code, serving as a bridge between C# and native C++. It's actually very straightforward and relatively easy to do, although it can get tedious if you have a lot of classes/functions to wrap.
Here is one example.
You can not access a c++ library file (.lib) directly. The best way is to have an unmanaged wrapper around your unmanaged code. Reference DllImportAttribute.
There's a good example of it's usage in the MSDN help document:
using System;
using System.Runtime.InteropServices;
class Example
{
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
static void Main()
{
// Call the MessageBox function using platform invoke.
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}
Also note: You can have a managed c++ wrapper around your c++ library, but it's better to write your wrapper in unmanaged c++ code.
I have couple of lib files and I want to use in my project. How to build the C# Wrapper around C++ lib files.
Any weblink or any tutorial you know, please send me.
Thanks in advance.
Harsha
Create a C++ CLR Class Library project, and then write a C++/CLI class which wraps the functions in your libs.
From C#, you can then add a reference to your class library, and call the class and its methods directly.
After creating the C++ CLR class library.
You can also consume it using DLLImport
//c# code
[DllImport(#CPPProjectName, CallingConvention = CallingConvention.StdCall)]
public static extern void SendData(string args);
Invoke the method in normal way
SendData("data123");