468,110 Members | 1,867 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

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 10786
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by manders2k | last post: by
4 posts views Thread by Scott Lyons | last post: by
7 posts views Thread by DarthBob88 | last post: by
3 posts views Thread by =?Utf-8?B?TWluZ3lp?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.