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

L

Lonewolf

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.
 
L

Lonewolf

Lonewolf said:
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. :)
 
G

Guest

you can do this using

const wchar_t __pin* p = PtrToStringChars(myNetString);

kind regards,
Bruno.
 
W

Willy Denoyette [MVP]

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

Willy.


Lonewolf said:
Lonewolf said:
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. :)
 
L

Lonewolf

Willy said:
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?
 
W

Willy Denoyette [MVP]

Lonewolf said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top