473,698 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is Marshal::String ToHGlobalAnsi argument not const?

I try to be const correct everywhere and found that I can't pass a const
String to this API. Is it going to change the value? Or is the signature
not the best it could be?

I want to write this:

// based on http://msdn.microsoft.com/en-us/magazine/bb985936.aspx

std::string MarshalString (const System::String^ s)
{
using namespace System::Runtime ::InteropServic es;
const char* chars =
(const char*)(Marshal: :StringToHGloba lAnsi
(s)).ToPointer( );
os = chars;
Marshal::FreeHG lobal(IntPtr((v oid*)chars));
return os;
}

Should I copy the argument and convert the copy?
Jun 27 '08 #1
2 2304
Kenneth Porter <sh************ *@sewingwitch.c omwrote in
news:Xn******** *************** ***@207.46.248. 16:
I try to be const correct everywhere and found that I can't pass a const
String to this API. Is it going to change the value? Or is the signature
not the best it could be?
I've discovered the answer to my question. The CLR threw const-correctness
on the scrap heap to be compatible with VB's lack of const, and that
decision infected C# and C++/CLI:

http://blogs.msdn.com/oldnewthing/ar...27/121049.aspx

So I guess I'll be copying my const Strings to non-const Strings before
passing them to system libraries.
Jun 27 '08 #2

"Kenneth Porter" <sh************ *@sewingwitch.c omwrote in message
news:Xn******** *************** ***@207.46.248. 16...
Kenneth Porter <sh************ *@sewingwitch.c omwrote in
news:Xn******** *************** ***@207.46.248. 16:
>I try to be const correct everywhere and found that I can't pass a const
String to this API. Is it going to change the value? Or is the signature
not the best it could be?

I've discovered the answer to my question. The CLR threw const-correctness
on the scrap heap to be compatible with VB's lack of const, and that
decision infected C# and C++/CLI:
Yup, and you can't do *anything* with a const managed type, because there
aren't any const member functions, so even reading properties isn't
possible. There's no syntax for defining const member functions in your own
types either, although it would have been easy for the C++ compiler to
implement this using attributes.
>
http://blogs.msdn.com/oldnewthing/ar...27/121049.aspx

So I guess I'll be copying my const Strings to non-const Strings before
passing them to system libraries.

Jun 27 '08 #3

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

Similar topics

1
7226
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is Marshaled as follows: static extern int VFGeneralize(byte features); In C# I have the following: // Allocate memory to store "Count" references to byte arrays
2
4720
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need to interact with many objects in that DLL and wrapper by reference. (So the classes inherit from MBRO.) While my app is running, I need to obtain info from objects in the 2nd app domain frequently/speedily and I need update the GUI; and I need to...
0
2692
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a mixed mode DLL unmanaged code is written in unmanaged C++ & resides in an unmanaged DL Product used is VS.NET 200 Please tell me what is the best practice for getting and setting data fro unmanaged to managed part when I have a class with...
13
2160
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the code? Thanks PS I added the +1 because as I understand the GetLogicalDriveStrings doc
20
14304
by: Frank Rizzo | last post by:
I have a class that handles events from another class. Unfortunately these events arrive on a different thread. How can I marshal the code execution to the thread on which the caller class was created. 'example public class MyClass { private ClassWithEvents cls = new ClassWithEvents(); public MyClass()
1
1311
by: Goran | last post by:
Hi all! I need to pass managed String from to C-style APIs. I see I can use Marshal::StringToXXX functions. Is this the best we have? I understand this will allocate a new string and create copy of my String. I want to pass it as constant (LPCWSTR). I don't need allocation and copying, just the pointer. Can I have it, pretty please? In normal C++, we usually have conversion operator or a function to get it from a string class...
4
4397
by: DaTurk | last post by:
Hi, I have a c# GUI that needs to untimately send its data down to unmanaged c++. I've decided to do this by having a ref struct in the CLI layer and have the c# populate this, and then pass it back to the CLI layer. The CLI layer will then populate the unmanged struct where the data needs to end up. Now, my question, I just realized that I'm going to have alot of strings. I need to have these things end up as char*, but they start
0
1793
by: Dilip | last post by:
I have cross-posted this question to 3 newsgroups since I am not sure exactly where it belongs (and some NG's have very less activity). Apologies in advance for this. I have set the followup to the dotnet.framework.interop NG but feel free to chage it as appopriate. I have a question regarding converting a Managed System.String^ to unmanaged const char*. I inherited a code that does it like this: String^ s = "00000";
2
2714
by: jonpb | last post by:
Hi, I cannot find the correct attributes to marshal the following unmanaged function signature: void WINAPI GeomVec2AngleD(const Vector2RecD& v1, const Vector2RecD& v2, double& angle) I thought this would work: public static extern void GeomVec2AngleD( Point2D v1, Point2D
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
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...
0
9014
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8855
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
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...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3037
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
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.