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.
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.
I'm having an issue building the Unity3d project in Xcode (to test on a device) with a Objective C plug-in I've made.
Here are the files:
The TestPlugin.h file:
#import <Foundation/Foundation.h>
#interface TestPlugin : NSObject
+(int)getGoodNumber;
#end
The TestPlugin.m file:
#import "TestPlugin.h"
#implementation TestPlugin
+(int)getGoodNumber
{
return 1111111;
}
#end
And finally the C# script in unity that's supposed to print out the value that getGoodNumber() returns:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class PluginTest : MonoBehaviour
{
[DllImport ("__Internal")]
public static extern int getGoodNumber();
void OnGUI()
{
string goodNum = getGoodNumber().ToString();
GUILayout.Label (goodNum);
}
}
As much as I can tell there shouldn't be any problem with the code. But even though I followed many different tutorials, when I try to compile I get an error in Xcode:
Undefined symbols for architecture armv7:
"_getGoodNumber", referenced from:
RegisterMonoModules() in RegisterMonoModules.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried a million different things and nothing seems to help. As much as I could read from other tutorials I do not need any special settings for Xcode and I can leave them same as it was for the Unity project without the plug-in.
I would also like to clarify a few things:
The plugin files are located in /Plugins/iOS/ folder in Unity3d
The build target was iPad Air 2 with the latest iOS, so no problem should arise there. Also using the newest Xcode on the newest OS X version.
I have the newest available version of Unity3d, and if that matters - I do have the Pro version of Unity3d.
The project works if the plugin is removed, so it's not a problem between Unity3d and Xcode.
I do not think I need to use extern "C" wrapper in Objective-C code as it's a ".m" file, not ".mm", so there shouldn't be a problem of name-mangling.
The plug-in was created in Xcode through the "Cocoa Touch Static Library" template. I was able to successfully build the Xcode project by itself before importing it to Unity3d.
If someone encountered such a problem as solved it, I would love to hear the solution.
You've written an "objective-c" class and method, but that can not be exposed to Unity. You need to create a "c" method (which could then call an objective-c method if needed).
Eg:
plugin.m:
long getGoodNumber() {
return 111;
}
Here is a fuller example that demonstrates out params to get a gyro.
Let's make a motion manager to get gyro (faked out for now). This would be standard objective-c:
MyMotionManager.h
#interface MyMotionManager : NSObject { }
+(MyMotionManager*) sharedManager;
-(void) getGyroXYZ:(float[])xyz;
#end
MyMotionManager.m:
#implementation MyMotionManager
+ (MyMotionManager*)sharedManager
{
static MyMotionManager *sharedManager = nil;
if( !sharedManager )
sharedManager = [[MyMotionManager alloc] init];
return sharedManager;
}
- (void) getGyroXYZ:(float[])xyz
{
// fake
xyz[0] = 0.5f;
xyz[1] = 0.5f;
xyz[2] = 0.5f;
}
#end
Now lets expose it via a C external reference (no need for extern as it's in a .m (not a .mm):
MyMotionManagerExterns.m:
#import "MyMotionManager.h"
void GetGyroXYZ(float xyz[])
{
[[MyMotionManager sharedManager] getGyroXYZ:xyz];
}
Finally, in Unity C# lets call it:
MotionPlugin.cs:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class MotionPlugin
{
[DllImport("__Internal")]
private static extern void GetGyroXYZ(float[] xyz);
public static Vector3 GetGyro()
{
float [] xyz = {0, 0, 0};
GetGyroXYZ(xyz);
return new Vector3(xyz[0], xyz[1], xyz[2]);
}
}
I want to bind an objective c library (For using a cable) in xamarin. I am new to xamarin platform, Can anyone help me to convert the below .h file to "ApiDefinition.cs" in Xamarin binding project.
#import <UIKit/UIKit.h>
#ifndef CABLE_
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#protocol CableManagerDelegate;
/*
This protocol), describes the main interface to the Cable Socket Manager layer.
To use, call factory method below [CableManager sharedInstance]
*/
#protocol CableManagerProtocol <NSObject>
// set delegate for cable connect callbacks
-(void)setDelegate:(id < CableManagerDelegate >) delegate;
-(BOOL)isCableConnected;
-(NSString *)getAccessoryFirmwareVersion;
#end
#protocol CableManagerDelegate <NSObject>
//Cable was connected
- (void) cableConnected:(NSString *)protocol;
// Cable was disconnected and/or application moved to background
- (void) cableDisconnected;
#end
#interface CableManager : NSObject
+ (id < CableManagerProtocol >)sharedInstance;
#end
Can't write it for you, but the sample below is the general pattern which you can learn more about in the guide linked below. One of your key challenges is going to be making sure the callback on the delegate fires. To Map this, check out the "Binding Protocols" section.
http://developer.xamarin.com/guides/ios/advanced_topics/binding_objective-c/binding_objc_libs/
ApiDefinition.cs
using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;
using System;
namespace MyNamespace
{
[BaseType (typeof (NSObject))]
interface MyObjCWrapper
{
[Export("initWithArg1:arg2:arg3:")]
void Constructor(string first, string second, string third);
[Export("mySelectorTaking1arg:")] // note colon, takes 1 arg
void DoSomethingWith1Arg(string filePath);
[Export("getSomething")] // note no colon, takes 0 args
int GetSomething();
}
To complete this binding, you should add the native library to the
project. You can do this by adding the native library to your project,
either by dragging and dropping the native library from Finder onto
the project in the solution explorer, or by right-clicking the project
and choosing Add > Add Files to select the native library. Native
libraries by convention start with the word "lib" and end with the
extension ".a". When you do this, Xamarin Studio will add two files:
the .a file and an automatically populated C# file that contains
information about what the native library contains:
You will end up with a file like this (libLibraryName.linkwith.cs):
using System;
using MonoTouch.ObjCRuntime;
[assembly: LinkWith ("libLibraryName.a", SmartLink = true, ForceLoad = true)]
Use Objective-Sharpie. It will do the initial bulk of work for you and you just need to fill in the gaps...
This question already has answers here:
Call a C++ function from C#
(4 answers)
Closed 9 years ago.
I am making a program to control a laser but to do that I need to import a dll into c# and call functions with it. The original program is written in C++ and I just cant figure it out how to do it.
So my questions are:
How to import a dll in c#
how to call the functions from C++ in c#
To make it a bit more clear to you I've added the code from the C++ program.
#include "MarkEzdDll.h"
class CDemoEzdDlg : public CDialog
{
// Construction
public:
CDemoEzdDlg(CWnd* pParent = NULL); // standard constructor
HINSTANCE m_hEzdDLL;//DLLµ÷Óþä±ú
LMC1_INITIAL lmc1_Initial;
LMC1_CLOSE lmc1_Close;
LMC1_LOADEZDFILE lmc1_LoadEzdFile;
LMC1_MARK lmc1_Mark;
LMC1_MARKENTITY lmc1_MarkEntity;
LMC1_GETPREVBITMAP lmc1_GetPrevBitmap;
LMC1_READPORT lmc1_ReadPort;
LMC1_WRITEPORT lmc1_WritePort;
LMC1_SETDEVCFG lmc1_SetDevCfg;
LMC1_SETHATCHPARAM lmc1_SetHatchParam;
LMC1_SETFONTPARAM lmc1_SetFontParam;
LMC1_GETPENPARAM lmc1_GetPenParam;
LMC1_SETPENPARAM lmc1_SetPenParam;
LMC1_CLEARENTLIB lmc1_ClearEntLib;
LMC1_ADDTEXTTOLIB lmc1_AddTextToLib;
LMC1_ADDFILETOLIB lmc1_AddFileToLib;
LMC1_ADDBARCODETOLIB lmc1_AddBarCodeToLib;
LMC1_CHANGETEXTBYNAME lmc1_ChangeTextByName;
LMC1_AXISMOVETO lmc1_AxisMoveTo;
LMC1_AXISCORRECTORIGIN lmc1_AxisCorrectOrigin;
LMC1_GETAXISCOOR lmc1_GetAxisCoor;
LMC1_SAVEENTLIBTOFILE lmc1_SaveEntLibToFile;
in the dialog, there is a button which has this code attached to it:
void CDemoEzdDlg::OnButtonRun()
{
// TODO: Add your control notification handler code here
UpdateData();
if(lmc1_Mark==NULL)
{
return;
}
lmc1_Mark(FALSE);
UpdatePrevBmp();
AfxMessageBox(_T("Mark file finish!"));
}
This is when the laser should start.
How to get this in c#?
(It was not very clear from your post, but I am guessing you want to call C++ functions from your C# code.)
PInvoke is perfect for this. Here is a nice tutorial: click.
You don't import the DLL in your project settings; instead you call it from your source code. You do have to make sure that the DLL is in the same folder as your project's output (.exe) file, of course.
In a nutshell, if you define a C method like this in foo.dll:
extern "C" __declspec(dllexport) bool __stdcall GetTrue()
{
return true;
}
Then your C# program can declare it like this:
[DllImport("foo.dll")]
static public extern bool GetTrue();
and simply use GetTrue() to call it. (You also need to add using System.Runtime.InteropServices; in the same file)
Of course it gets more complicated when you use data types that differ between C# and C++ (like strings, objects, etc) but the tutorial covers that pretty nicely.