欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#调用c++方法介绍,window api

程序员文章站 2024-02-21 09:06:52
例子1如下:复制代码 代码如下://声明部分 [dllimport("um_web_client.dll")]  private static extern i...

例子1如下:

复制代码 代码如下:

//声明部分

[dllimport("um_web_client.dll")]
  private static extern int checkusername(string username);

//

public static int authusername(string username)
  {
   if(!isconnection())
   {
    startwebclient(udbclientip,udbclientport,udbclienttimeout);
   }
   return checkusername(username);
  }


但是如果c++方法中用了指针或者结构体等则引用就要注意,相应就要用到引用数组或者指针,例子2:

声明部分

复制代码 代码如下:

[structlayout(layoutkind.sequential, charset=charset.ansi )]
  public class tagplayerinfoout
  {
   public int id;
   public string playername;
   public int country;
   public int officerrace;
   public int level;
   public int militaryrank;
   public int money;
   public int resourcecount;
   public int citycount;
   public int generalcount;
   public int credit;
  }

  [ structlayout( layoutkind.sequential )]
  public struct tagplayerinfooutex
  {
   public int id;
   public intptr playername;
   public int country;
   public int officerrace;
   public int level;
   public int militaryrank;
   public int money;
   public int resourcecount;
   public int citycount;
   public int generalcount;
   public int credit;
  }

 

[dllimport("um_web_client.dll")]
  public static extern int getplayerlistcs(int maxrows, out int rows,  out intptr playerlist);

  [dllimport("um_web_client.dll")]
  public static extern int getplayerlistcs(int maxrows, out int rows, tagplayerinfooutex** playerlist);


方法
复制代码 代码如下:

public static datatable usingmarshal()
  {
   if(!isconnection())
   {
    startwebclient(udbclientip,udbclientport,udbclienttimeout);
   }
   datatable dt = new datatable();

   dt.columns.add("id");
   dt.columns.add("playername");
   dt.columns.add("country");
   dt.columns.add("officerrace");
   dt.columns.add("level");
   dt.columns.add("militaryrank");
   dt.columns.add("money");
   dt.columns.add("resourcecount");
   dt.columns.add("citycount");
   dt.columns.add("generalcount");
   dt.columns.add("credit");

   int size = 0;
   intptr outarray;
   getplayerlistcs(20, out size, out outarray);
   tagplayerinfoout[] manarray = new tagplayerinfoout[size];
   intptr current = outarray;
   //console.writeline("记录数{0}",size);

   for( int i = 0; i < size; i++ )
   {
    manarray[ i ] = new tagplayerinfoout();
    marshal.ptrtostructure(current, manarray[i]);
    marshal.destroystructure(current, typeof(tagplayerinfoout) );
    current = (intptr)((long)current + marshal.sizeof(manarray[i]));

    dt.rows.add(new object[] {manarray[i].id,manarray[i].playername,manarray[i].country,manarray[i].officerrace,manarray[i].level,manarray[i].militaryrank,manarray[i].money,manarray[i].resourcecount,manarray[i].citycount,manarray[i].generalcount,manarray[i].credit});

    dt.acceptchanges();
   //console.writeline( "element {0}: {1} {2}", i, manarray[i].id, manarray[i].playername);
  }
   marshal.freecotaskmem(outarray);
   return dt;
  }

  public static unsafe datatable usingunsafe()
  {
   int size;
   if(!isconnection())
   {
    startwebclient(udbclientip,udbclientport,udbclienttimeout);
   }
   datatable dt = new datatable();

   dt.columns.add("id");
   dt.columns.add("playername");
   dt.columns.add("country");
   dt.columns.add("officerrace");
   dt.columns.add("level");
   dt.columns.add("militaryrank");
   dt.columns.add("money");
   dt.columns.add("resourcecount");
   dt.columns.add("citycount");
   dt.columns.add("generalcount");
   dt.columns.add("credit");
   tagplayerinfooutex* presult;
   getplayerlistcs(20,  out size, &presult );
   tagplayerinfooutex* pcurrent = presult;
   //console.writeline("记录数{0}",size);

   for( int i = 0; i < size; i++, pcurrent++ )
   {
    //console.writeline( "element {0}: {1} {2}", i, pcurrent->id ,marshal.ptrtostringansi(pcurrent->playername));
    //marshal.freecotaskmem(pcurrent->playername );
    dt.rows.add(new object[] {pcurrent->id,pcurrent->playername,pcurrent->country,pcurrent->officerrace,pcurrent->level,pcurrent->militaryrank,pcurrent->money,pcurrent->resourcecount,pcurrent->citycount,pcurrent->generalcount,pcurrent->credit});

    dt.acceptchanges();
   }
   marshal.freecotaskmem((intptr)presult );
   return dt;
  }