Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a C++ class in C# windows application?

Tags:

c#

I'm trying to use c++ class method in my c# windows application

I use this

[DllImport("ZKNetLib.dll", EntryPoint = "ZK_NET_DEV_SetEventCallBack")]
public static extern int ZK_NET_DEV_SetEventCallBack(int u32Handle, ref OnEventCallBack u32Event, IntPtr pUserData);

to import the dll and their method

But, in dll method there are some other methods are also used, which i find very tough to find out what exactly is happening. Like, in above import there is "OnEventCallBack" method that dll is using. So, is there any way to use these c++ class and header files in my windows application.

I, heard of using c# application class in c++ by making the class object and using there namespace.

So, i was wondering there should be any way of using these header file and class into my c# application too?

Below is the edited question

ZK_NET_DEV_SetDataCallBack This function is used for information data callback. HI_S32 ZK_NET_DEV_SetDataCallBack ( HI_U32 u32Handle HI_ON_DATA_CALLBACK cbDataCallBack, HI_VOID* pUserData );

Parameters u32Handle [IN] Operation handle cbDataCallBack [IN] Information data callback function pUserData [IN] User data

Callback Function typedef HI_S32 (*HI_ON_DATA_CALLBACK)( HI_U32 u32Handle, HI_U32 u32DataType, HI_U8* pu8Buffer, HI_U32 u32Length, HI_VOID* pUserData );

Callback Function Parameters u32Handle Operation handle u32DataType Data type Macro Definition Value Meaning ZK_NET_DEV_MOTION_DETECTION 0 Motion detection alarm ZK_NET_DEV_INPUT_ALARM 1 Input alarm ZK_NET_DEV_KEEP_ALIVE 2 Heartbeat packet pu8Buffer Data. If the value of u32DataType is ZK_NET_DEV_MOTION_DETECTION, pu8Buffer is stored as HI_S_ALARM_MD. typedef struct { HI_U32 u32Area; //Area HI_U32 u32X; //x coordinate HI_U32 u32Y; //y coordinate HI_U32 u32Width; //Rectangular width HI_U32 u32Height; //Rectangular height } HI_S_ALARM_MD; The maximum value of u32Area is 4. Related data is as follows: Macro Definition Value Meaning ZK_NET_DEV_MOTION_AREA_1 1 Area 1 ZK_NET_DEV_MOTION_AREA_2 2 Area 2 ZK_NET_DEV_MOTION_AREA_3 3 Area 3 ZK_NET_DEV_MOTION_AREA_4 4 Area 4 u32Length Data length. If the value of u32DataType is ZK_NET_DEV_MOTION_DETECTION and alarms are generated in two areas, the value of u32Length is: u32Length = 2*sizeof(HI_S_ALARM_MD) u32DataType User data

Return Values HI_SUCCESS is returned for a successful operation and HI_ FAILURE for a failed operation.

like image 315
Kapil Avatar asked Nov 11 '22 14:11

Kapil


1 Answers

SWIG by far is the most reliable option you can use (recommended by Mono guys as it supports even Xamarin.iOS and Xamarin.Android)

http://www.swig.org/

As it has been used in too many scenarios (Java, C# and so on).

There are of course other alternatives, such as cxxi from Mono and so on, but if SWIG works for you then why use them?

like image 82
Lex Li Avatar answered Nov 14 '22 22:11

Lex Li