Marshal wchar_t** from C++ to C# as an out parameter? - c#

I have this function in a dll in C and I cannot change it:
extern "C" SIBIO_MULTILANGUAGE_API_C DWORD getLabel(const char* const i_formName,
const char* const i_fieldName,
wchar_t** i_output);
I know that this call inside allocates the memory for the wchar_t* using the function CoTaskMemAlloc.
In C# I wrapped this function in this way:
[DllImport("sibio_multilanguage_c.dll", EntryPoint = "getLabel", CallingConvention = CallingConvention.Cdecl)]
private static extern UInt32 _getLabel([In] string i_formName, [In] string i_fieldName,
[MarshalAs(UnmanagedType.LPWStr)] out string i_output);
static public string getLabel(string i_formName, string i_fieldName)
{
string str = null;
UInt32 err = _getLabel(i_formName, i_fieldName, out str);
if (0 != err)
{
throw new System.IO.FileNotFoundException();
}
return str;
}
I'm able to read correctly the content of the wchar_t* but reading in this way I don't free the memory allocated in the C function.
How can I read the wchar_t* and also be able to free it? Any help is greatly appreciated!

Thanks to #Dai and #IanAbbot comments I've come up to a solution that works perfectly:
[DllImport("sibio_multilanguage_c.dll", EntryPoint = "getLabel", CallingConvention = CallingConvention.Cdecl)]
private static extern UInt32 _getLabel([In] string i_formName, [In] string i_fieldName,
out IntPtr i_output);
static public string getLabel(string i_formName, string i_fieldName)
{
IntPtr i_result;
string str = null;
UInt32 err = _getLabel(i_formName, i_fieldName, out i_result);
if (0 != err)
{
throw new System.IO.FileNotFoundException();
}
str = Marshal.PtrToStringAuto(i_result);
Marshal.FreeCoTaskMem(i_result);
return str;
}

Related

How to call C ++ method in C # using Invoke

