473,406 Members | 2,208 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,406 software developers and data experts.

C# string to C++ dll char*

I'm really stuck - can someone help me!

I've got a basic setup consisting of two things:
1. A C# web service
2. An unmanaged C++ DLL

The WS is intended to call some functions in the DLL. The DLL has some
legacy code which requires char* in its method parameters.

I also have a new file in the DLL which is basically an intermediary -
the WS can call the exported functions from this file, which then call
the legacy code. The problem is converting from the native C# strings in
the WS to the char*'s in the C++ code.

So, the WS calls method(string) in the DLL, and the DLL then needs to
call method(char*) in its C++ code, where string==char*.

At the moment I have the DLL exporting method(String*)'s, but of course
in C# I can't get the address of a string to pass to the method. If I
change the DLL methods to method(String) (ie. no pointer), the compiler
complains that they HAVE to be pointers. I can't use method(char*)
either because then I'm back to the original problem of C# not wanting
to call a method with a char* in it.

Help!

Nov 16 '05 #1
8 37840
For char*, you have to do this

byte [] str = new byte [255];

Pass str to the function

--
Shak
(Houston)
"johnsto" <an****@anywhere.com> wrote in message
news:cd**********@titan.btinternet.com...
I'm really stuck - can someone help me!

I've got a basic setup consisting of two things:
1. A C# web service
2. An unmanaged C++ DLL

The WS is intended to call some functions in the DLL. The DLL has some
legacy code which requires char* in its method parameters.

I also have a new file in the DLL which is basically an intermediary -
the WS can call the exported functions from this file, which then call
the legacy code. The problem is converting from the native C# strings in
the WS to the char*'s in the C++ code.

So, the WS calls method(string) in the DLL, and the DLL then needs to
call method(char*) in its C++ code, where string==char*.

At the moment I have the DLL exporting method(String*)'s, but of course
in C# I can't get the address of a string to pass to the method. If I
change the DLL methods to method(String) (ie. no pointer), the compiler
complains that they HAVE to be pointers. I can't use method(char*)
either because then I'm back to the original problem of C# not wanting
to call a method with a char* in it.

Help!

Nov 16 '05 #2
johnsto <an****@anywhere.com> wrote in
news:cd**********@titan.btinternet.com:
I'm really stuck - can someone help me!

I've got a basic setup consisting of two things:
1. A C# web service
2. An unmanaged C++ DLL

The WS is intended to call some functions in the DLL. The DLL
has some legacy code which requires char* in its method
parameters.

I also have a new file in the DLL which is basically an
intermediary - the WS can call the exported functions from this
file, which then call the legacy code. The problem is converting
from the native C# strings in the WS to the char*'s in the C++
code.

So, the WS calls method(string) in the DLL, and the DLL then
needs to call method(char*) in its C++ code, where
string==char*.

At the moment I have the DLL exporting method(String*)'s, but of
course in C# I can't get the address of a string to pass to the
method. If I change the DLL methods to method(String) (ie. no
pointer), the compiler complains that they HAVE to be pointers.
I can't use method(char*) either because then I'm back to the
original problem of C# not wanting to call a method with a char*
in it.


You can use the either the System.String or System.Text.StringBuilder
class. The underlying P/Invoke marshalling code in the .Net
framework will do all of the messy character pointer conversions for
you. If the string parameter is used as an output or input/output
buffer, then use StringBuilder. Otherwise if the string parameter is
input-only you can use the String class.

For example, the Win32 API function GetPrivateProfileString has both
input-only string parameters, and a string output parameter. Here's
how it can be accessed in C# using the String and StringBuilder
classes as parameter types:

/* Original C Method Signature:

DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);

*/

[DllImport("kernel32", EntryPoint="GetPrivateProfileString",
SetLastError = false, CharSet = CharSet.Auto)]
private static extern long GetPrivateProfileString(
string lpApplicationName, // in
string lpKeyName, // in
string lpDefault, // in
StringBuilder lpReturnedString, // out
int nSize, // in
string lpFileName); // in
public string GetIniValue(
string iniFileName,
string sectionName,
string keyName,
string defaultValue)
{
const int maxlen = 255;

StringBuilder sBuffer = new StringBuilder(maxlen);

// The P/Invoke marshaller takes care of converting the
// String and StringBuilder parameters to/from
// character pointers.

GetPrivateProfileString(sectionName, keyName, defaultValue,
sBuffer, maxlen, iniFileName);

return sBuffer.ToString();
}

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #3
Chris R. Timmons wrote:
You can use the either the System.String or System.Text.StringBuilder
class. The underlying P/Invoke marshalling code in the .Net
framework will do all of the messy character pointer conversions for
you. If the string parameter is used as an output or input/output
buffer, then use StringBuilder. Otherwise if the string parameter is
input-only you can use the String class.


It is input-only... so you're saying if I pass it a System::String, it
will convert to a char* automatically?

I mean, the very basic version of the problem is converting the
System::String to char*, and working out where to do the conversion. The
C++ requires char*, the C# WS needs String (it makes life complicated
and complains about char*), and the DLL intermediary is there really
just to forward method calls and handle any parameter conversion.

