c#

位置:IT落伍者 >> c# >> 浏览文章

C#中调用带回调函数和自定义结构体的DLL例程


发布日期:2020年11月06日
 
C#中调用带回调函数和自定义结构体的DLL例程

开发环境:WinXP Pro(SP英文版) + VSNET 中文版

接口库版本:CMPP API第二版(V)

该例程演示了如何在C#里面调用VC开发的带回调函数的API而且回调函数的参数包含结构体使用C#的委托和IntPtr方法实现

由于我使用C#刚两天这是我写的第一个C#程序因此例程写的可能有点粗糙但是编译和运行完全没有问题

CMPP的API封装成了标准C调用的方法提供以下三个接口使用的时候只要有CMPPAPIdll就可以了

#define DllExport extern C __declspec(dllexport)

DllExport int __stdcall CmppStart(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 CmppSubmit(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 CmppRelease();

在C#里面如何调用API如何声明结构体如何使用委托实现回调函数如何实现使用自定义结构体作为参数的回调函数请仔细查看例程源码注意:CMPPAPIdll要和可执行文件放到同一个目录下或者放到可执行文件能找到的目录或者放到系统目录下(如:C:\windows\system)

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

//Classcs

using System;

// 该名称空间包含了在Visual C#中调用API的一些必要集合

using SystemRuntimeInteropServices;

// 使用Sleep方法需要的命名空间

using SystemThreading;

namespace CMPPAPI_Sample_CSharp

{

//

//以下是DLL中需要使用的结构体的定义

//Pack = 表示结构体按一个字节对齐

[StructLayout(LayoutKindSequential Pack = )]

public struct CMPP_HEAD

{

public uint nTotalLength;

public uint nCommandId;

public uint nSeqId;

}

[StructLayout(LayoutKindSequential Pack = )]

public struct CMPP_CONNECT

{

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sSourceAddr;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sAuthSource;

public byte cVersion;

public uint nTimeStamp;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_CONNECT_RESP

{

public byte uchStatus;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sAuthISMG;

public byte cVersion;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_SUBMIT_RESP

{

public long nMsgid;

public byte uchResult;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_STATUS_REPORT

{

public long nMsgid;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sStat;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sSubmitTime;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sDoneTime;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sDestTerminalId;

public uint nSmscSeq;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_MO_MSGCONTENT

{

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sMsgcontent;

public CMPP_STATUS_REPORT csr;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_DELIVER

{

public longnMsgid;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sDestid;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sServiceid;

public byteuchTppid;

public byteuchTpudhi;

public byteuchMsgfmt;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sSrcterminalid;

public byteuchRegisteredDelivery;

public byteuchMsglength;

public CMPP_MO_MSGCONTENT mo_msg;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sReserved;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_SUBMIT

{

public longnMsgid;

public byteuchPkTotal;

public byteuchPkNumber;

public byteuchRegisteredDelivery;

public byteuchMsgLevel;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sServiceId;

public byteuchFeeUserType;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sFeeTerminalId;

public byteuchTpPid;

public byteuchTpUdhi;

public byteuchMsgFmt;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sMsgSrc;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sFeeType;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sFeeCode;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sValidTime;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sAtTime;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sSrcId;

public byte uchDstUsrTl;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = *)]

public string sDstTerminalId;

public byteuchMsgLength;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sMsgContent;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sReserved;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_QUERY

{

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sTime;

public byte uchQueryType;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sQueryCode;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sReserved;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_QUERY_RESP

{

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sTime;

public byte uchQueryType;

[MarshalAs(UnmanagedTypeByValTStr SizeConst = )]

public string sQueryCode;

public uint nMTTLMsg;//从SP接收消息总数

public uint nMTTLUsr;//从SP接收用户总数

public uint nMTScs; //成功转发数量

public uint nMTWT; //待转发数量

public uint nMTFL; //转发失败数量

public uint nMOScs; //向SP成功送达数量

public uint nMOWT; //向SP待送达数量

public uint nMOFL; //向SP送达失败数量

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_CANCEL

{

public long nMsgid;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_CANCEL_RESP

{

public byte uchSuccessId;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_ACTIVETEST_RESP

{

public byte uchReserved;

}

[StructLayout(LayoutKindSequential Pack = )]

publicstruct CMPP_BODY

{

public CMPP_CONNECT_RESP pk_connectresp;

public CMPP_SUBMIT_RESPpk_submitresp;

public CMPP_QUERY_RESPpk_queryre               

上一篇:C# 中数组和类型转换的功能实现

下一篇:C#Windows控件的焦点事件顺序