5

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.

Kapil
  • 51
  • 2
  • You mentioned "C++ class" but the code is referencing something that looks like a global function, `ZK_NET_DEV_SetEventCallBack`. There's no class here. – Dialecticus Jan 22 '14 at 07:58
  • You might not read the question properly. ZK_NET_DEV_SetEventCallBack is a dll method. And, this dll method accessing another c++ class method like 'OnEventCallBack', that are declared and defined in c++ code. So, is there any way to directly use this class into my c# application. – Kapil Jan 22 '14 at 08:04
  • Through COM you can access a C++ class and its methods, – fhnaseer Jan 22 '14 at 08:14
  • I don't know how to access a C++ class and its methods using COM, Can you please guide me in doing so. – Kapil Jan 22 '14 at 08:25
  • Please, post the relevant information from the C(++) header file of the DLL - that is, the method definition and the callback method definition. A simple method call like this definitely does not need a C++.NET intermediate :) – Luaan Jan 22 '14 at 10:05
  • http://stackoverflow.com/questions/4642702/wrapping-unmanaged-c-with-c-cli-a-proper-approach – Ivan Jan 22 '14 at 10:09
  • Below is full user manual to use these c++ dll. – Kapil Jan 22 '14 at 10:17
  • This function is used for event data callback. HI_S32 ZK_NET_DEV_SetEventCallBack(HI_U32 u32HandleHI_ON_EVENT_CALLBACK cbEventCallBack, HI_VOID* pUserData); Parameters u32Handle [IN] Operation handle,cbEventCallBack [IN] Event data callback function,pUserData [IN] User data – Kapil Jan 22 '14 at 10:28
  • Callback Function typedef HI_S32 (*HI_ON_EVENT_CALLBACK) ( HI_U32 u32Handle, HI_U32 u32Event, HI_VOID* pUserData ); Callback Function Parameters u32Handle Operation handle u32Event Event Macro Definition Value Meaning ZK_NET_DEV_CONNECTING 0 Connecting ZK_NET_DEV_CONNECTED 1 Connected ZK_NET_DEV_CONNECT_FAILED 2 Connection failed ZK_NET_DEV_ABORTIBE_DISCONNECTED 3 Disconnected ZK_NET_DEV_NORMAL_DISCONNECTED 4 Disconnected ZK_NET_DEV_RECONNECTING 5 Reconnecting ZK_NET_DEV_RECORD_START 6 Recording started ZK_NET_DEV_RECORD_STOP 7 Recording stopped pUserData User data – Kapil Jan 22 '14 at 10:28
  • You should edit your question and add this information to the question itself.. – Ivan Jan 22 '14 at 10:59
  • It really depends on how your c++ dll and export and conventions are done. Do you have access to c++ code? – Mauro Sampietro Jan 22 '14 at 12:01
  • Yes, I do access to my c++ code. And, i want to create my application using its dll and method in c#. – Kapil Jan 22 '14 at 12:18

1 Answers1

1

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?

Lex Li
  • 60,503
  • 9
  • 116
  • 147