欢迎光临
我们一直在努力

在C#里面调用带有回调函数和自定义结构体的DLL的例程-.NET教程,C#语言

建站超值云服务器,限时71元/月

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

}

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在C#里面调用带有回调函数和自定义结构体的DLL的例程-.NET教程,C#语言
分享到: 更多 (0)