To invoke a routine in C # with a DLL with a callback function and a custom struct body

Source: Internet
Author: User
Tags definition connect pack sleep
function development environment: WinXP Pro (SP2 English edition) + vs.net 2003 Chinese version
Interface Library version: CMPP2.0 API Second Edition (V2.6)
This routine demonstrates how to invoke the VC6.0 developed API with a callback function in C #, and the parameter of the callback function contains the structure, which is implemented using the delegates and IntPtr methods of C #.
Since I've been using C # for just two days, this is the first C # program I wrote, so the routines may be a bit rough to write, but there's no problem compiling and running at all.
The CMPP2.0 API encapsulates the standard C call method, providing the following three interfaces, which can be used as long as there is CMPPAPI.dll.
#define dllexport extern "C" __declspec (dllexport)
dllexport int __stdcall Cmpp2start (lpctstr pchsmgip, int nmtport, int nmoport, \
LPCTSTR Pchusername, LPCTSTR pchuserpwd, unsigned char uchversion, \
void (__stdcall *onsmgmsg) (cmpp_smgtosp* css), int nconntype, void (__stdcall *onlogfile) (LPCTSTR str));
dllexport int __stdcall cmpp2submit (unsigned char uchpktotal, unsigned char uchpknumber, \
unsigned char uchneedreport, unsigned char uchmsglevel, LPCTSTR Pchserviceid, \
unsigned char Uchfeeusertype, LPCTSTR pchfeeterminalid, unsigned char uchtppid, \
unsigned char uchtpudhi, unsigned char uchmsgfmt, LPCTSTR pchmsgsrc, \
LPCTSTR Pchfeetype, LPCTSTR Pchfeecode, LPCTSTR pchvalidtime, \
LPCTSTR Pchattime, LPCTSTR pchsrcid, unsigned char Uchdestusrtl, \
LPCTSTR pchdestterminalid, unsigned char Uchmsglen, LPCTSTR pchmsgcontent);
dllexport int __stdcall cmpp2release ();
In C # inside how to invoke the API, how to declare the structure, how to use the delegate implementation callback function, how to implement the use of a custom structure as a parameter of the callback function, please carefully look at the example routines code. Note: CMPPAPI.dll to the same directory as the executable file, or to the directory where the executable file can be found, or to the system directory (such as: C:\windows\system32).


Here is the complete call code below C #

Class1.cs

Using System;
This namespace contains some of the necessary collections to invoke the API in Visual C #
Using System.Runtime.InteropServices;
Namespaces needed to use the Sleep method
Using System.Threading;

Namespace Cmppapi_sample_csharp
{
//---------------------------------------------------------------------
---------The following are the definitions of the structure that you need to use in your DLL---------------------------
--------Pack = 1 indicates that the struct body is aligned by one byte----------------------------
[StructLayout (layoutkind.sequential, Pack = 1)]
public struct Cmpp_head
{
public UINT Ntotallength;
public UINT nCommandID;
public UINT Nseqid;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct Cmpp_connect
{
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 6)]
public string ssourceaddr;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 16)]
public string Sauthsource;
public byte cversion;
public UINT Ntimestamp;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_CONNECT_RESP
{
public byte Uchstatus;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 16)]
public string sauthismg;
public byte cversion;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_SUBMIT_RESP
{
public long Nmsgid;
public byte Uchresult;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct Cmpp_status_report
{
public long Nmsgid;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 7)]
public string Sstat;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 10)]
public string Ssubmittime;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 10)]
public string Sdonetime;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 21)]
public string Sdestterminalid;
public UINT Nsmscseq;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct cmpp_mo_msgcontent
{
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 160)]
public string smsgcontent;
Public Cmpp_status_report CSR;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct Cmpp_deliver
{
public long Nmsgid;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 21)]
public string Sdestid;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 10)]
public string Sserviceid;
public byte Uchtppid;
public byte Uchtpudhi;
public byte uchmsgfmt;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 21)]
public string Ssrcterminalid;
public byte Uchregistereddelivery;
public byte Uchmsglength;

Public Cmpp_mo_msgcontent mo_msg;