Part of the problem is actually convincing the C# bit to let me use
String* as a pointer - which I can then pass to the DLL... That's the
last hurdle that got me before I gave up for the weekend...

Argh!!!
Nov 16 '05 #4
Hi,

"johnsto" <an****@anywhere.com> wrote in message
news:cd**********@titan.btinternet.com...
I'm really stuck - can someone help me!

I've got a basic setup consisting of two things:
1. A C# web service
2. An unmanaged C++ DLL

The WS is intended to call some functions in the DLL. The DLL has some
legacy code which requires char* in its method parameters.

I also have a new file in the DLL which is basically an intermediary -
the WS can call the exported functions from this file, which then call
the legacy code. The problem is converting from the native C# strings in
the WS to the char*'s in the C++ code.

So, the WS calls method(string) in the DLL, and the DLL then needs to
call method(char*) in its C++ code, where string==char*.
The dll (intermediary) that WS suppose to call, is it a managed c dll or a
plain old c dll ? Because that's still not very clear here.
HTH,
greetings


At the moment I have the DLL exporting method(String*)'s, but of course
in C# I can't get the address of a string to pass to the method. If I
change the DLL methods to method(String) (ie. no pointer), the compiler
complains that they HAVE to be pointers. I can't use method(char*)
either because then I'm back to the original problem of C# not wanting
to call a method with a char* in it.

Help!

Nov 16 '05 #5
johnsto <an****@anywhere.com> wrote in
news:cd**********@titan.btinternet.com:
Chris R. Timmons wrote:
You can use the either the System.String or
System.Text.StringBuilder class. The underlying P/Invoke
marshalling code in the .Net framework will do all of the messy
character pointer conversions for you. If the string parameter
is used as an output or input/output buffer, then use
StringBuilder. Otherwise if the string parameter is input-only
you can use the String class.
It is input-only... so you're saying if I pass it a
System::String, it will convert to a char* automatically?


Yes.
I mean, the very basic version of the problem is converting the
System::String to char*, and working out where to do the
conversion. The C++ requires char*, the C# WS needs String (it
makes life complicated and complains about char*), and the DLL
intermediary is there really just to forward method calls and
handle any parameter conversion.

Part of the problem is actually convincing the C# bit to let me
use String* as a pointer - which I can then pass to the DLL...
That's the last hurdle that got me before I gave up for the
weekend...


Are you trying to use a "string *" in the C# code? Just try a plain
old System.String. If that doesn't work, try
System.Text.StringBuilder. I don't think there's any need to use
pointers (*) in the C# code.

To see why, look at the example code I posted. The C "LPSTR"
parameter type means "Long Pointer to STRing", which is a typedef for
"char *". And LPCSTR means "Long Pointer to a Const STRing", which
is a typedef for "const char *" (see WTypes.h for the
actual typedefs). The P/Invoker marshaller correctly translates from
System.String to LPCSTR, and from System.Text.StringBuilder to LPSTR.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #6
BMermuys wrote:
Hi,
"johnsto" <an****@anywhere.com> wrote in message
I've got a basic setup consisting of two things:
1. A C# web service
2. An unmanaged C++ DLL

The dll (intermediary) that WS suppose to call, is it a managed c dll or a
plain old c dll ? Because that's still not very clear here.


It's unmanaged - I think it was throwing up errors as a managed DLL (due
to the legacy stuff). I guess that makes it 'plain old c' although I'm
building it with VS2003.
Nov 16 '05 #7

"johnsto" <an****@anywhere.com> wrote in message
news:cd**********@sparta.btinternet.com...
BMermuys wrote:
Hi,
"johnsto" <an****@anywhere.com> wrote in message
I've got a basic setup consisting of two things:
1. A C# web service
2. An unmanaged C++ DLL

The dll (intermediary) that WS suppose to call, is it a managed c dll or a plain old c dll ? Because that's still not very clear here.


It's unmanaged - I think it was throwing up errors as a managed DLL (due
to the legacy stuff). I guess that makes it 'plain old c' although I'm
building it with VS2003.


ok, no problem, then you have to follow Chris' replies.

Greetings
Nov 16 '05 #8
Chris R. Timmons wrote:
To see why, look at the example code I posted. The C "LPSTR"
parameter type means "Long Pointer to STRing", which is a typedef for
"char *". And LPCSTR means "Long Pointer to a Const STRing", which
is a typedef for "const char *" (see WTypes.h for the
actual typedefs). The P/Invoker marshaller correctly translates from
System.String to LPCSTR, and from System.Text.StringBuilder to LPSTR.


Damn, .NET is so smart :)
Nov 16 '05 #9

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

Similar topics

6
by: Gaurav | last post by:
Hello, I am using visual c++ 6 and i am having problems with string to work. ******** Here is the program project.cpp********* #include <iostream.h> #include <string> #include "stdafx.h"
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
6
by: becte | last post by:
I am little bit confused Is this a legal way of removing a substring from a string? What about the second alternative using strcpy, is it ok even though the source and dest. strings overlap? ...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.