Am I doing it right? I have two projects in parallel, the first is code that was made in C ++ and the second project (Console made in AspNetCore v3.1) is the attempt to call the method that is in C ++ code.
I need to call the C ++ method "Decrypt" in the C # project. How do I do that?
C++ code
#include <stdlib.h>
#include <string.h>
#include<windows.h>
#include "bascript.hpp"
extern "C"
int FAR PASCAL _export
Decript( const LPSTR name, const LPSTR passwordWithCript,
LPSTR passwordWithoutCript, unsigned int sizeSpaceRetorn ) {
LPSTR result = lpDecript( name, passwordWithCript);
if ( sizeSpaceRetorn < strlen(result) )
return 0;
strcpy( passwordWithoutCript, result );
delete result;
return 1;
}
C#
class Program
{
[DllImport(#"C:\MS\VS\TesteDLLCentura\TesteDLLCentura\bin\Debug\netcoreapp3.1\Sises.DLL", CharSet = CharSet.Auto, EntryPoint = "Decript")]
private static extern string Decript(string name, string passwordWithCript, string passwordWithoutCript, uint sizeSpaceRetorn);
static void Main(string[] args)
{
string retorno = Decript("<user>", "<cript_password>", "", 0);
Console.WriteLine(retorno);
Console.ReadLine();
}
}
You can return a pointer from native world (C/C++, etc.) as long as you use a .NET compatible memory allocator. On Windows, that would be the COM Allocator.
So here are 3 ways to return a string: Ansi, Unicode and BSTR (unicode). Note: you should avoid using Ansi on Windows.
C++ side:
extern "C" __declspec(dllexport) void* DecryptA(const char* name, const char* password)
{
char str[] = "hello ansi world";
int size = (lstrlenA(str) + 1) * sizeof(char); // terminating zero
// use .NET compatible allocator
void* buffer = CoTaskMemAlloc(size);
CopyMemory(buffer, str, size);
return buffer;
}
extern "C" __declspec(dllexport) void* DecryptW(const wchar_t* name, const wchar_t* password)
{
wchar_t str[] = L"hello unicode world";
int size = (lstrlenW(str) + 1) * sizeof(wchar_t); // terminating zero
// use .NET compatible allocator
void* buffer = CoTaskMemAlloc(size);
CopyMemory(buffer, str, size);
return buffer;
}
extern "C" __declspec(dllexport) BSTR DecryptBSTR(const wchar_t* name, const wchar_t* password)
{
wchar_t str[] = L"hello BSTR world";
// use .NET compatible allocator and COM coolness
return SysAllocString(str);
}
C# side:
[DllImport("mydll", CharSet = CharSet.Ansi)]
private static extern string DecryptA(string name, string password);
[DllImport("mydll", CharSet = CharSet.Unicode)]
private static extern string DecryptW(string name, string password);
[DllImport("mydll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string DecryptBSTR(string name, string password);
...
static void Main()
{
Console.WriteLine(DecryptA("name", "password"));
Console.WriteLine(DecryptW("name", "password"));
Console.WriteLine(DecryptBSTR("name", "password"));
}
Your C++ function does not return a string or equivalent. It returns an int result (success or failure), and the actual result goes in the passwordWithoutCript buffer.
So you need to create the buffer and pass it in.
Because you are using LPSTR on the C++ side, you need CharSet.Ansi:
[DllImport(#"C:\MS\VS\TesteDLLCentura\TesteDLLCentura\bin\Debug\netcoreapp3.1\Sises.DLL", CharSet = CharSet.Ansi, EntryPoint = "Decript")]
private static extern int Decript(string name, string passwordWithCript, StringBuilder passwordWithoutCript, uint sizeSpaceRetorn);
static void Main(string[] args)
{
var sb = new StringBuilder(1000); // or whatever size
if(Decript("<user>", "<cript_password>", sb, sb.Length) == 1)
Console.WriteLine(retorno);
Console.ReadLine();
}

Why does passing a string from C# to a C++ dll get NULL?

I'm trying to connect a C# executable to a C++ dll. One of the methods of the dll receives a const char* and an int* (the first one specifying an input value, and the second one, an address to return a value):
extern "C" __declspec(dllexport)
int setVal(long handle, const char* ptrVal, int* ptrRet);
The first thing this function does is to check whether ptrVal is null, and returns -1 if so.
On the other hand, the C# code invokes the dll as follows:
[DllImport(dllName,
EntryPoint = "setVal",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi)]
public static extern int setVal(long handle,
[MarshalAs(UnmanagedType.LPStr)] string str,
ref int ptrRes);
In the main function, I have
long handle = 0;
int result = 0;
int res = 0;
string str = "Hello World!";
result = setVal(handle, str, ref res);
When calling this function, I always receive a null pointer at the C side, which makes result equal to -1. I have tried different approaches when declaring the wrapper function, without success:
public static extern int setVal(long handle,
[MarshalAs(UnmanagedType.LPStr)] [In] string str,
[Out] int ptrRes);
public static unsafe extern int setVal(long handle,
[MarshalAs(UnmanagedType.LPStr)] string str,
ref int ptrRes);
public static extern int setVal(long handle,
StringBuilder sb,
ref int ptrRes); // also the unsafe version
public static extern int setVal(long handle,
byte[] value,
ref int ptrRes); // also the unsafe version
I'm using Visual Studio 2017, and .NET framework 4.6.1.
Why am I always receiving NULL as the second argument (const char*) of the dll function?
I did a bit of a google so this would be untested code but I can see it's something you haven't tried
declare external like so
[DllImport(dllName, EntryPoint = "setVal", ExactSpelling = true, CallingConvention, CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
public static extern int setVal(int handle, StringBuilder sb, ref int ptrRes);
and use it like so
int handle = 0;
int result = 0;
int res = 0;
StringBuilder sb = new StringBuilder("Hello World");
result = setVal(handle, sb, ref res);
On Windows, the long handle in C/C++ is int handle in C# both at 32 and 64 bits. You can check it by doing a sizeof(long) in C/C++. Windows is LLP64.

c++ to csharp string passing with dllimport , lost accents

I am trying to send a message from c++ to csharp but some of my accents are lost in the way ( not all of them?? ) ps: writing from italian
Here is what I do :
c++:
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
extern "C" {
DLL_API void __cdecl getResults(char* entry, wchar_t* result);
}
[...]
void getResults(char* entry,wchar_t* result)
{
std::string str(entry);
std::string Stringresult= "héà" ;
std::wstring wsTmp(Stringresult.begin(), Stringresult.end());
const wchar_t* constChar = wsTmp.c_str();
swprintf(result, Stringresult.length(), constChar);
c# :
[DllImport("libface.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void getResults([MarshalAs(UnmanagedType.LPStr)] string entry, StringBuilder res);
static void Main()
{
StringBuilder result = new StringBuilder(2000);
string entry = Console.ReadLine();
getResults( entry,result);
Console.WriteLine(result);
Solved the problem thanks to this link (that state that the problem is much more complicated that some persons may think .... ) :
http://blog.kutulu.org/2012/04/marshaling-utf-8-harder-than-it-ought.html
c++ code:
extern "C" {
DLL_API char* __cdecl getResults(char* entry);
}
char* getResults(char* entry)
{
std::string Stringresult= "hàé";
char *cstr = new char[Stringresult.length() + 1];
strcpy(cstr, Stringresult.c_str());
return cstr;
}
c# code:
[DllImport("libface.dll" , EntryPoint = "getResults")]
private static extern IntPtr getResults([MarshalAs(UnmanagedType.LPStr)] string entry);
static void Main()
{
var data = new List<byte>();
var ptr = getResults("p");
var off = 0;
while (true)
{
var ch = Marshal.ReadByte(ptr, off++);
if (ch == 0)
{
break;
}
data.Add(ch);
}
string sptr = Encoding.UTF8.GetString(data.ToArray());
Console.WriteLine(sptr);

Passing an array of strings from C++ to C#

I've already written this piece of code which works fine:
C++ code
extern "C"
{
const MYLIBRARY_EXPORT char* giefStrPlx(char* addon)
{
return addon;
}
}
C# code
[DllImport("ClassLibrary1")]
private static extern IntPtr giefStrPlx(string x);
void Start()
{
IntPtr stringPtr = giefStrPlx("Huntsman");
string huntsman = Marshal.PtrToStringAnsi(echoedStringPtr);
}
After this huntsman contains "Huntsman".
My problem is the step of doing something similar for an array of strings. I wrote the following function
extern "C"
{
const MYLIBRARY_EXPORT bool fillStrArray(char** lizt, int* length)
{
char* one = "one";
char* two = "two";
char* three = "three";
lizt[0] = one;
lizt[1] = two;
lizt[2] = three;
*length = 3;
}
}
I then tried to write the following piece of code in C#
[DllImport("ClassLibrary1")]
private static extern bool fillStrArray(ref IntPtr array, ref int length);
void Start()
{
IntPtr charArray = IntPtr.Zero;
int charArraySize = 0;
fillStrArray(ref charArray, ref charArraySize);
IntPtr[] results = new IntPtr[charArraySize];
Marshal.Copy(charArray, results, 0, charArraySize);
foreach (IntPtr ptr in results)
{
string str = Marshal.PtrToStringAnsi(ptr);
}
}
Which does not work. So now I'm a bit lost on how to accomplish this.
Here are the two helper functions I have from CLR to std::string and from std::string to string CLR
std::string CLROperations::ClrStringToStdString(String^ str)
{
if (String::IsNullOrEmpty(str))
return "";
std::string outStr;
IntPtr ansiStr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
outStr = (const char*)ansiStr.ToPointer();
System::Runtime::InteropServices::Marshal::FreeHGlobal(ansiStr);
return outStr;
}
String ^ CLROperations::StdStringToClr(std::string str)
{
return gcnew String(str.c_str());
}
for using a List of strings you will need to use List<String^>^ mind the capital String. for a list of std::string use std::vector<std::string>

How to call NetUserModalsGet() from C#.NET?

EDIT: followup at NetUserModalsGet() returns strings incorrectly for C#.NET
I'm struggling with the DLL declarations for this function:
NET_API_STATUS NetUserModalsGet(
__in LPCWSTR servername,
__in DWORD level,
__out LPBYTE *bufptr
);
(Reference: http://msdn.microsoft.com/en-us/library/aa370656%28VS.85%29.aspx)
I tried this:
private string BArrayToString(byte[] myArray)
{
string retVal = "";
if (myArray == null)
retVal = "Null";
else
{
foreach (byte myByte in myArray)
{
retVal += myByte.ToString("X2");
}
}
return retVal;
}
...
[DllImport("netapi32.dll")]
public static extern int NetUserModalsGet(
string servername,
int level,
out byte[] bufptr
);
[DllImport("netapi32.dll")]
public static extern int NetApiBufferFree(
byte[] bufptr
);
...
int retVal;
byte[] myBuf;
retVal = NetUserModalsGet("\\\\" + tbHost.Text, 0, out myBuf);
myResults.Text += String.Format("retVal={0}\nBuffer={1}\n", retVal, BArrayToString(myBuf));
retVal = NetApiBufferFree(myBuf);
I get a return value of 1231 (Network Location cannot be reached) no matter if I use an IP address or a NetBIOS name of a machine that's undoubtedly online, or even my own. On edit: this happens even if I don't put a "\\" in front of the hostname.
I'm doing things wrong, I know, and let's not even get started on how to declare that blasted return buffer (which can have a number of different lengths, ewww).
From pinvoke.net (they also have some sample code on how to use it):
[DllImport("netapi32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
static extern uint NetUserModalsGet(
string server,
int level,
out IntPtr BufPtr);
It would be better to use System.Text.Encoding.ASCII.GetBytes(somestring_param) and System.Text.Encoding.ASCII.GetString(byte_array) instead of your own routine BArrayToString.
Hope this helps.

Categories

Resources