Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2006

Tip: Looking for answers? Try searching our database.

Marshal::StringToXXX question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Goran - 31 Aug 2006 14:39 GMT
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 (string::c_str() or CString::operator LPCTSTR or
_bstr_t::operator LPCTSTR etc). Is there a similar thing in .NET?

Thanks, Goran.
Jochen Kalmbach [MVP] - 31 Aug 2006 14:55 GMT
Hi Goran!

> 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?

If you just need an unciode-string, you can use (version for VC2003):

#include <vcclr.h>

const __wchar_t __pin * unmanaged_str = PtrToStringChars(managed_str);

And use the "unmanaged_str" pointer.
See also:
http://blog.kalmbachnet.de/?postid=18
http://support.microsoft.com/kb/311259/

> In normal C++, we usually have conversion operator or a function to get
> it from a string class (string::c_str() or CString::operator LPCTSTR or
> _bstr_t::operator LPCTSTR etc). Is there a similar thing in .NET?

For conversion you could use the following (version for VC2005):

#include <windows.h>
#include <tchar.h>
#include <string>
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

int _tmain()
{
  String ^s = "abc";
  std::string ansi = StringConvA(s);
  std::wstring unicode = StringConvW(s);
  _tprintf(_T("%s"), (LPCTSTR) StringConvT(s));
}

Greetings
  Jochen

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.