473,396 Members | 2,011 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,396 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 11311
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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: 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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.