Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

import C++ into C#

//you will be using the using 
using System.Runtime.InteropServices;

//Before we begin you must understand you cannot truely import CPP.
//Only static functions and structs for data can be imported. This
//may mean writeing a quick CCP class to static name space wrapper
//on the CPP side and then importing. This may take passing a 
//pointer to the class its self and passing of structs for the data.

//Note that the .dll should be in the same folder as the C# directory
//or registered with your OS

//Importing a static Function to a method. You may want to wrap the functions
//with a more C# frendly method to get the types to play well together in C#
//For indstance I have seen methods in CPP where two calls are required
//one for a count and one for the array list its self. This could be
//method wrapped to call both times for a single call using a special contianer in C#.
//but the below is the import type table
//Note that the .dll should be in the same folder as the C# directory
//or registered with your OS
[DllImport("TheSource.dll", EntryPoint = "TheCPPFunction")]
static extern int/*Return type must match your CPP return*/ 
YourInitImportSignature(/*any params that match your CPP params*/);
//Type Equiviants rough (you can sometimes interchange them or usage may change them)
// CPP                C#
// char               byte or char
// short              short
// unsigned short     ushort
// int                int or IntPtr(as a underef pointer) or EnumType
// unsigned int       uint
// long int           long
// unsigned long int  ulong
// double             double
// short              short
//Special type modifiers
//CPP                   C#
//*(pointer)type        Type[] or IntPtr or StructLayout(LayoutKind.Sequential) marked struct
//*(pointer)char        char[] and/or string
//&(reference)type      ref type and/or out
//Note Enums can be in int places but must be cast with a wrapping class 
//after ecxternal import as C++ will treat them as ints in use.

//Importing a struct
[StructLayout(LayoutKind.Sequential)]
struct YourLayoutOfCPPStrunct
{
  //Add your properties as is the same in the C++ class in the squent order
  public string FistTextLocation;
  public int SecondInt;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 36)]
  public byte[] LoctionArrays;
}
// you can mostly follow the above equivalants, but arrays must now be give 
// a size according to the CPP's values.
//Some of this will be detective work and a guessing game to see what imports propertly.
Comment

PREVIOUS NEXT
Code Example
Csharp :: wpf label text in center 
Csharp :: unity to string 
Csharp :: Character Controller unity isGrounded is false 
Csharp :: how to detect collision in unity 
Csharp :: unity get distance between two objects 
Csharp :: unity how to set an objects postion x,y,z 
Csharp :: c# write all bytes to a file 
Csharp :: restclient basic auth c# 
Csharp :: get unix time in seconds C# 
Csharp :: c# linq extension methods left join 
Csharp :: c# play sound 
Csharp :: c# check if string is empty 
Csharp :: round corners of textbox wpf 
Csharp :: c# wait seconds 
Csharp :: unity c# instantiate prefab 
Csharp :: unity pause animator 
Csharp :: unity deactive code from code 
Csharp :: replace text c# file 
Csharp :: if animation ends 
Csharp :: how to destroy in unity 
Csharp :: get all files in all subdirectories c# 
Csharp :: unity movetowards 2d 
Csharp :: c# run as administrator 
Csharp :: c# count specific element in list 
Csharp :: unity know when mouse on ui 
Csharp :: c# array last element 
Csharp :: unity google play games plugin spam 
Csharp :: winforms messagebox with button 
Csharp :: index in foreach c# 
Csharp :: how to compare 2 date time in asp.net core 3.1 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =