Question about C++ DLL function use in C# - c#

I have a DLL made in C++ that is then used in a C# program. the C# program references the DLL and i call the classes and can use the functions defined in the DLL.
Now i want to add other functions to the DLL so i can run C++ code in C#. I Follow the example of how the other functions in the DLL are defined; .h and .cpp and i create a new function that executes code. I put it in the same places as the previously define functions, but when i put the DLL in my C# code my function is not there. when i create an instance of the DLL class my function is not a part of it.
I put it in the .h as a public function of the class and gave it code to run in the .cpp but its not found.
Why can't my C# program see the function i made even though its following the same suite as the other functions?
UPDATE
code.h:
#pragma once
#include <windows.h>
#include <PvDisplayWnd.h>
#include <vfw.h>
#include "NPvResult.h"
#include "NPvDeviceInfo.h"
#include "NPvBuffer.h"
#include <string>
using namespace System;
using namespace System::Runtime::InteropServices;
public ref class NPvDisplayWnd
{
public :
NPvDisplayWnd();
bool Handle();
NPvResult^ ShowModeless(long locx, long locy, long x, long y);
NPvResult^ ShowModal();
NPvResult^ NPvDisplayWnd::Create();
int^ Display( NPvBuffer^ aBuffer, int^ x);
NPvResult^ Work( const std::string afilename, unsigned short asizex, unsigned short asizey, unsigned short aBPP, double aFPS);
NPvResult^ Close();
NPvResult^ DoEvents();
}
code.cpp:
#include "NPvDisplayWnd.h"
NPvDisplayWnd::NPvDisplayWnd(){code}
NPvResult^ NPvDisplayWnd::Create(){code}
bool NPvDisplayWnd::Handle()
NPvResult^ NPvDisplayWnd::ShowModeless(long locx, long locy, long x, long y){code}
NPvResult^ NPvDisplayWnd::ShowModal(){code}
int^ NPvDisplayWnd::Display( NPvBuffer^ aBuffer, int^ x){code}
NPvResult^ NPvDisplayWnd::DoEvents(){code}
NPvResult^ NPvDisplayWnd::Work( const std::string aFileName, unsigned short aSizeX, unsigned short aSizeY, unsigned short aBPP, double aFPS){code}
NPvResult^ NPvDisplayWnd::Close(){code}
The function that i added was the Work() function. if i build this and place it in my C# code, it sees all the functions except work. To make sure i was using the right .dll, i changed the name of ShowModeless() to ShowModeless__F() and rebuilt it and added it to my C# and the change carried over, but i still didn't see my Work() function.

On Windows, you must explicitly export symbols in order for users to be able to access them via DLLs. This is done using the __declspec(export) syntax. This is needed during compilation and linking of the DLL.
Now that we know you're using C++/CLI instead of regular C++, we can see a problem. Work takes a std::string. But Work is intended to be a managed function, and managed code cannot take a std::string as a parameter. You need to take a managed string type instead.

A native C++ class cannot be imported in C#. You need to utilize C++/CLI feature in existing C++ DLL so that managed clients (like C#) can import the classes.
namespace XX
{
public ref class YourClass{...};
}
It will export managed class YourClass under XX namespace. You need to use /CLR compiler flag.
You may compile set of CPP files as managed, and set of other files as unmanaged. Managed classes cannot contain unmanaged stuff (unless it is declared pointer).

Related

DLLExport in .NET Core 3 for use in an unmanaged C/C++ Code