[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 8)]
public string sreserved;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_SUBMIT
{
public long Nmsgid;
public byte Uchpktotal;
public byte Uchpknumber;
public byte Uchregistereddelivery;
public byte Uchmsglevel;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 10)]
public string Sserviceid;
public byte Uchfeeusertype;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 21)]
public string Sfeeterminalid;
public byte Uchtppid;
public byte Uchtpudhi;
public byte uchmsgfmt;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 6)]
public string smsgsrc;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 2)]
public string Sfeetype;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 6)]
public string Sfeecode;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 17)]
public string Svalidtime;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 17)]
public string Sattime;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 21)]
public string ssrcid;
public byte Uchdstusrtl;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 21*100)]
public string Sdstterminalid;
public byte Uchmsglength;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 160)]
public string smsgcontent;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 8)]
public string sreserved;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct Cmpp_query
{
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 8)]
public string stime;
public byte Uchquerytype;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 10)]
public string Squerycode;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 8)]
public string sreserved;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_QUERY_RESP
{
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 8)]
public string stime;
public byte Uchquerytype;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 10)]
public string Squerycode;
public UINT Nmttlmsg; The total number of messages received from the SP.
public UINT NMTTLUSR; Total number of users received from the SP.
public UINT Nmtscs; Number successfully forwarded.
public UINT NMTWT; Quantity to be forwarded.
public UINT NMTFL; Number of forwarding failures.
public UINT Nmoscs; Number of successful deliveries to SP.
public UINT Nmowt; Number of pending deliveries to the SP.
public UINT NMOFL; The number of failed deliveries to the SP.
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_CANCEL
{
public long Nmsgid;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_CANCEL_RESP
{
public byte Uchsuccessid;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_ACTIVETEST_RESP
{
public byte uchreserved;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct Cmpp_body
{
Public Cmpp_connect_resp Pk_connectresp;
Public Cmpp_submit_resp Pk_submitresp;
Public Cmpp_query_resp Pk_queryresp;
Public Cmpp_deliver Pk_deliver;
Public Cmpp_cancel_resp Pk_cancelresp;
Public Cmpp_activetest_resp Pk_activetestresp;
}

[StructLayout (layoutkind.sequential, Pack = 1)]
public struct CMPP_SMGTOSP
{
Public Cmpp_head Pk_head;
Public Cmpp_body Pk_body;
}
----------------the definition of the structure that the DLL needs to use----------------------------
//---------------------------------------------------------------------


----------------declare two delegates, equivalent to the callback function within VC------------------
public delegate void Onsmgmsg (IntPtr css);
public delegate void Onlogmsg (String str);
//---------------------------------------------------------------------

Class Class1
{
//--------------------------------------------------------------------------
An API function declaration in--------------DLL------------------------------------------
1.Start Func
[DllImport ("CMPPAPI.dll")]
public static extern int Cmpp2start (string pchsmgip, int nmtport, int nmoport,
String Pchusername, String pchuserpwd, byte uchversion,
Onsmgmsg pf, int nconntype, onlogmsg pf2);

2.Submit Func
[DllImport ("CMPPAPI.dll")]
public static extern int Cmpp2submit (byte uchpktotal, byte Uchpknumber,
Byte Uchneedreport, byte Uchmsglevel, string Pchserviceid,
Byte Uchfeeusertype, String pchfeeterminalid, byte uchtppid,
Byte Uchtpudhi, byte uchmsgfmt, string pchmsgsrc,
String Pchfeetype, String Pchfeecode, String pchvalidtime,
String Pchattime, String pchsrcid, byte Uchdestusrtl,
String Pchdestterminalid, Byte Uchmsglen, string pchmsgcontent);

3.Release
[DllImport ("CMPPAPI.dll")]
public static extern int cmpp2release ();
An API function declaration in--------------DLL------------------------------------------
//--------------------------------------------------------------------------

-------------here uses the IntPtr to implement the transition to the struct body----------------
public static void Onmsg (IntPtr ptr)
{
Cmpp_smgtosp CSS = new Cmpp_smgtosp ();
CSS = (CMPP_SMGTOSP) marshal.ptrtostructure (PTR, typeof (Cmpp_smgtosp));

UINT nCmdID = css.pk_head.nCommandId;
Console.WriteLine (CSS.PK_HEAD.NCOMMANDID);
if (nCmdID = = 0x80000004) {
Submit Answer Message
}
else if (nCmdID = = 0x0000005) {
Deliver message
}

Return
}

------------the definition of the second delegate function-------------------------------
public static void OnLog (String str)
{
Console.WriteLine (str);
Return
}

///
The main entry point for the application.
///
[STAThread]
static void Main (string[] args)
{
Class1 C1 = new Class1 ();
Onsmgmsg pt = new Onsmgmsg (onmsg);
Onlogmsg Plog = new Onlogmsg (onlog);

-------------boot module, connect the gateway, the ONSMGMSG function will be triggered after receiving the message------------------------------
int nretcode = Cmpp2start ("127.0.0.1", 7890, 7891, "901234", "1234", (Byte) 0x20, PT, 0, Plog);
if (0 = nretcode)
{
Console.WriteLine ("Connect Smg success\n");

while (true)
{
----------submit a submit message to the Gateway----------
nRetCode = Cmpp2submit (1, 1,
1, 3, "2939",
0, "", 0,
0, 0, "901234",
"02", "000020", "",
"", "01850", 1,
"8613660617374", 8, "test Message");

if (0 = nretcode)
{
Console.WriteLine ("Submit Success.");
}
else {
Console.WriteLine ("Submit Fail.");
}

SLEEP5 seconds
Thread.Sleep (5000);
}

-----Release the connection-----
Cmpp2release ();
}
else {
Console.WriteLine ("Connect SMG Fail with Error:" + nRetCode);
}
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.