zoukankan      html  css  js  c++  java
  • C# 调用C接口

    (1) C 返回 char* 中C# 处理方法

    ------CSHARP ::

    [DllImport(DLL, CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr luna_genGvoiceToken(ref int time, int type, string openId, string room, string client_ip, string member_id, string file);

    public static string genGvoiceToken(ref int time, int type, string openId, string room, string client_ip, string member_id, string file)
    {
      return Marshal.PtrToStringAnsi(luna_genGvoiceToken(ref time, type, openId, room, client_ip, member_id, file));
    }

    C#调用C的时候 不要随便传null

    ---------C++ ::

    LUNA_API const char* luna_genGvoiceToken(int* time , int type, const char* open_id, const char* room_name, const char* client_ip, const char* member_id, const char* file_key){
      int ret = gvoice::genGvoiceToken(*_tokenRet, (gvoice::GEN_TYPE)type, open_id, room_name, client_ip, member_id, file_key);
      if (ret) {
      return "";
      }
      *time = _tokenRet->timestamp;
      return _tokenRet->token.c_str();
    }

    C#中的引用可以对应的C函数中的指针

    从C 返回值给C#的时候 不要用memcpy();给C# 传入的ref赋值, 要出错, 指针无法共用。

    ========

    犯的错:

    1,memcpy 给C#传入的char *  赋值

    2,C#传NULL给C++ 的char *, 转string;

    3, .so 库文全局变量 是否有问题??

  • 相关阅读:
    23种设计模式目录总览
    Unity3d优化总结2
    Unity3d优化总结1
    Unity四元数和旋转
    浅谈Unity中的GC以及优化
    Unity自带寻路Navmesh
    High-level NavMesh Building Components
    Unity3D的四种坐标系
    安装SQLserver2008r2出现 试图执行未经授权的操作
    C#获取上传文件的扩展名
  • 原文地址:https://www.cnblogs.com/blackcatx/p/6248834.html
Copyright © 2011-2022 走看看