分享
 
 
 

在C#里面调用带有回调函数和自定义结构体的DLL的例程

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

开发环境: 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;

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; //从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(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;

}

//----------------DLL需要使用的结构体的定义----------------------------

//---------------------------------------------------------------------

//----------------声明两个委托,相当于VC里面的回调函数------------------

public delegate void OnSmgMsg(IntPtr css);

public delegate void OnLogMsg(string str);

//---------------------------------------------------------------------

class Class1

{

//--------------------------------------------------------------------------

//--------------DLL中的API函数声明------------------------------------------

// 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();

//--------------DLL中的API函数声明------------------------------------------

//--------------------------------------------------------------------------

//-------------这儿使用IntPtr实现与结构体之间的转换----------------

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应答消息

}

else if ( nCmdId == 0x0000005) {

// Deliver消息

}

return;

}

//------------第二个委托函数的定义-------------------------------

public static void OnLog(string str)

{

Console.WriteLine(str);

return;

}

///

/// 应用程序的主入口点。

///

[STAThread]

static void Main(string[] args)

{

Class1 c1 = new Class1();

OnSmgMsg pt = new OnSmgMsg(OnMsg);

OnLogMsg pLog = new OnLogMsg(OnLog);

//-------------启动模块,连接网关,收到消息以后会触发OnSmgMsg函数------------------------------

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消息----------

nRetCode = Cmpp2Submit(1, 1,

1, 3, "2939",

0, "", 0,

0, 0, "901234",

"02", "000020", "",

"", "01850", 1,

"8613660617374", 8, "测试消息");

if ( 0 == nRetCode)

{

Console.WriteLine("Submit Success.");

}

else {

Console.WriteLine("Submit Fail.");

}

// Sleep5秒钟

Thread.Sleep(5000);

}

//-----释放连接-----

Cmpp2Release();

}

else {

Console.WriteLine("Connect SMG Fail With Error: " + nRetCode);

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有