C Api interface

B

Bart V

Hi,

got this c api interface:

#ifndef _USBCDDLL_HPP
#define _USBCDDLL_HPP


#define DEVICE_COMMANDOK 0
#define DEVICE_IDERROR 1
#define DEVICE_BUSY 2
#define DEVICE_UNKNOWERROR 3

extern "C"
{
typedef void (CALLBACK* CDDeviceChangePROC )(int);

WINUSERAPI VOID WINAPI InitUSBCDLibrary();
WINUSERAPI VOID WINAPI CloseUSBCDLibrary();
WINUSERAPI VOID WINAPI SetCDCallbackProc( CDDeviceChangePROC lpProc );
WINUSERAPI int WINAPI GetDeviceNumber();
WINUSERAPI int WINAPI EnumDevice( int);

// CD Device Operate function below
WINUSERAPI int WINAPI USBCDReset( int ID );
WINUSERAPI int WINAPI USBCDMoveto( int ID, int Index );
WINUSERAPI int WINAPI USBCDGetCDDown( int ID );
WINUSERAPI int WINAPI USBCDLEDON( int ID );
WINUSERAPI int WINAPI USBCDLEDOFF( int ID );
WINUSERAPI int WINAPI USBCDGetStatus( int ID );
}


#endif

now for the regular functions to transform them to use in c# its

/////////////////////////////////////
using System.Runtime.InteropServices;

[DllImport("USBCDDLL.dll")]
private static extern void InitUSBCDLibrary();
//////////////////////////////////////////////

not very difficult, right. But how would I use the "callback" thingie?
please any code example would be very helpfull.

thx
 
M

Mattias Sjögren

Bart,
But how would I use the "callback" thingie?

You use a delegate.

delegate void CDDeviceChangePROC(int i);

[DllImport("USBCDDLL.dll")]
private static extern void SetCDCallbackProc(CDDeviceChangePROC
lpProc);

Just make sure that the delegate object you pass in to
SetCDCallbackProc is kept alive (you hold a reference to it) so it
doesn't get garbage collected as long as it's needed by the unmanaged
API.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top