开发环境:WinXP Pro(SP英文版) + VSNET 中文版
接口库版本:CMPP API第二版(V)
该例程演示了如何在C#里面调用VC开发的C#回调函数的API应用而且回调函数的参数包含结构体使用C#的委托和IntPtr方法实现
由于我使用C#刚两天这是我写的第一个C#程序因此例程写的可能有点粗糙但是编译和运行完全没有问题
CMPP的API应用封装成了标准C调用的方法提供以下三个接口使用的时候只要有CMPPAPIdll就可以了
在C#里面如何调用API应用如何声明结构体如何使用委托实现C#回调函数如何实现使用自定义结构体作为参数的C#回调函数请仔细查看例程源码注意:CMPPAPIdll要和可执行文件放到同一个目录下或者放到可执行文件能找到的目录或者放到系统目录下(如:C:\windows\system)
下面是C#下面的完整的调用代码
//Classcs
usingSystem;
//该名称空间包含了在VisualC#中调用API的一些必要集合
usingSystemRuntimeInteropServices;
//使用Sleep方法需要的命名空间
usingSystemThreading;
namespaceCMPPAPI_Sample_CSharp
{
[StructLayout(LayoutKindSequentialPack=)]
publicstructCMPP_HEAD
{
publicuintnTotalLength;
publicuintnCommandId;
publicuintnSeqId;
}
[StructLayout(LayoutKindSequentialPack=)]
publicstructCMPP_CONNECT
{
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsSourceAddr;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsAuthSource;
publicbytecVersion;
publicuintnTimeStamp;
}
[StructLayout(LayoutKindSequentialPack=)]
publicstructCMPP_CONNECT_RESP
{
publicbyteuchStatus;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsAuthISMG;
publicbytecVersion;
}
[StructLayout(LayoutKindSequentialPack=)]
publicstructCMPP_SUBMIT_RESP
{
publiclongnMsgid;
publicbyteuchResult;
}
[StructLayout(LayoutKindSequentialPack=)]
publicstructCMPP_STATUS_REPORT
{
publiclongnMsgid;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsStat;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsSubmitTime;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsDoneTime;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsDestTerminalId;
publicuintnSmscSeq;
}
[StructLayout(LayoutKindSequentialPack=)]
publicstructCMPP_MO_MSGCONTENT
{
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsMsgcontent;
publicCMPP_STATUS_REPORTcsr;
}
[StructLayout(LayoutKindSequentialPack=)]
publicstructCMPP_DELIVER
{
publiclongnMsgid;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsDestid;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsServiceid;
publicbyteuchTppid;
publicbyteuchTpudhi;
publicbyteuchMsgfmt;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsSrcterminalid;
publicbyteuchRegisteredDelivery;
publicbyteuchMsglength;
publicCMPP_MO_MSGCONTENTmo_msg;
[MarshalAs(UnmanagedTypeByValTStrSizeConst=)]
publicstringsReserved;
}