I have found the DllExport Project on GitHub while searching for a way to use C# .NET Core 3 Code from Plain C/C++. My goal is being able to compile the C# to any dynamic library and use it on both Linux or Windows with dlopen (dlopen for Windows).
C# Library I'm trying to create:
using RGiesecke.DllExport;
using System;
namespace CSharpPart
{
public class Test
{
[DllExport]
public static int _add(int a, int b)
{
return a + b;
}
}
}
C/C++ Code where I'm trying to Reference the C# Library:
#include "CPartCMake.h"
#include "dlfcn.h"
#include "cstring"
using namespace std;
int main()
{
const char* pemodule = "path_to_dll\\CSharpPart.dll";
void* handle = dlopen(pemodule, RTLD_NOW);
typedef int(_cdecl* _add)(int a, int b);
_add pAdd = (_add)dlsym(handle, "_add");
cout << pAdd(5, 7);
return 0;
}
The Call to dlopen returns a non NULL handle which I guess means that the Library itself can be opened. dlsym return a NULL. When viewing the C# DLL through DLL Export Viewer it can be confirmed that the DLL doesn't contain the _add entry.
I tried Wrapping the C# project in an C++/CLI Project like proposed here but it doesn't seems to work with .NET Core.
I read there is also a possibility with using COM interop but I didn't really understood what it is about and I'm not sure if it would work cross-platform like.
I'm afraid that DLLExport just isn't yet compatible with .NET Core as well. Is there any way of achieving my goal like I described it?

Sharing function between c and c# [duplicate]

