To write dll, first, open VS and create a new C++ console program, and select dll and empty documents after the next step. Then add a class and add a method. The method header fixed format is extern"C"__declspec(dllexport) and add the method afterwards. For example, the following code:
C++dll code:
extern"C"__declspec(dllexport) char* ShowImages(BYTE img[],int w,int h){;}
C# calls dll basically have a fixed format, as shown in the following style, there is an added unsafe here. This is because pointers are used [unsafe is not needed for pointers], C# must be added with pointers
unsafe, and the generation of unsafe codes allowed to be chewed in the project properties.
Another point is that C# calls DLLs with namespace
using ;
The code style is as follows:
C# call code:
[DllImport("dll name.dll", CharSet = , CallingConvention = )]
unsafe public static extern byte* ShowImages(byte[] src, int w, int h);
The reason for writing dll as described above and the call failure is mostly because the C++ C# data type does not correspond to.
Finally, one thing to mention is that the method with a pointer return value must be global in C++, and private ones in the method body will make an error. The reason is that the method body is released, and although it is returned, it has no effect.
I have encountered this problem before. When a pointer address is released in C++, and then called in C#, the data is messy. I was always very depressed at the time. Later, I finally found that the reason is that the pointer return will still be released, so the data obtained is messy. The solution is of course to set the pointer variable to a global variable.