472,971 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

passing C# string to native WHCAR buffer via C++/CLI

Hi,
I'm not sure if this has been asked before so please pardon me if this
is a repeated question. Basically I have some performance critical
directshow codes which is implemented in native, unmanaged C++. I have
written a managed wrapper for it using C++/CLI. problem comes when I try
to pass a C# string over to the DLL via C++/CLO. Basically I have a
textbox in the C# GUI, and I declared my C++/CLI property as follows.

C++/CLI property
~~~~~~~~~~~~~~
public ref class CMR
{
property String^ SetFileName
{
void set(String^ value)
{
Debug::Write("\n FileName passed in is "+ value);
pNativeClass->(value->ToCharArray());
}
};
};

C# code
~~~~~~~~

mref is reference to C++/CLI assembly

CMR mref;
mref.SetFileName=txtboxFileName.Text;

whenever I tried to do that, the program would give a null reference
exception. I never saw the debug string. So I guess it failed when
trying to read the String value. Now, the question is, both C# and
C++/CLI are part of the .NET framework, why is it that the native string
class used by both are not compatible?? Is there some .NET string class
which is supported by all .NET lang? the C++/CLI assembly has to work
with all .NET lang, so the property for the file name has to be able to
process the value passed in without knowing what lang is being used.
Coudl someone please enlighten me ?

in the native class, I also need to convert from String^ to the normal
WCHAR buffer, what is the way to do it? those examples I found are all
related to MC++, I don't know what is the correct C++/CLI way of doing
it. Basically the native cdoe copies the file name into a unmanaged
array declared as

WCHAR szFilename[MAX_PATH];

I tried ToCharArray but that is converting to a wchar_t array, not a
WCHAR*, and the compiler complained. could some kind souls please
enlughten me on this as well?

I could pass a C# string to a WCHAR buffer, that's not an issue, but it
involves the ugly Marshalling code, I want to make C# development as
painless as possible by handling all the conversion from managed data
type to native C/C++ data type within C++/CLI. It amazed me that desite
the CLI claim in C++/CLI, the String class is not compatible with C#'s
string.
Jan 4 '06 #1
5 11266
Lonewolf wrote:
Hi,
I'm not sure if this has been asked before so please pardon me if this
is a repeated question. Basically I have some performance critical
directshow codes which is implemented in native, unmanaged C++. I have
written a managed wrapper for it using C++/CLI. problem comes when I try
to pass a C# string over to the DLL via C++/CLO. Basically I have a
textbox in the C# GUI, and I declared my C++/CLI property as follows.

C++/CLI property
~~~~~~~~~~~~~~
public ref class CMR
{
property String^ SetFileName
{
void set(String^ value)
{
Debug::Write("\n FileName passed in is "+ value);
pNativeClass->(value->ToCharArray());
}
};
};

C# code
~~~~~~~~

mref is reference to C++/CLI assembly

CMR mref;
mref.SetFileName=txtboxFileName.Text;

whenever I tried to do that, the program would give a null reference
exception. I never saw the debug string. So I guess it failed when
trying to read the String value. Now, the question is, both C# and
C++/CLI are part of the .NET framework, why is it that the native string
class used by both are not compatible?? Is there some .NET string class
which is supported by all .NET lang? the C++/CLI assembly has to work
with all .NET lang, so the property for the file name has to be able to
process the value passed in without knowing what lang is being used.
Coudl someone please enlighten me ?

in the native class, I also need to convert from String^ to the normal
WCHAR buffer, what is the way to do it? those examples I found are all
related to MC++, I don't know what is the correct C++/CLI way of doing
it. Basically the native cdoe copies the file name into a unmanaged
array declared as

WCHAR szFilename[MAX_PATH];

I tried ToCharArray but that is converting to a wchar_t array, not a
WCHAR*, and the compiler complained. could some kind souls please
enlughten me on this as well?

I could pass a C# string to a WCHAR buffer, that's not an issue, but it
involves the ugly Marshalling code, I want to make C# development as
painless as possible by handling all the conversion from managed data
type to native C/C++ data type within C++/CLI. It amazed me that desite
the CLI claim in C++/CLI, the String class is not compatible with C#'s
string.

I stand corrected. My unreserved apologies for C# string not being
compatible with C++/CLI String class. my C# class did not instantiate
the C++ class within the assembly, thus the null reference issue. I'm
deeply sosry. A major oversight on my part.

As for the conversion from C++/CLI's String^ to WCHAR array, I still
don't know how to do it. Could someone please enlighten me on that ?
thank you very much. :)
Jan 4 '06 #2
you can do this using

const wchar_t __pin* p = PtrToStringChars(myNetString);

kind regards,
Bruno.

"Lonewolf" wrote:
Hi,
I'm not sure if this has been asked before so please pardon me if this
is a repeated question. Basically I have some performance critical
directshow codes which is implemented in native, unmanaged C++. I have
written a managed wrapper for it using C++/CLI. problem comes when I try
to pass a C# string over to the DLL via C++/CLO. Basically I have a
textbox in the C# GUI, and I declared my C++/CLI property as follows.

C++/CLI property
~~~~~~~~~~~~~~
public ref class CMR
{
property String^ SetFileName
{
void set(String^ value)
{
Debug::Write("\n FileName passed in is "+ value);
pNativeClass->(value->ToCharArray());
}
};
};

C# code
~~~~~~~~

mref is reference to C++/CLI assembly

CMR mref;
mref.SetFileName=txtboxFileName.Text;

whenever I tried to do that, the program would give a null reference
exception. I never saw the debug string. So I guess it failed when
trying to read the String value. Now, the question is, both C# and
C++/CLI are part of the .NET framework, why is it that the native string
class used by both are not compatible?? Is there some .NET string class
which is supported by all .NET lang? the C++/CLI assembly has to work
with all .NET lang, so the property for the file name has to be able to
process the value passed in without knowing what lang is being used.
Coudl someone please enlighten me ?

in the native class, I also need to convert from String^ to the normal
WCHAR buffer, what is the way to do it? those examples I found are all
related to MC++, I don't know what is the correct C++/CLI way of doing
it. Basically the native cdoe copies the file name into a unmanaged
array declared as

WCHAR szFilename[MAX_PATH];

I tried ToCharArray but that is converting to a wchar_t array, not a
WCHAR*, and the compiler complained. could some kind souls please
enlughten me on this as well?

I could pass a C# string to a WCHAR buffer, that's not an issue, but it
involves the ugly Marshalling code, I want to make C# development as
painless as possible by handling all the conversion from managed data
type to native C/C++ data type within C++/CLI. It amazed me that desite
the CLI claim in C++/CLI, the String class is not compatible with C#'s
string.

Jan 4 '06 #3
pin_ptr<const wchar_t> wch = PtrToStringChars(mngdStr);
For more interop suff search MSDN for "C++ Interop".

Willy.
"Lonewolf" <an*******@mozilla.org> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Lonewolf wrote:
Hi,
I'm not sure if this has been asked before so please pardon me if
this
is a repeated question. Basically I have some performance critical
directshow codes which is implemented in native, unmanaged C++. I have
written a managed wrapper for it using C++/CLI. problem comes when I try
to pass a C# string over to the DLL via C++/CLO. Basically I have a
textbox in the C# GUI, and I declared my C++/CLI property as follows.

C++/CLI property
~~~~~~~~~~~~~~
public ref class CMR
{
property String^ SetFileName
{
void set(String^ value)
{
Debug::Write("\n FileName passed in is "+ value);
pNativeClass->(value->ToCharArray());
}
};
};

C# code
~~~~~~~~

mref is reference to C++/CLI assembly

CMR mref;
mref.SetFileName=txtboxFileName.Text;

whenever I tried to do that, the program would give a null reference
exception. I never saw the debug string. So I guess it failed when
trying to read the String value. Now, the question is, both C# and
C++/CLI are part of the .NET framework, why is it that the native string
class used by both are not compatible?? Is there some .NET string class
which is supported by all .NET lang? the C++/CLI assembly has to work
with all .NET lang, so the property for the file name has to be able to
process the value passed in without knowing what lang is being used.
Coudl someone please enlighten me ?

