Call unmanaged dll fromC#

  • Thread starter Thread starter supermamie
  • Start date Start date
S

supermamie

I need to call a function in a C++ dll from a C# program.
I can pass a string for an input parameter, but how do I get the
returned string?
The original C++ function : TransformString(char*)

sb = new StringBuilder("hello");
MyDll.TransformString(sb); /*supposed to transform "hello" into
"bye"*/
Console.WriteLine("{0}", sb.Tostring()); // always "hello" ...

Perhaps StringBuilders are not the solution ... :idea:
 
You probably want to pass in a char** (pointer to a string), and make the
call a 'ref string' instead. The CLR copies the string before it passes it
in otherwise.

--
John Wood
EMail: first name, dot, second name at priorganize.com


supermamie said:
I need to call a function in a C++ dll from a C# program.
I can pass a string for an input parameter, but how do I get the
returned string?
The original C++ function : TransformString(char*)

sb = new StringBuilder("hello");
MyDll.TransformString(sb); /*supposed to transform "hello" into
"bye"*/
Console.WriteLine("{0}", sb.Tostring()); // always "hello" ...

Perhaps StringBuilders are not the solution ... :idea:
 
supermamie,

The problem here is that you are passing ToString, which returns a
string, which is immutable. Declare the function to take a StringBuilder,
and not a String, and then pass the StringBuilder itself, and it should
work.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

supermamie said:
I need to call a function in a C++ dll from a C# program.
I can pass a string for an input parameter, but how do I get the
returned string?
The original C++ function : TransformString(char*)

sb = new StringBuilder("hello");
MyDll.TransformString(sb); /*supposed to transform "hello" into
"bye"*/
Console.WriteLine("{0}", sb.Tostring()); // always "hello" ...

Perhaps StringBuilders are not the solution ... :idea:
 
I'm trying something else with Byte Array
because ASCII char are 1 byte long.
See my C# function

using System.Runtime.InteropServices; //DLL Import
using System;
using System.Text;

namespace ConsoleApplication3
{

class ParameterControl
{
[DllImport("Dll_01",EntryPoint =
"StrFromCpp",ExactSpelling = true,CharSet =
CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
public static extern void StrFromCpp(
[MarshalAs(UnmanagedType.LPArray)]
byte[] param2);
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
byte[] b = new byte[256];
ParameterControl.StrFromCpp(b);
Console.WriteLine(System.Text.Encoding.ASCII.GetString(b));

Console.WriteLine("end");
Console.ReadLine();
}
}
}

and StrFromCpp function in my dll:

[code:1:ceccda0fb3]
void __stdcall StrFromCpp(char* param1)
{
printf("passed value: %s\n",param1);
param1="byebye";
printf("new value : %s\n",param1);
printf("\nfin de la fonction StrFromCpp");
}[/code:1:ceccda0fb3]
'b' has not been changed bye StrFromCpp function, it had to return the
string "byebye" ... ? :?:
 
Hi,
<inline>

supermamie said:
I'm trying something else with Byte Array
because ASCII char are 1 byte long.
See my C# function

using System.Runtime.InteropServices; //DLL Import
using System;
using System.Text;

namespace ConsoleApplication3
{

class ParameterControl
{
[DllImport("Dll_01",EntryPoint =
"StrFromCpp",ExactSpelling = true,CharSet =
CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
public static extern void StrFromCpp(
[MarshalAs(UnmanagedType.LPArray)]
byte[] param2);
}

Just use a StringBuilder as the parameter without ref.
class Class1
{
[STAThread]
static void Main(string[] args)
{
byte[] b = new byte[256];

// Allocate space for the stringbuilder
StringBuilder sb = new StringBuilder(256);
ParameterControl.StrFromCpp( sb, 256 );
Console.WriteLine(System.Text.Encoding.ASCII.GetString(b));
Console.WriteLine("end");
Console.ReadLine();
}
}
}

and StrFromCpp function in my dll:

[code:1:ceccda0fb3]
void __stdcall StrFromCpp(char* param1, int maxLen )
{
printf("passed value: %s\n",param1);
param1="byebye";

This means you're trying to change a pointer, but you need a
pointer-to-pointer for that to work.
Instead, use strcpy to copy the string to the location where char* points at
:

strcpy ( param1, "byebye" );
printf("new value : %s\n",param1);
printf("\nfin de la fonction StrFromCpp");
}[/code:1:ceccda0fb3]
'b' has not been changed bye StrFromCpp function, it had to return the
string "byebye" ... ? :?:

HTH,
greetings
 
Back
Top