Example Analysis of C# callback function and API Application
This article mainly shows you the "example analysis of C# callback function and API application", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of C# callback function and API application".
Development environment: WinXP Pro (SP2 English version) + VS.NET 2003 Chinese version
Interface Library version: CMPP2.0 API second Edition (V2.6)
This routine demonstrates how to call the API application of the C # callback function developed by VC6.0 in C #, and the parameters of the callback function contain the structure, which is implemented using the delegate of C # and the IntPtr method.
Since I have only been using C # for two days, this is the * C # program I have written, so the routine may be a little rough, but there is no problem with compiling and running.
The API application of CMPP2.0 is encapsulated into a standard C call method, which provides the following three interfaces, as long as it is used with CMPPAPI.dll.
How to call the API application in C#, how to declare the structure, how to use the delegate to implement the C# callback function, and how to implement the C# callback function using the custom structure as a parameter, please check the routine source code carefully. Note: CMPPAPI.dll should be placed in the same directory as the executable file, or in a directory where the executable file can be found, or under the system directory (for example: C:\ windows\ system32).
Here is the complete calling code under C#
/ / Class1.cs using System; / / this namespace contains some necessary collections for calling API in Visual C # using System.Runtime.InteropServices; / / the namespace using System.Threading; namespace CMPPAPI_Sample_CSharp {[StructLayout (LayoutKind.Sequential, Pack = 1)] public struct CMPP_HEAD {public uint nTotalLength; public uint nCommandId; public uint nSeqId required to use the Sleep method } [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 nTimeSt} [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 = 1)] 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 } these are all the contents of this article entitled "sample Analysis of C# callback functions and API applications". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!