Unity C # calls C + + DLL problem collection

Source: Internet
Author: User

1. Calling convention

StdCall, cdecl, fastcall, etc.

This is used to specify the order in which parameters are passed and how the stack is purged when the function returns.

You can use the following

[AttributeUsage (AttributeTargets.Method)] public class DllImportAttribute:System.Attribute {public Dllimportattribu Te (string dllName) {...}//positional parameter is dllName public callingconvention callingconvention; Entry point calling convention public CharSet CharSet; The entry point uses a character that is connected to the public string entrypoint; Entry point name public bool exactspelling; Whether the spelling must be exactly the same as the entry point indicated, by default false public bool PreserveSig; 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 {...}} }
property specifies the calling convention, the character set, and so on.

2. Turn on x64 support

This is easy to change, vs Normal is Win32 release, debug, you can create a new x64 release, Debug.

3. Delivery of basic data types

in the intermodulation process, the most basic thing to pass is numeric values and characters, namely: Int,long,float,char and so on, but this type is not the type, C + + with the C # There are some data type lengths that are different, and the following table lists the similarities and differences of common data types:

C + +  

C #  

length

short  

short  

2bytes

int

int  

4bytes  

long (

int  

4bytes  

bool  

bool  

1byte  

char (Ascii code character "

byte  

1byte  

wchar_t ( Unicode character, which is the same type as C # in the Char compatible)

Char

2bytes  

float

float

4bytes

Double

Double

8Bytes

The easiest thing to confuse isLong,chartwo types, inC + +inLongand theint's All4bytes, all corresponding to theC #in theinttype, andC + +in theChartype is a byte that is used to represent aASCIIcode characters, inC #The ability to represent a byte in abytetype. With theC #inCharthe type corresponding to that should beC + +in thewchar_ttype, which corresponds to a2bytes ofUnicodecharacters.

kindly note that C + + char is not a C # char, and these two types are incompatible. and wchar_t under Windows 2byte, under Linux 4byte.
4. Passing arrays

----------------------------------------------------------

The C # declaration parameters are as follows:

Char[] CHS

The C + + declaration parameters are as follows:

Char Chs[5]

When the array is copied in the past, C + + modifies the array without changing the C # array

----------------------------------------------------------

The C # declaration parameters are as follows:

([In, Out][marshalas (UnmanagedType.LPArray, Sizeparamindex=1)] char[] CHS

The C + + declaration parameters are as follows:

wchar_t* CHS

This is equivalent to passing by reference, and C + + modifies the array to change the C # array

----------------------------------------------------------

The C # declaration parameters are as follows:

Ref Char CH (when invoked, ref char[0])

The C + + declaration parameters are as follows:

wchar_t* CHS

This is equivalent to passing by reference, and C + + modifies the array to change the C # array

------------------------------------------------------------

If you need to use pointers, you may need to turn on unsafe.

5. Open unsafe

Methods of using pointers (Unsafe) in C # in Unity3d

Recently, due to the use of data transfer in the U3D project (c + + DLL data passed to C # in u3d), pointers to C # are involved. Direct compilation will cause the following error, unsafe code requires the 'unsafe' command line option to be specified.

Here is the solution I summarized:

1. Remove the unsafe error from the Mono editor and assembly-csharp the right mouse button to find the options->build->general. Allow ' unsafe ' code to tick. This only removes mono error, but it still doesn't work.

2. First look at the following long

Custom Preprocessor directives

It is also possible to define your own preprocessor directives to control which code gets included when compiling. To does this you must add in the ' assets/' folder a text file with the extra directives. The name of the file depends on the language is using:

C# <project PATH>/ASSETS/SMCS.RSP
C #-Editor Scripts <project PATH>/ASSETS/GMCS.RSP
Unityscript <project PATH>/ASSETS/US.RSP
Boo <project PATH>/ASSETS/BOO.RSP

As an example, if you include the single line '-define:unity_debug ' in your SMCS.RSP file the define Unity_debug would exis T as a global define for C # scripts, except for Editor scripts.

Every time changes to the. rsp files a recompilation needs to is done for them to be effective. Updating or reimporting a single script (. js,. cs or. Boo) file.

The usage of the. rsp files are described in the "the" SMCs application, included in the Editor installation folder. You can get more information by running: "Smcs-help".

add smcs.rsp file under your assets directory with only one line and no space-unsafe. OK, fix it. Be sure to restart Unity3d, because this precompilation is run at the start of u3d. The project file name is not in Chinese.

The principle is that the editor smcs.exe add compile commands, you can also run under CMD under the Editor directory Smcs.exe add one by one, it will be very tired.



Reference:

[1] C + + Interactive http://www.cnblogs.com/warensoft/archive/2011/12/09/warenosoft3d.html

[2] C#_dllimport usage and path problems http://www.cnblogs.com/szytwo/archive/2011/12/11/2283780.html

[3] Unity3d in C # using pointers (Unsafe) method http://www.j2megame.com/html/xwzx/ty/3652.html

[4] Unity3d Tutorial: Calling the DLL method of C + + http://www.unitymanual.com/5291.html

[5] array marshalling http://www.kycis.com/blog/read.php?21

Unity C # calls C + + DLL problem collection

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.