in the native class, I also need to convert from String^ to the normal
WCHAR buffer, what is the way to do it? those examples I found are all
related to MC++, I don't know what is the correct C++/CLI way of doing
it. Basically the native cdoe copies the file name into a unmanaged
array declared as

WCHAR szFilename[MAX_PATH];

I tried ToCharArray but that is converting to a wchar_t array, not a
WCHAR*, and the compiler complained. could some kind souls please
enlughten me on this as well?

I could pass a C# string to a WCHAR buffer, that's not an issue, but it
involves the ugly Marshalling code, I want to make C# development as
painless as possible by handling all the conversion from managed data
type to native C/C++ data type within C++/CLI. It amazed me that desite
the CLI claim in C++/CLI, the String class is not compatible with C#'s
string.

I stand corrected. My unreserved apologies for C# string not being
compatible with C++/CLI String class. my C# class did not instantiate the
C++ class within the assembly, thus the null reference issue. I'm deeply
sosry. A major oversight on my part.

As for the conversion from C++/CLI's String^ to WCHAR array, I still don't
know how to do it. Could someone please enlighten me on that ? thank you
very much. :)

Jan 4 '06 #4
Willy Denoyette [MVP] wrote:
pin_ptr<const wchar_t> wch = PtrToStringChars(mngdStr);
For more interop suff search MSDN for "C++ Interop".

Willy.

thanx for the guidance.. it worked. Well, just a small question, how do
I do something like Debug::Write in unmanaged? I miss the days of MFC's
TRACE macro, I tried ATLTRACE but it keeps giving error, in the end, I
give up. Basically I want to do something like

TRACE("\n file name=%s", pszFileName);

where pszFilename is declared as WHCAR pszFilename[MAX_PATH];

I tried using String like,

String^ s;
s="{0}"+pszFileName;
which obviously can't work due to s being managed class.

I used ATLTRACE like
ATLTRACE(pszFileName) which gives a debug assertion adn i can't see a
thing.

could you enlighten me on this? or anyone who knows?
Jan 4 '06 #5

"Lonewolf" <an*******@mozilla.org> wrote in message
news:uE**************@TK2MSFTNGP14.phx.gbl...
Willy Denoyette [MVP] wrote:
pin_ptr<const wchar_t> wch = PtrToStringChars(mngdStr);
For more interop suff search MSDN for "C++ Interop".

Willy.

thanx for the guidance.. it worked. Well, just a small question, how do I
do something like Debug::Write in unmanaged? I miss the days of MFC's


I guess you mean from managed code, here is how...

wchar_t *unm = L"Test";
String ^s = String::Format("{0}", Marshal::PtrToStringUni((IntPtr)unm));
Debug::Write(s);
But you should definitely read the MSDN docs about unmanaged/managed interop
and managed debugging before posting.

Willy.
Jan 4 '06 #6

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

Similar topics

6
by: manders2k | last post by:
Hi all -- I'm contemplating the idea of writing a simple emacs-like editor in python (for fun and the experience of doing so). In reading through Craig Finseth's "The Craft of Text Editing": ...
5
by: {AGUT2} {H}-IWIK | last post by:
Hi, I'm trying to pass my vector to a function external to my main(), but my compiler is complaining. FYI, the struct for the facetInfo works perfectly, it's just the vector passing. A quick...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
3
by: linguae | last post by:
Hello. In my C program, I have an array of character pointers. I'm trying to input character strings to each index of the character pointer array using scanf(), but when I run the program, I get...
0
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
8
by: Zytan | last post by:
When using DllImport to import win32 functions that have strings, I know you can use the native c# string type. But, what about a function that expects a string buffer, and writes to it? I've...
1
by: | last post by:
Hi all, I am writing a sendmail milter application in Java. The incoming mails will usually have image file as attachments. My application is currently able to extract the ImageFile and save it...
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
3
by: =?Utf-8?B?TWluZ3lp?= | last post by:
Hi, I have the following question regarding communicating with a OCX control using C#. Idon't have access to the ocx code, so my c# code is actually mimic the behavior of C++ counterparts....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.