How to create unmanaged DLL using C#? - c#

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.

Related

Creating C++ DLL called from C#

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.

Calling c dll in C#

Introduction to the problem:
I've got to control a certain device through API provided with a DLL file, LIB file, and c header files whose functions are declared as dllimport.
When I use the API in a C++ project everything worked just fine - I included the headers, lib , dll, and called the functions as declared in the header files.
The problem begins when trying to call those functions from a C#.NET project, using [DllImport] attribute: The functions were declared with the exact name and parameters, and running the code did not throw any exception. And yet the device did not respond at all, like the functions had never been actually called.
How it is declared in the C header:
int __declspec(dllimport) Init_Card_RTx(unsigned short device_num, unsigned short enabled_channels, unsigned short xmt_channels);
How it is declared in C#:
[DllImport(Path, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Init_Card_RTx")]
public static extern int Init_Card_RTx(UInt16 device_num, UInt16 enabled_channels, UInt16 xmt_channels);
The questions:
Is that because the functions in the headers are declared dllimport?
In that case, do I have to wrap the DLL with C++ functions declared as dllexport?
What are the steps required for a C DLL to be accessible from C#?
In the C# project, do I have to include the LIB file as well? not just the DLL file itself?
Is that because the functions in the headers are declared dllimport?
Possibly. The functions need to be exported. Whether they are exported or not depends on how they DLL was compiled by whoever gave it to you. I'd guess that they should be though (or the C++ code would try import them and fail).
The first thing I do when troubleshooting this kind of thing is to load the DLL in Dependency Walker which will show you all the exported functions. If they're not showing up, they're not exported, and C# can't call them. If they are showing up, then you're ok and you don't need to change any of your C code or create any wrapper functions.
In that case, do I have to wrap the DLL with C++ functions declared as dllexport?
No. C# can only call C functions using DllImport. C++ does name mangling when exporting functions, which makes things a mess and generally not workable.
What you need to do is make your functions exported somehow. My guess is that they are already exported, but if not, you could make some wrapper functions like this.
int __declspec(dllexport) Init_Card_RTx(unsigned short device_num, unsigned short enabled_channels, unsigned short xmt_channels) {
// just call the other function here
}
These should make the function exported and you should see it show up in dependency walker.
What are the steps required for a C DLL to be accessible from C#?
The CLR has to be able to find the dll (usually you just put it in the same directory as the C# exe), and the functions have to be exported as C functions. That's pretty much it.
In the C# project, do I have to include the LIB file as well? not just the DLL file itself?
You don't need to include anything in the C# project at all, just the public static extern wrapper functions with their DllImport attributes on them. So long as the CLR can find the dll at runtime, that's all you need. If it can't find it at runtime, you should get an exception when you call the first imported method.
PS: Get Dependency walker. I can't recommend it more highly for this kind of thing
This kind of thing is always a pain in the butt. You have to make sure that the byte structure of the data being passed matches what your DLL is expecting, and exactly. The native structures in C# don't know that they are being passed externally, so you have to structure the code more rigorously.
See here for more info:
http://msdn.microsoft.com/en-us/library/awbckfbz(v=vs.110).aspx
The reason the functions are declared as dllimport is (correct me if I'm wrong) the the DLL is provided by your card vendor, that's ok because dllexport is only needed when building the DLL.
[DllImport(Path, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Init_Card_RTx")]
The first argument Path is there only for ilustration purposes, am I rigth?
Can you open the DLL with depends.exe or PE Explorer(http://www.heaventools.com/download-pe-explorer.htm) and see the DLL's export table?
I'm asking this because the function name maybe decorated, that could happen if the DLL was compiled as C++ instead of plain C.
Try declacring it like this (you only do this once, in a CPP file, NOT a header)
extern "C" __declspec(dllexport) int __stdcall Init_Card_RTx(unsigned short device_num, unsigned short enabled_channels, unsigned short xmt_channels)
{
//
}
and in C#:
[DllImport("MyDll.dll")]
int Init_Card_RTx(ushort device_num, ushort enabled_channels, ushort xmt_channels);
you need to export it and declare with extern "C". __stdcall is convenient as it's the default on the .net side. I have a macro in my C++ project to make sure I type it the same everytime.
And yes, get dependencywalker....

How to call C# code in C++

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.

Accessing a C++ .lib library from c#

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.

Instantiating a C++ class in C# using P/Invoke via a pointer

I am importing the CreateICeeFileGen() function from the unmanaged DLL mscorpe.dll in a C# application, in order to generate a PE file. This function returns a pointer to an C++ object defined here, is there any way I can access fields from this class via C# or do I need to write an unmanaged wrapper DLL?
The code I'm using currently is as follows:-
[DllImport(#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorpe.dll", EntryPoint = "CreateICeeFileGen")]
static extern void CreateICeeFileGen(out IntPtr iceeFileGenPointer);
...
IntPtr filePtr;
CreateICeeFileGen(out filePtr);
N.B.: I know you can do similar things with .net libraries but I need to use unmanaged libraries for my purposes.
You need a wrapper library to be able to use the class from C#.
The best bet would be to create the wrapper using C++/CLI, which can directly call the unmanaged function and expose the details with a managed class. This will eliminate the need to use P/Invoke for anything.
(Well, technically if you know the class layout you could mess around with pointer arithmetic to access the fields, but this would be very fragile and messy, and trying to call virtual functions in this way would be extremely ugly).
It looks like COM class/interface. Can you not just use COM instead?

Categories

Resources