hi all,
i'm using this code to convert a String ^ in char *
String ^str = "string .net";
IntPtr p =
System::Runtime ::InteropServic es::Marshal::St ringToHGlobalAn si(str);
LPCSTR str2 = reinterpret_cas t<LPCSTR>(stati c_cast<void *>(p));
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(p);
I would like to make safe code, so i would like to use safe_cast
instead of reinterpret_cas t or static_cast.
I tryed, but i can't achieve making something to work.
Do you have an idea ?
Thanks in advance for your help
Nicolas H. 7 3244
Hi nicolas! i'm using this code to convert a String ^ in char *
I use the following struct and code:
struct StringConvA
{
char *szAnsi;
StringConvA(Sys tem::String* s)
:
szAnsi(static_c ast<char*>(Syst em::Runtime::In teropServices:: Marshal::String *ToHGlobalAnsi( s).ToPointer()) )
{}
~StringConvA()
{
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(IntPt r(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
struct StringConvW
{
wchar_t *szUnicode;
StringConvW(Sys tem::String* s)
:
szUnicode(stati c_cast<wchar_t* >(System::Runti me::InteropServ ices::Marshal:: *StringToHGloba lUni(s).ToPoint er()))
{}
~StringConvW()
{
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(IntPt r(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};
#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif
int _tmain()
{
String *s = S"abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
_tprintf(_T("%s "), (LPCTSTR) StringConvT(s)) ;
}
--
Greetings
Jochen
My blog about Win32 and .NET http://blog.kalmbachnet.de/
thanks for your answer Jochen
Your method looks like mine, using static_cast.
I would rather user safe_cast, but this should not be possible ...
Regards,
Nicolas
What would the safe_cast give you?
If you compile with /clr:safe, you can not use pointers at all. If you use
/clr:pure or /clr, and encapsulate the ugly cast in a separate class that
also provides cleanup of the native data, you should be fine.
Marcus
<ni************ *@motorola.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. . thanks for your answer Jochen
Your method looks like mine, using static_cast.
I would rather user safe_cast, but this should not be possible ...
Regards, Nicolas
Hello, Jochen!
The same can also be accomplished, using
pin_ptr class and PtrToStringChar s(...) inline func.
String^ managedString;
pin_ptr<const TCHAR> srvNameUnmanged ;
srvNameUnmanged = PtrToStringChar s(managedString );
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
>What would the safe_cast give you? If you compile with /clr:safe, you can not use pointers at all. If you use /clr:pure or /clr, and encapsulate the ugly cast in a separate class that also provides cleanup of the native data, you should be fine.
Marcus
Yes, you're right Marcus, that's the good question :)
No need to make it safe or to get error messages exceptions
Thanks for your help
Nicolas H.
Vadym Stetsyak schrieb: Hello, Jochen!
The same can also be accomplished, using pin_ptr class and PtrToStringChar s(...) inline func.
String^ managedString; pin_ptr<const TCHAR> srvNameUnmanged ; srvNameUnmanged = PtrToStringChar s(managedString );
Yes, at least for the W-Version... for the A-Version you need to use
such a construct...
--
Greetings
Jochen
My blog about Win32 and .NET http://blog.kalmbachnet.de/
JKM> Yes, at least for the W-Version... for the A-Version you need to use
JKM> such a construct...
Agree, this constuct is valid only for W, since String^ are always unicodes...
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Trep |
last post by:
Hi there!
I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.
This is necessary because I have some legcay C code that needs to process
a string taken from a textbox, then I need to re-display the string as the
textbox->Text. I easily found how to convert from system::string to char
but I can't figure out how to go the other way!!
|
by: IamZadok |
last post by:
Hi
I was wondering if anyone knew how to convert a string or an integer
into a Static Char.
Thx
|
by: MilanB |
last post by:
Hello
How to convert char to int?
Thanks
|
by: Yifan |
last post by:
Hi
Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks
Yifan
|
by: Brian Henry |
last post by:
first question... I have a flat file which unfortinuatly has columns
seperated by nulls instead of spaces (a higher up company created it this
way for us) is there anyway to do a readline with this and not have it
affected by the null? because it is right now causes truncated data at wierd
places... but as soon as i manually with a hex editor change char(00) to
char(20) in the files it reads prerfectly... which leads me to my 2nd...
| |
by: Steve Kershaw |
last post by:
I can convert from a string to a char array (char) by using
string.ToCharArray() function. Now how do I convert it back?
The string looks like this: "000457". I've converted it to a char
array: char = {0, 0, 0, 4, 5, 7}. Now I need to convert it back to a
string.
Thanks in advance for your help.
Steve
|
by: Zytan |
last post by:
I am surprised that a single character string is not auto-created from
a single char. It is hard to find information on converting a char
into a string, since most people ask how to convert char to string.
I have a function that accepts a string, and I wish to access this
function for each character in the string, individually. The only
solution I have found is:
char c = 'A';
string s = System.Text.Encoding.ASCII.GetString(c);
|
by: Man4ish |
last post by:
HI ,
I am trying to convert string into char array of characters.but facing problem.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string t="1,2,3,4,5,6";
|
by: Peter |
last post by:
Trying to convert string to byte array.
the following code returns byte array of {107, 62, 194, 139, 64}
how can I convert this string to a byte array of {107, 62, 139, 65}
System.Text.UTF8Encoding str = new System.Text.UTF8Encoding();
string s = new string((char)107, 1);
s += new string((char)62, 1);
|
by: rtillmore |
last post by:
Hello,
I did a quick google search and nothing that was returned is quite
what I am looking for. I have a 200 character hexadecimal string that
I need to convert into a 100 character string.
This is what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |