473,387 Members | 1,742 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,387 software developers and data experts.

Why is Marshal::StringToHGlobalAnsi 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::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi
(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
return os;
}

Should I copy the argument and convert the copy?
Jun 27 '08 #1
2 2284
Kenneth Porter <sh*************@sewingwitch.comwrote 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.comwrote in message
news:Xn**************************@207.46.248.16...
Kenneth Porter <sh*************@sewingwitch.comwrote 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
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...
2
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...
0
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...
13
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...
20
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...
1
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...
4
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...
0
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.