To summarize C # calls to C + + DLL files

Source: Internet
Author: User

In actual project work, C # is often used to invoke DLL files written in C + + or C.

DLLs support General function declarations and definition declarations for classes, but are generally simplified in the way that function declarations are used. This is not primarily written DLL writing.

First create a DLL project in VS

Add a new CPP file to test the code as follows:

struct Student//defines a struct that contains basic types, string types, and arrays, which basically satisfies many situations.
{
int no;
Char name[10];
int score[4];
};

extern "C" __declspec (dllexport) void __stdcall returnstring (char* s)//This is the method that returns a string, and the following can be used as a comparison. That is, the return string can either use a pointer to a string as a parameter or as a function return value.
{
strcpy (S, "123456");
}

extern "C" __declspec (dllexport) char* __stdcall returnstring ()
//{
return "AAAAAA";
//}

extern "C" __declspec (dllexport) int __stdcall ReturnInt ()//This is a basic function that returns the value of a base type
{
return 123;
}

extern "C" __declspec (dllexport) void __stdcall returnintarray (int *a)//This is an array that returns a primitive type
{
for (int i=0;i<10;i++)
{
A[i] = i+1;
}
}


extern "C" __declspec (dllexport) void __stdcall returnstruct (Student & Stu)//This is the return of a struct type note: Why do we generally return with parameters instead of Use the function body to return, because sometimes you will need to return multiple parameter values, we are accustomed to use this way
{
stu.no = 100;
strcpy (Stu.name, "CSL");
for (int j=0;j<4;j++)
{
STU.SCORE[J] = j*2+14;
}
}

extern "C" __declspec (dllexport) void __stdcall Returnstructarray (student* stu)//This is the return of a struct pointer type, that is, the return value is a struct-body array, You can return more than one struct object and the following way is the same
//{
Stu = (student*) malloc (sizeof (Student) *10);
for (int i=0;i<10;i++)
//{
stu[i].no = i+1;
strcpy (Stu[i].name, "CSL");
//}
//
//}

extern "C" __declspec (dllexport) void __stdcall Returnstructarray (Student stu[])
{
Stu = (student*) malloc (sizeof (Student) *10);
for (int i=0;i<10;i++)
{
stu[i].no = i+1;
strcpy (Stu[i].name, "CSL");
for (int j=0;j<4;j++)
{
STU[I].SCORE[J] = j*2+14;
}
}

}

------------a call in C #---------------

public struct Student
{
public int No;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 10)]
public string name;
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 4)]//Specify array size
Public int32[] score;
}; struct declaration

Calling a function's declaration

[DllImport ("E:\\c#\\testlibdll\\debug\\testlibdll.dll")]
public static extern void Returnstring (StringBuilder s); String type directly using StringBuilder

[DllImport ("E:\\c#\\testlibdll\\debug\\testlibdll.dll")]
public static extern int ReturnInt ();//Return basic type

[DllImport ("E:\\c#\\testlibdll\\debug\\testlibdll.dll")]
public static extern void Returnintarray (int[] a); Returns an array because the array is a reference type, so declare it directly

[DllImport ("E:\\c#\\testlibdll\\debug\\testlibdll.dll")]
public static extern void Returnstruct (out Student Stu); Returns struct because the C + + definition uses & references, so we need to use out or ref in C #


[DllImport ("E:\\c#\\testlibdll\\debug\\testlibdll.dll")]
public static extern void Returnstructarray (IntPtr ptr); For an array that returns a struct, it is online to use MarshalAs to manipulate the memory pointer by itself using the student[] array as a parameter, without results, and using pointers can indeed

Specific operation

static void Main (string[] args)
{
string S;
s = returnstring ();
Console.WriteLine (s);


StringBuilder s = new StringBuilder ();
Returnstring (s);
Console.WriteLine (S.tostring ());

int nInt = ReturnInt ();
Console.WriteLine (NINT);

int[] arrayList = new INT[10];
Returnintarray (arrayList);

Student stu = new Student ();
Returnstruct (out Stu);

student[] Stus = new STUDENT[10];

int size = marshal.sizeof (typeof (Student)) * 10;
IntPtr ptr = marshal.allochglobal (size);
Returnstructarray (PTR);
for (int i = 0; i <; i++)
{
IntPtr temp = (INTPTR) (UInt32) (ptr + i * SIZE/10);
Stus[i] = (Student) marshal.ptrtostructure (temp, typeof (Student));
}
Marshal.freehglobal (PTR); Freeing memory

Console.readkey ();
}

To summarize C # calls to C + + DLL files

Related Article

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.