日期:2013-03-11  浏览次数:20545 次

开发环境: WinXP Pro(SP2英文版) + VS.NET 2003中文版
接口库版本: CMPP2.0 API第二版(V2.6)
该例程演示了如何在C#里面调用VC6.0开发的带回调函数的API,而且回调函数的参数包含结构体,使用C#的委托和IntPtr方法实现.
由于我使用C#刚两天,这是我写的第一个C#程序,因此例程写的可能有点粗糙,但是编译和运行完全没有问题.
CMPP2.0的API封装成了标准C调用的方法,提供以下三个接口,使用的时候只要有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();
在C#里面如何调用API,如何声明结构体,如何使用委托实现回调函数,如何实现使用自定义结构体作为参数的回调函数,请仔细查看例程源码。注意:CMPPAPI.dll要和可执行文件放到同一个目录下,或者放到可执行文件能找到的目录,或者放到系统目录下(如:C:\windows\system32).


下面是C#下面的完整的调用代码

//Class1.cs

using System;
// 该名称空间包含了在Visual C#中调用API的一些必要集合
using System.Runtime.InteropServices;
// 使用Sleep方法需要的命名空间
using System.Threading;

namespace CMPPAPI_Sample_CSharp
{
//---------------------------------------------------------------------
//---------以下是DLL中需要使用的结构体的定义---------------------------
//--------Pack = 1表示结构体按一个字节对齐----------------------------
[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;
publi