This is my first question here. I'm also new to programming, so I'll do my best to make it as clear as possible.
There's this DLL that was originally written in Delphi. One of its functions requires an int (Integer in Delphi) as its argument and in turn returns a string (PWideChar in Delphi).
The DLL documentation does not bring many details, so I'm trying to explore different possibilities in order to get the proper results. Also, there's no issue with the DLL, as I'm able to successfully call this function from Python by using restype. E.g.:
DLLName.FunctionName.restype = c_wchar_p
When I call the callback function below from C# (5.0) though, I'm able to see something whenever I use any number type, being that either sbyte, int or long, for example. However, I get an error when I try to use string, which I'd think would be the appropriate data type to be placed as its return data type:
[DllImport(dll_path, CallingConvention = CallingConvention.StdCall)]
public static extern sbyte GetNameByID(int nID);
This was my first trial:
public static string GetName()
{
string result = GetNameByID(1)
return result;
}
But, as I mentioned, I'd only see something being returned when I use numerical data types instead of string. I also (naively) gave it a trial with .ToString() or casting the result with (string)...
SOLUTION AFTER INVESTIGATION:
For those that may be facing similar issues, this is how the issue was solved, after some tentatives to figure it out:
The function call returns a pointer, so:
[DllImport(dll_path, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetNameByID(int nID);
And then, when hadling its return, marshalling the pointer to Unicode string:
public static string GetName()
{
string result = Marshal.PtrToStringUni(GetNameByID(1)));
return result;
}