473,395 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Call unmanaged dll fromC#

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:
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
4 2096
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" <ae*******@siticom-dot-com.no-spam.invalid> wrote in message
news:40********@Usenet.com...
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:
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"supermamie" <ae*******@siticom-dot-com.no-spam.invalid> wrote in message
news:40********@Usenet.com...
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:
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #3
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.GetSt ring(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" ... ? :?:
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #4
Hi,
<inline>

"supermamie" <ae*******@siticom-dot-com.no-spam.invalid> wrote in message
news:40**********@Usenet.com...
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.GetSt ring(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


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: supermamie | last post by:
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...
4
by: Gnanaprakash Rathinam | last post by:
Hi Expert, Is there a way to obtain assembly name in an unmanaged call? During Interop call between managed to unmanaged, I would like to know in unmanaged code about the caller of assembly file...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
4
by: Andreas Ntalakas | last post by:
hello is it possible to call functions implemented in a C# .dll from unmanaged C++ code Thank you
14
by: Paul Welter | last post by:
I'm having trouble getting the api PathYetAnotherMakeUniqueName to work. Here is what I have so far. It is not working. What am i missing? const int MAX_PATH = 260; static extern int...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
0
by: Ken Durden | last post by:
A little more info. I have a unmanaged C++ DLL, compiled without /CLR. Inside a Managed C++ DLL, I have the following class: MarshalWrapper.h ---- #ifdef _MANAGED __nogc #endif
28
by: Peter Olcott | last post by:
I want to double check my understanding about how the .NET framework works. From what I understand every call to the .NET framework is ultimately translated into one of more API calls, is this...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.