Click here to Skip to main content
15,120,268 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody

I have a 3rd party dll, which I want to use in my app(.net framework, c#4.0). Dll is used to control IR learner hardware component. Below is the header file for the dll.

extern "C" {

/******************** CALLBACKS **********************/
//Callback called when the status of the PIR-1 driver is affected.
typedef void (__stdcall *fStatusCB) ( const char * serial, unsigned char status, const char *message);

//fIrSentCB is called when an IR code was sent. 
typedef void (__stdcall *fIrSentCB) ( const char * serial, unsigned char status);

//fIrReceivedCB is called when an IR code is received
typedef void (__stdcall *fIrReceivedCB) ( const char * serial, const char * irCode);

//fCCFLearnCB is called during the learning process.
typedef void (__stdcall *fCCFLearnCB) ( const char * serial, int progress, const char * ccf);

//fDigitalIOCB is called in response to a digital IO command. (PIR-1 DEVELOPMENT)
typedef void (__stdcall *fDigitalIOCB) ( const char * serial, unsigned char cmd, unsigned char val);

/******************** FUNCTIONS **********************/
// To start the PIR-1 driver call open ONCE
void _stdcall pir_open(fStatusCB statusCB, fIrSentCB irSentCB, fIrReceivedCB irReceivedCB, fCCFLearnCB CCFLearnCB, fDigitalIOCB digitalIOCB );

// Sets the PIR-1 with specified serial into learning mode if state =1, pass 0 to stop learning */
void _stdcall pir_learnCCF( const char * serial, unsigned char state );
}


Can somebody give me some hints or sample code on how to call functions pir_open and pir_learnCCF from managed(C#) code. Any idea will be appreciated.

Uroš Bregar
Posted

1 solution

have a look at the PInvoke Interop Assistant [^] it'll help you.
For the header you provided the toolkit generated this C# code:
C#
/// Return Type: void
///serial: char*
///status: unsigned char
///message: char*
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
public delegate void fStatusCB([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string serial, byte status, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string message);

/// Return Type: void
///serial: char*
///status: unsigned char
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
public delegate void fIrSentCB([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string serial, byte status);

/// Return Type: void
///serial: char*
///irCode: char*
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
public delegate void fIrReceivedCB([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string serial, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string irCode);

/// Return Type: void
///serial: char*
///progress: int
///ccf: char*
public delegate void fCCFLearnCB([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string serial, int progress, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string ccf);

/// Return Type: void
///serial: char*
///cmd: unsigned char
///val: unsigned char
public delegate void fDigitalIOCB([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string serial, byte cmd, byte val);

public partial class NativeMethods {
    
    /// Return Type: void
    ///statusCB: fStatusCB
    ///irSentCB: fIrSentCB
    ///irReceivedCB: fIrReceivedCB
    ///CCFLearnCB: fCCFLearnCB
    ///digitalIOCB: fDigitalIOCB
    [System.Runtime.InteropServices.DllImportAttribute("<unknown>", EntryPoint="pir_open")]
public static extern  void pir_open(fStatusCB statusCB, fIrSentCB irSentCB, fIrReceivedCB irReceivedCB, fCCFLearnCB CCFLearnCB, fDigitalIOCB digitalIOCB) ;

    
    /// Return Type: void
    ///serial: char*
    ///state: unsigned char
    [System.Runtime.InteropServices.DllImportAttribute("<unknown>", EntryPoint="pir_learnCCF")]
public static extern  void pir_learnCCF([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string serial, byte state) ;

}


remember to replease the "<unknown>" string with the name of your library file.
   
v3
Comments
koleraba 30-Sep-11 15:30pm
   
Thank you for yor time Simon. It worked like a charm.
P.S. PInvoke Interop Assistant is very usefull, especially when the external dll is complex. It will save me quite some time.
[no name] 1-Oct-11 3:51am
   
My 5. Excellent Effort with result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900