Autor
Nachricht
Jochen Kalmbach
Moderator
Benutzerprofil
Anmeldungsdatum: 11.11.2005
Beiträge: 11307
Jochen Kalmbach Moderator
13:24:45 07.09.2006 Titel:
Konvertierung System::String => char* oder wchar_t*
Zitieren
Es gibt mehrere Möglichkeiten dies zu tun, der "einfachst" ist aber eine Hilfsklasse (struct) zu verwenden, der sich um das Freigeben von Speicher automatisch kümmert:
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <windows.h>
#include <tchar.h>
using namespace System;
struct StringConvA
{
char *szAnsi;
StringConvA(System::String ^s)
: szAnsi(static_cast <char *>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{}
~StringConvA()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String^ s)
: szUnicode(static_cast <wchar_t *>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))
{}
~StringConvW()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};
#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <windows.h>
#include <tchar.h>
using namespace System;
struct StringConvA
{
char *szAnsi;
StringConvA(System::String ^s)
: szAnsi(static_cast <char *>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{}
~StringConvA()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String^ s)
: szUnicode(static_cast <wchar_t *>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))
{}
~StringConvW()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};
#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <windows.h>
#include <tchar.h>
using namespace System;
struct StringConvA
{
char *szAnsi;
StringConvA(System::String ^s)
: szAnsi(static_cast <char *>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{}
~StringConvA()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String^ s)
: szUnicode(static_cast <wchar_t *>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))
{}
~StringConvW()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};
#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif
Die Verwendung ist dann z.b: wie folgt:C/C++ Code: 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
#include <stdio.h>
int _tmain()
{
String ^s = "abc ";
printf("%s ", (LPCSTR) StringConvA(s));
wprintf(L"%s ", (LPCWSTR) StringConvW(s));
_tprintf(_T("%s "), (LPCTSTR) StringConvT(s));
return 0;
}
C/C++ Code: 1 2 3 4 5 6 7 8 9
#include <stdio.h>
int _tmain()
{
String ^s = "abc ";
printf("%s ", (LPCSTR) StringConvA(s));
wprintf(L"%s ", (LPCWSTR) StringConvW(s));
_tprintf(_T("%s "), (LPCTSTR) StringConvT(s));
return 0;
}
C/C++ Code: 1 2 3 4 5 6 7 8 9
#include <stdio.h>
int _tmain()
{
String ^s = "abc ";
printf("%s ", (LPCSTR) StringConvA(s));
wprintf(L"%s ", (LPCWSTR) StringConvW(s));
_tprintf(_T("%s "), (LPCTSTR) StringConvT(s));
return 0;
}
Oder für std::string/wstring:C/C++ Code: #include <string>
int _tmain()
{
String ^s = "abc ";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
}
C/C++ Code: #include <string>
int _tmain()
{
String ^s = "abc ";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
}
C/C++ Code: #include <string>
int _tmain()
{
String ^s = "abc ";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
}
Will man nur nach "const wchar_t" umwandeln, dann kann man auch folgendes verwenden. Dabei wird dann keine kopie des Strings erstellt, sondern direkt auf den Native-Speicher zugegriffen!C/C++ Code: 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
#include <stdio.h>
#include <vcclr.h>
int _tmain()
{
String ^s = "abc ";
pin_ptr<const wchar_t > szStr = PtrToStringChars(s);
wprintf(L"%s ", szStr);
}
C/C++ Code: 1 2 3 4 5 6 7 8
#include <stdio.h>
#include <vcclr.h>
int _tmain()
{
String ^s = "abc ";
pin_ptr<const wchar_t > szStr = PtrToStringChars(s);
wprintf(L"%s ", szStr);
}
C/C++ Code: 1 2 3 4 5 6 7 8
#include <stdio.h>
#include <vcclr.h>
int _tmain()
{
String ^s = "abc ";
pin_ptr<const wchar_t > szStr = PtrToStringChars(s);
wprintf(L"%s ", szStr);
}
Wer MFC verwendet kann es auch ganz einfach machen:C/C++ Code: CString str(managedString);
C/C++ Code: CString str(managedString);
C/C++ Code: CString str(managedString);
_________________ Greetings
Jochen
(Microsoft MVP VC++ ) My blog about Win32 and .NET: http://blog.kalmbach-software.de/ (deutsch)
Zuletzt bearbeitet von Jochen Kalmbach am 13:27:26 07.09.2006, insgesamt 2-mal bearbeitet
Werbeunterbrechung
Jochen Kalmbach
Moderator
Benutzerprofil
Anmeldungsdatum: 11.11.2005
Beiträge: 11307
Jochen Kalmbach Moderator
14:11:02 14.02.2008 Titel:
Zitieren
Ab VC2008 gibt es von VC++ Team eine "marshal_as" Library, die im wesentlichen das Konvertieren von Strings vornimmt.
Siehe dazu auch:
marshal_as library in VC2008
Die Verwendung ist simpel:
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <string>
#include <tchar.h>
#include <msclr/marshal.h>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
int main(array<String ^> ^args)
{
// TO String^
String ^s1 = marshal_as<System::String^>("Hello world ");
Console::WriteLine(s1);
String ^s2 = marshal_as<System::String^>(L"Hello world ");
Console::WriteLine(s2);
String ^s3 = marshal_as<System::String^>(_T("Hello world "));
Console::WriteLine(s3);
String ^s = "Hello world ";
// FROM String^
std::string us1 = marshal_as<std::string>(s);
printf(us1.c_str());
std::wstring us2 = marshal_as<std::wstring>(s);
wprintf(us2.c_str());
msclr::interop::marshal_context c; // Context kann wiederverwendet werden!
const char *us3 = c.marshal_as<const char *>(s);
printf(us3);
const wchar_t *us4 = c.marshal_as<const wchar_t *>(s);
wprintf(us4);
LPCTSTR us5 = c.marshal_as<LPCTSTR>(s);
_tprintf(us5);
}
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <string>
#include <tchar.h>
#include <msclr/marshal.h>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
int main(array<String ^> ^args)
{
// TO String^
String ^s1 = marshal_as<System::String^>("Hello world ");
Console::WriteLine(s1);
String ^s2 = marshal_as<System::String^>(L"Hello world ");
Console::WriteLine(s2);
String ^s3 = marshal_as<System::String^>(_T("Hello world "));
Console::WriteLine(s3);
String ^s = "Hello world ";
// FROM String^
std::string us1 = marshal_as<std::string>(s);
printf(us1.c_str());
std::wstring us2 = marshal_as<std::wstring>(s);
wprintf(us2.c_str());
msclr::interop::marshal_context c; // Context kann wiederverwendet werden!
const char *us3 = c.marshal_as<const char *>(s);
printf(us3);
const wchar_t *us4 = c.marshal_as<const wchar_t *>(s);
wprintf(us4);
LPCTSTR us5 = c.marshal_as<LPCTSTR>(s);
_tprintf(us5);
}
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <string>
#include <tchar.h>
#include <msclr/marshal.h>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
int main(array<String ^> ^args)
{
// TO String^
String ^s1 = marshal_as<System::String^>("Hello world ");
Console::WriteLine(s1);
String ^s2 = marshal_as<System::String^>(L"Hello world ");
Console::WriteLine(s2);
String ^s3 = marshal_as<System::String^>(_T("Hello world "));
Console::WriteLine(s3);
String ^s = "Hello world ";
// FROM String^
std::string us1 = marshal_as<std::string>(s);
printf(us1.c_str());
std::wstring us2 = marshal_as<std::wstring>(s);
wprintf(us2.c_str());
msclr::interop::marshal_context c; // Context kann wiederverwendet werden!
const char *us3 = c.marshal_as<const char *>(s);
printf(us3);
const wchar_t *us4 = c.marshal_as<const wchar_t *>(s);
wprintf(us4);
LPCTSTR us5 = c.marshal_as<LPCTSTR>(s);
_tprintf(us5);
}
_________________ Greetings
Jochen
(Microsoft MVP VC++ ) My blog about Win32 and .NET: http://blog.kalmbach-software.de/ (deutsch)
Zuletzt bearbeitet von Jochen Kalmbach am 19:20:16 19.01.2010, insgesamt 5-mal bearbeitet
Nächstes Thema anzeigen
Vorheriges Thema anzeigen
Sie können keine Beiträge in dieses Forum schreiben. Sie können auf Beiträge in diesem Forum nicht antworten. Sie können Ihre Beiträge in diesem Forum nicht bearbeiten. Sie können Ihre Beiträge in diesem Forum nicht löschen. Sie können an Umfragen in diesem Forum nicht mitmachen.