Unity in the call DLL (char*, as well as parameters with function pointers) method to summarize __ function

Source: Internet
Author: User
DllImport is an attribute class under the System.Runtime.InteropServices namespace, so the DllImport in asp.net must precede the "using System.Runtime.InteropServices; ". The function is to provide the information necessary to invoke a function exported from an unmanaged DLL. The DllImport attribute is applied to the method, requiring at least the name of the DLL that contains the entry point.
As follows:
Namespace System.Runtime.InteropServices
   {
  [AttributeUsage (AttributeTargets.Method)] public
  class DllImportAttribute:System.Attribute
  {public
DllImportAttribute (string dllName) {...}   The positional parameters are dllname public
callingconvention callingconvention;      The entry point Call Convention public
CharSet CharSet;                          The character of the entry point is followed by the public
string entrypoint;                        The entry point name is public
bool exactspelling;                       Whether it must be spelled exactly the same as the entry point indicated, default false public
bool PreserveSig;                         Whether the signature of the method is reserved or converted to public
bool SetLastError;                        The return value of the Findlasterror method is saved here public
String Value {get {...}}}                            
  } 
  }

Description
1, DllImport can only be placed on the method declaration.
2, DllImport has a single positioning parameter: Specifies the DllName parameter that contains the DLL name of the method being imported.
3, DllImport has five named parameters:
   A, callingconvention parameters indicate the calling convention for the entry point. If CallingConvention is not specified, the default value Callingconvention.winapi is used.
   The B, charset parameter specifies the character set to use at the entry point. If CharSet is not specified, the default value CharSet.Auto is used.
   The C, 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.
   The D, exactspelling parameter indicates whether the entrypoint must exactly match the spelling of the indicated entry point. If ExactSpelling is not specified, the default value false is used.
   The E, preservesig parameter indicates whether the signature of the method is preserved or converted. When the signature is converted, it is converted to a signature with an HRESULT return value and a return value of an additional output parameter named RetVal. If PreserveSig is not specified, the default value of True is used.
   The F, setlasterror parameter indicates whether the method retains the Win32 "previous error". If SetLastError is not specified, the default value false is used.
4, it is a one-time attribute class.
5. A method decorated with the DllImport property must have an extern modifier.
First, you should import the relative due DLL in unity.
C + +. h files
extern "C" __declspec (dllimport) void creat ();

C # files
[DllImport ("webrtcclient")]
    public static extern void creat ();
A method with char* parameters
C++.h file
extern "C" __declspec (dllimport) void Connect (const char* szconnetid	);

C # files
[DllImport ("webrtcclient")]
    public static extern Voidconnect ([MarshalAs (UNMANAGEDTYPE.LPSTR)]string Connetid);
The MarshalAs property indicates how to marshal data between managed and unmanaged code.
If you do not add [MarshalAs (UNMANAGEDTYPE.LPSTR)] There may be a breakdown in unity
The use of pointers in C + + is commonplace, but also very useful, but in C # to emphasize the concept of hosting, the pointer does not have to think about. Let's take a look at the passing of parameters with function pointers
C++.h file
extern "C" __declspec (dllimport) void Callbackfunc (
	void (*sendmessagecallback) (const char* szmessage)		// In: callback function pointer
	);
C # files
[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);



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.