欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > c# >内容正文

c#

c#调用c 编写的dll函数各种参数传递问题 -尊龙游戏旗舰厅官网

发布时间:2024/10/12 c# 24 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 c#调用c 编写的dll函数各种参数传递问题 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

----在网上查了一个星期,终于找到解决方法

1. 不返回值的参数

c 原型:

bool    sendnewsms(char *sztel, char *szmessage);

c#引用;

[dllimport( "cdmacard.dll",entrypoint="sendnewsms")] public     static     extern    bool sendnewsms(string phone,string msg);

2. 带返回值(char *)

c 原型:
bool getcarderrormessage(char *szerrormessage , int errorcode);

c#引用

[dllimport( "cdmacard.dll",entrypoint="getcarderrormessage")]      public     static     extern    int getcarderrormessage(stringbuilder msg,int errorcode);

stringbuilder buf = new stringbuilder(1024);//指定的buf大小必须大于可能的最大长度        getcarderrormessage(buf,1);

3. 带返回值(其他类型)

c 原型:

   bool getsmssavestation (int *nsmsstation);

c#引用

   [dllimport( "cdmacard.dll",entrypoint="getsmssavestation")]     public    static    extern   bool getsmssavestation(ref int nstation);

4. 传递结构体指针(c 填充) c 原型: struct net_info_struct {    dword ndurationtime; //持续时间    double nreceivebyte; //接收字节    double nsendbyte;   //发送字节 };  bool netgetconnectdetail(net_info_struct *lpnetinfo); c#引用   public struct net_info_struct {    public uint ndurationtime; //持续时间    public double nreceivebyte; //接收字节    public double nsendbyte;   //发送字节 } [dllimport( "cdmacard.dll",entrypoint="netgetconnectdetail")]          public    static    extern   int netgetconnectdetail(ref net_info_struct pnetinfo);                  net_info_struct netinfo = new net_info_struct();          netgetconnectdetail(ref netinfo);
        5. 传递结构体数组(c 来填充) c 原型: struct uim_book_struct {    int uimindex;    char szname[15];    char szphone[21]; }; int readuimallbook(uim_book_struct lpuimbookitem[],int nmaxarraysize);
c#引用 [structlayout(layoutkind.sequential, charset = charset.ansi)]//可以指定编码类型 public struct uim_book_struct {    public int uimindex;    [marshalas(unmanagedtype.byvaltstr, sizeconst= 15)]    public string szname;    [marshalas(unmanagedtype.byvaltstr, sizeconst= 21)]    public string szphone; };

[dllimport( "cdmacard.dll",entrypoint="readuimallbook")] public    static    extern   int readuimallbook([out] uim_book_struct [] lpuimbookitem,int nmaxarraysize);

uim_book_struct[] p = new uim_book_struct[20]; int ret = readuimallbook(p,p.length);
6. 注意问题 类型不一致,会导致调用失败, (1) long 类型,在c 中是4字节的整数,在c#中是8字节的整数; (2) 字符串类型的设置不正确;

转载于:https://www.cnblogs.com/kun91101/p/3190891.html

总结

以上是尊龙游戏旗舰厅官网为你收集整理的c#调用c 编写的dll函数各种参数传递问题的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图