I have a C lib and want to call function in this library from C# application. I tried creating a C++/CLI wrapper on the C lib by adding the C lib file as linker input and adding the source files as additional dependencies.
Is there any better way to achieve this as am not sure how to add C output to c# application.
My C Code -
__declspec(dllexport) unsigned long ConnectSession(unsigned long handle,
unsigned char * publicKey,
unsigned char publicKeyLen);
My CPP Wrapper -
long MyClass::ConnectSessionWrapper(unsigned long handle,
unsigned char * publicKey,
unsigned char publicKeyLen)
{
return ConnectSession(handle, publicKey, publicKeyLen);
}
The example will be, for Linux:
1) Create a C file, libtest.c with this content:
#include <stdio.h>
void print(const char *message)
{
printf("%s\\n", message);
}
That’s a simple pseudo-wrapper for printf. But represents any C function in the library you want to call. If you have a C++ function don’t forget to put extern C to avoid mangling the name.
2) create the C# file
using System;
using System.Runtime.InteropServices;
public class Tester
{
[DllImport("libtest.so", EntryPoint="print")]
static extern void print(string message);
public static void Main(string[] args)
{
print("Hello World C# => C++");
}
}
3) Unless you have the library libtest.so in a standard library path like “/usr/lib”, you are likely to see a System.DllNotFoundException, to fix this you can move your libtest.so to /usr/lib, or better yet, just add your CWD to the library path: export LD_LIBRARY_PATH=pwd
credits from here
EDIT
For Windows, it's not much different.
Taking an example from here, you only have yo enclose in your *.cpp file your method with extern "C"
Something like
extern "C"
{
//Note: must use __declspec(dllexport) to make (export) methods as 'public'
__declspec(dllexport) void DoSomethingInC(unsigned short int ExampleParam, unsigned char AnotherExampleParam)
{
printf("You called method DoSomethingInC(), You passed in %d and %c\n\r", ExampleParam, AnotherExampleParam);
}
}//End 'extern "C"' to prevent name mangling
then, compile, and in your C# file do
[DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]
public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);
and then just use it:
using System;
using System.Runtime.InteropServices;
public class Tester
{
[DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]
public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);
public static void Main(string[] args)
{
ushort var1 = 2;
char var2 = '';
DoSomethingInC(var1, var2);
}
}
UPDATE - Feb 22 2019: Since this answer has been getting quite a few upvotes, I decided to update it with a better way of calling the C method. Previously I had suggested using unsafe code, but the safe and correct way is to use MarshalAs attribute for converting a .NET string to a char*. Also, in VS2017 there is no Win32 project anymore, you'll probably have to create a Visual C++ dll or empty project and modify that. Thank you!
You can directly call C functions from C# by using P/Invoke.
Here's a short how-to on creating a C# lbrary that wraps around a C dll.
Create a new C# Library project (I'll call it "Wrapper")
Add a Win32 project to the solution, set application type to: DLL (I'll call it "CLibrary")
You can remove all the other cpp/h files since we won't need them
Rename the CLibrary.cpp file to CLibrary.c
Add a CLibrary.h header file
Now we need to configure the CLibrary project, right-click it and go to properties, and select Configuration: "All Configurations"
In Configuration Properties > C/C++ > Precompiled headers, set Precompiled Headers to: "Not using Precompiled Headers"
In the same C/C++ branch, go to Advanced, change Compile As to: "Compile as C code (/TC)"
Now in the Linker branch, go to General, and change Output File to: "$(SolutionDir)Wrapper\$(ProjectName).dll", this will copy the built C DLL to the C# project root.
CLibrary.h
__declspec(dllexport) unsigned long ConnectSession(unsigned long handle,
unsigned char * publicKey,
unsigned char publicKeyLen);
CLibrary.c
#include "CLibrary.h"
unsigned long ConnectSession(unsigned long handle,
unsigned char * publicKey,
unsigned char publicKeyLen)
{
return 42;
}
Right-click CLibrary project, build it, so we get the DLL in the C# project directory
Right-click C# Wrapper project, add existing item, add CLibrary.dll
Click on CLibrary.dll, go to the properties pane, set "Copy to output Directory" to "Copy Always"
It's a good idea to make the Wrapper project dependent on CLibrary so CLibrary gets built first, you can do that by right-clicking the Wrapper project, going to "Project Dependencies" and checking "CLibrary".
Now for the actual wrapper code:
ConnectSessionWrapper.cs
using System.Runtime.InteropServices;
namespace Wrapper
{
public class ConnectSessionWrapper
{
[DllImport("CLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
static extern uint ConnectSession(uint handle,
[MarshalAs(UnmanagedType.LPStr)] string publicKey,
char publicKeyLen);
public uint GetConnectSession(uint handle,
string publicKey,
char publicKeyLen)
{
return ConnectSession(handle, publicKey, publicKeyLen);
}
}
}
Now just call GetConnectSession, and it should return 42.
Result:
Okay well, Open VS 2010, Goto File -> New -> Project -> Visual C++ -> Win32 -> Win32 Project and give it a name (HelloWorldDll in my case), Then in the window that follows under Application Type choose 'DLL' and under Additonal Options choose 'Empty Project'.
Now goto your Solution Explorer tab usually right hand side of VS window, right click Source Files -> Add Item -> C++ file (.cpp) and give it a name (HelloWorld in my case)
Then in the new class paste this code:
#include <stdio.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL()
{
printf ("Hello from DLL !\n");
}
}
Now Build the project, after navigate to your projects DEBUG folder and there you should find: HelloWorldDll.dll.
Now, lets create our C# app which will access the dll, Goto File -> New -> Project -> Visual C# -> Console Application and give it a name (CallDllCSharp), now copy and paste this code to your main:
using System;
using System.Runtime.InteropServices;
...
static void Main(string[] args)
{
Console.WriteLine("This is C# program");
DisplayHelloFromDLL();
Console.ReadKey();
}
and build the program, now that we have both our apps built lets use them, get your *.dll and your .exe (bin/debug/.exe) in the same directory, and execute the application output should be
This is C# program
Hello from DLL !
Hope that clears some of your issues up.
References:
How to create a DLL library in C and then use it with C#
NOTE : BELOW CODE IS FOR MULTIPLE METHODS FROM DLL.
[DllImport("MyLibc.so")] public static extern bool MyLib_GetName();
[DllImport("MyLibc.so")] public static extern bool MyLib_SetName(string name);
[DllImport("MyLibc.so")] public static extern bool MyLib_DisplayName(string name);
public static void Main(string[] args)
{
string name = MyLib_GetName();
MyLib_SetName(name);
MyLib_DisplayName(name);
}
The P/Invoke method has been described extensively and repeatedly, ok so far.
What I'm missing here is, that the C++/CLI method has a big advantage: Calling safety.
In contrast to P/Invoke, where the call of the C funtion is like shooting blind into the sky (if this comparison is allowed), nobody will check the function arguments when calling the C function.
Using C++/CLI in this case means normally, you include a headerfile with the functions prototypes you want to use. If you are calling the C function with wrong/to much /to few arguments, the compiler will tell you.
I don't think you can say in general which is the better method, honestly I don't like either of them.

How to create a C# functions DLL and then use this functions in C?

I'm trying to create a DLL exposing some static functions to use then in C.
Recently I read an article of Microsoft named "An Overview of Managed/Unmanaged Code Interoperability" and in this there is no a clear explanation on how to "Exposing a Managed API as a Flat API".
I installed this plugin to Visual Studio (https://www.nuget.org/packages/UnmanagedExports) but I still can't compile a project in C.
My C# project exposes a function like this:
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
namespace libcallcstest
{
public class Class1
{
[DllExport("add", CallingConvention = CallingConvention.Cdecl)]
public static int add(int a, int b)
{
return a + b;
}
}
}
After building project, result these three files:
libcallcstest.dll
libcallcstest.pdb
libcallcstest.tlb
My C code is:
#include <stdio.h>
#include <stdlib.h>
int add(int, int);
int main(int argc, char** argv) {
int z = add(2,5);
printf("%d\n", z);
return (EXIT_SUCCESS);
}
And finally when I try to compile this file with:
gcc -o main.exe main.c -lcallcstest
Not work properly, files created by building the C# project are in the same folder as the main.c file.
Pleas any help!!!
One way to go: you may want to host CLR in your process. I would recommend against it though, because hosting is not the easiest procedure out there.
Also it's often not really needed or you can use some slower methods to communicate with .Net code from unmanaged environment (for example, present your library as a local server and access it through network interfaces. As I see it that way you'll have ten times less work to do).
Or you could go with your original variant using utilities to help you like mentioned here.

Call native method from dll

I am trying to use MimeTex.dll in Mixed Mode of C++/CLI project. I include the dll by:
#pragma comment(lib,"MimeTex.dll")
and I tried to call this method:
CreateGifFromEq("expression","path");
but the compiler inform that it doesn't know CreateGifFromEq() method.
I didn't find resources in the web in how to use MimeTex.dll in C++. I just find how to use it with C# in this link by Pinvok like:
[System.Security.SuppressUnmanagedCodeSecurity()]
internal class NativeMethods
{
private NativeMethods()
{ //all methods in this class would be static
}
[System.Runtime.InteropServices.DllImport("MimeTex.dll")]
internal static extern int CreateGifFromEq(string expr, string fileName);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
internal extern static IntPtr GetModuleHandle(string lpModuleName);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
internal extern static bool FreeLibrary(IntPtr hLibModule);
}
and then call it like:
NativeMethods.CreateGifFromEq(equation, tempGifFilePath);
How I can call it without Pinvok in mixed mode of C++/CLI?
Surely you meant to write:
#pragma comment(lib,"MimeTex.lib")
In other words, you pass the .lib file to the linker rather than the .dll. When you compiled the DLL, a .lib file will have been generated.
But that's not your immediate problem. The compiler has no declaration for CreateGifFromEq. That's because you have not included the library's header file in your C++ code. Doing that should resolve the issue.
If all you need is that one function then it should be trivial to declare it in your C++ code.
__declspec(dllimport) extern int CreateGifFromEq(char *expr, char *fileName);
You probably will need to wrap that in an extern "C" block too.
Actually, having looked at the library, the header file that is supplied with the source does not declare that function, even though it's present in the library.
In C++/CLI you can use P/Invoke as in C#.
If you don't want to use P/Invoke, the other way is to load the DLL with LoadLibrary and get pointers to the functions with GetProcAddress.
Here's the equivalent of the C# code in C++/CLI:
using namespace System;
using namespace System::Runtime::InteropServices;
private ref class NativeMethods abstract sealed
{
internal:
[DllImport("MimeTex.dll")]
static int CreateGifFromEq(String^ expr, String^ fileName);
[DllImport("kernel32.dll")]
static IntPtr GetModuleHandle(String^ lpModuleName);
[DllImport("kernel32.dll")]
[returnvalue: MarshalAs(UnmanagedType::Bool)]
static bool FreeLibrary(IntPtr hLibModule);
};
As you can see it's almost identical to the C# code except for some minor syntax changes.
Here's how you would use LoadLibrary and GetProcAddress (which has the advantage of not requiring marshaling, which is unnecessary in C++ anyway):
HMODULE hModule = LoadLibrary(L"MimeTex.dll");
int (*fpCreateGifFromEq)(WCHAR*, WCHAR*) = GetProcAddress(hModule, "CreateGifFromEq");
(*fpCreateGifFromEq)(L"expression", L"filename");
You don't need to do that for GetModuleHandle and FreeLibrary, because they're from kernel32.dll so you can just include Windows.h and call them normally.
There's a cleaner way of doing things:
Create a C++/CLI project. It will be your interface between C# and C++ (this is what C++/CLI is aimed at).
Make your C# project have a reference to this new assembly.
Inside your new C++/CLI project, configure project options so as you point to the directory with mimetex .h files for the compiler to compile your project, and the directory with .lib/.dll for the linker.
Inside your C++/CLI part, you just write C++ inside the functions. They are exposed to your C# assembly, but in the inside it's just calls to C++ functions as usual.
(You should check a C++CLI tutorial to know what to do with strings to convert them to native char or std::string. I remember I had to look at that.)*
In your C# part, you just call the C++CLI project assembly exposed functions like normal C# functions, without noticing it's native code called behind.

P/Invoke g++ from mono in ubuntu OS

Is it possible to invoke a g++ executable file's function from mono in Ubuntu? Note that both C++ and C# code compiled in Ubuntu Operation System.
C++ application source:
#include <stdlib.h>
static int32_t Sum(int32_t a, int32_t b){
return a + b;
}
/*
*
*/
int main(int argc, char** argv) {
return (EXIT_SUCCESS);
}
C# mono application source:
using System;
using System.Runtime.InteropServices;
namespace MonoCsTest
{
class MainClass
{
[DllImport("/home/.../MonoCsTest/bin/Debug/testcpp")]
public static extern Int32 Sum(Int32 a, Int32 b);
public static void Main (string[] args)
{
Console.WriteLine (" 5 + 6 = " + Sum(5,6));
}
}
}
This throws DllNotFoundException
You need to compile the library as a shared library: a static library can't be loaded at runtime with P/Invoke.
The fact that you added a main() function suggests that you're compiling the code into an executable instead.
So the first thing for you is to learn how to compile a shared library, you can try something like:
gcc -shared -o libtestcpp.so testcpp.cpp
Then change the DllImport name to the path to the complete library name:
DllImport("/home/yourlogin/MonoCsTest/bin/Debug/libtestcpp.so")
The other mistake you made is not considering the C++ manadated name mangling: the simpler solution here is to export Sum() as a C function surrounding it with extern "C" {}.
To diagnose such mistakes it is often useful to enable the debug logging from mono using:
MONO_LOG_LEVEL="debug" MONO_LOG_MASK="dll" mono yourprog.exe
As long as a symbol is exported and Mono can understand the parameters (enough to marshal data to/from them), then yes, you can do this. I think the Mono pages on PInvoke actually mention that you can invoke functions exported by both libraries and executables, including an executable that embeds Mono.
You need to verify that the symbol is indeed being exported, and more importantly, that it is not mangled or you match that in the C# side. That's the spot I've had the most stupid problems with.

Categories

Resources