473,761 Members | 4,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SetFileNam e=txtboxFileNam e.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 11363
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.SetFileNam e=txtboxFileNam e.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 = PtrToStringChar s(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.SetFileNam e=txtboxFileNam e.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 = PtrToStringChar s(mngdStr);
For more interop suff search MSDN for "C++ Interop".

Willy.
"Lonewolf" <an*******@mozi lla.org> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.SetFileNam e=txtboxFileNam e.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 = PtrToStringChar s(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}"+pszFile Name;
which obviously can't work due to s being managed class.

I used ATLTRACE like
ATLTRACE(pszFil eName) 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*******@mozi lla.org> wrote in message
news:uE******** ******@TK2MSFTN GP14.phx.gbl...
Willy Denoyette [MVP] wrote:
pin_ptr<const wchar_t> wch = PtrToStringChar s(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::PtrToS tringUni((IntPt r)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
2224
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": http://www.finseth.com/~fin/craft/ , I've come across the "buffer gap" representation for the text data of the buffer. Very briefly, it keeps the unallocated memory of the
5
3192
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 overview... I have four sets of co-ordinates in each 'facetInfo', and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects that have y-values of zero (.v1y, .v2y and .v3y), and return a 'facetInfo' vector with the remaining...
6
4285
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 function called readline() because I would also like to do some processing on the line each time (ignoring comments and so on). I have:
4
7697
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 simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
3
14141
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 segmentation faults and core dumps. The problem occurs when the program calls scanf(). I don't know what is wrong with it. Here is my code: #include "stdio.h" #define SIZE 5
0
327
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, 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...
8
4058
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 seen people use StringBuilder for this, but is string good enough? The problem is that I am seeing what appears to be uninitialized data when I use StringBuilder, so I am wonder what is the correct solution thanks Zytan
1
9314
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 on the filesystem. This part is working perfectly. ((MimeBodyPart)p).saveFile(new File(p.getFileName())); However, I would like to pass this file as a byte array to a c++ library
7
4910
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 this with a lot of replacements and by the fact that some of the target strings are two words or more long, so I can't just break up the file at whitespace, commas, and periods. How's the best way to do this? I've thought about using strstr() to...
3
2757
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. Basically, I have problem passing a structure to a function. Here is the C++ part code: //****************************************
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.