I'm trying to open a char* that's being exported from a Dll in my C# project.
Since the codes are too much, I'll write a short code that is similar to the code that I'm working on.
C++:
__declspec(dllexport) typedef struct Kid {
char* _name;
int _age;
int _grade;
} Kid;
Kid get_default_kid()
{
char name[] = { "Quinn" };
return Kid{
name, 2,7
};
}
extern "C" __declspec(dllexport) Kid get_default_kid();
C#:
public struct Kid
{
public string _name;
public int _age;
public int _grade;
}
private const string Dll2 = @"C:\Users\Me\source\repos\Tom\x64\Release\JerryDll.dll";
[DllImport(Dll2, CallingConvention = CallingConvention.Cdecl)]
private static extern Kid get_default_kid();
public static void Main(string[] args){
Kid defaultKid = get_default_kid();
Console.WriteLine(defaultKid._name);
Console.WriteLine(defaultKid._age);
Console.WriteLine(defaultKid._grade);
}
This code writes some random characters like '♠' in the console instead of the name that is being exported from the dll.
What I've tried:
Trying to import the char*
as IntPtr
then read it using:
Marshal.PtrToStringAnsi()/BSTR/Auto/Uni
and Marshal.ReadIntPtr
before trying to read it using the line above.
I tried converting the string to UTF-8 in C# since.
searching a lot in google.