Summary of the method of calling dll (char*, and parameters with function pointers) in unity

dllImport is an attribute class under the System.Runtime.InteropServices namespace, so to use DllImport in ASP.NET , you must first " using System.Runtime.InteropServices ;". Its function is to provide information necessary to call functions exported from unmanaged DLLs. The DllImport attribute applies to methods that require at least the name of the dll that contains the entry point.
 
 
DllImport Property Definition
as follows:
  
namespace System.Runtime.InteropServices
   {
    [AttributeUsage(AttributeTargets.Method)]
    public class DllImportAttribute: System.Attribute
    {
public DllImportAttribute(string dllName){...}   //定位参数为dllName
public CallingConvention CallingConvention;      //入口点调用约定
public CharSet CharSet;                          //入口点采用的字符接
public string EntryPoint;                        //入口点名称
public bool ExactSpelling;                       //是否必须与指示的入口点拼写完全一致,默认false
public bool PreserveSig;                         //方法的签名是被保留还是被转换
public bool SetLastError;                        //FindLastError方法的返回值保存在这里
public string Value {get {...}}                            
    } 
  }

illustrate:
1. DllImport can only be placed on the method declaration.
2. DllImport has a single positioning parameter: the dllName parameter that specifies the name of the dll containing the imported method.
3. DllImport has five named parameters:
   a. The CallingConvention parameter indicates the calling convention of the entry point. If CallingConvention is not specified, the default value CallingConvention.Winapi is used.
   b. The CharSet parameter specifies the character set used at the entry point. If CharSet is not specified, the default value CharSet.Auto is used.
   c. The EntryPoint parameter gives the name of the entry point in the dll. If EntryPoint is not specified, the name of the method itself is used.
   d. The ExactSpelling parameter indicates whether the EntryPoint must exactly match the spelling of the indicated entry point. If ExactSpelling is not specified, the default value of false is used.
   e. The PreserveSig parameter indicates whether the signature of the method is preserved or converted. When the signature is transformed, it is transformed into a signature with an HRESULT return value and an additional output parameter called retval for that return value. If PreserveSig is not specified, the default value of true is used.
   f、SetLastError参数指示方法是否保留Win32“上一错误”。如果未指定SetLastError,则使用默认值false。
4、它是一次性属性类。
5、用DllImport属性修饰的方法必须具有extern修饰符。
 
 
首先要在Unity中导入相对应得DLL
C++ .h文件
extern "C" __declspec(dllimport) void Creat();

C#文件
 [DllImport("WebrtcClient")]
    public static extern void Creat();
带char* 参数的方法
 
 
c++.h 文件
extern "C" __declspec(dllimport) void Connect(const char* szConnetId	);

C# 文件
[DllImport("WebrtcClient")]
    public static extern voidConnect([MarshalAs(UnmanagedType.LPStr)]string connetId);
 MarshalAs属性指示如何在托管代码和非托管代码之间封送数据。
如果不加[MarshalAs(UnmanagedType.LPStr)] 在Unity中可能会出现崩溃
 
 
C++中使用指针是家常便饭了,也非常的好用,但是在C#中就强调托管的概念了,指针就不用想了。下面来看一下带有函数指针的参数的传递
C++.h文件
extern "C" __declspec(dllimport) void CallbackFunc(
	void(*SendMessageCallback)( const char* szMessage)		// IN:回调函数指针
	);
C#文件
[DllImport("WebrtcClient", EntryPoint = "CallbackFunc")]
    public static extern void CallbackFunc(SendMessageFuncCallback sendMsgFunc);//(void(*SendMessageCallback)(const char*));

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void SendMessageFuncCallback([MarshalAs(UnmanagedType.LPStr)]string message);



 

Guess you like

Origin blog.csdn.net/zjw1349547081/article/details/70229098