Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:09:32+00:00 2026-05-23T13:09:32+00:00

I am wrapping some unmanaged C++ code inside a .NET project. For this I

  • 0

I am wrapping some unmanaged C++ code inside a .NET project. For this I need to convert System::String to UTF8-bytes stored in char*.

I am unsure if this is the best or even a correct way to do this and I’d appreciate if someone could take a look and provide feedback.

Thanks,

/David

// Copy into blank VisualStudio C++/CLR command line solution.
#include "stdafx.h"
#include <stdio.h>

using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;

// Test for calling with char* argument.
void MyTest(const char* buffer)
{
    printf_s("%s\n", buffer);
    return;
}

int main()
{

   // Create a UTF-8 encoding.
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;

   // A Unicode string with two characters outside an 8-bit code range.
   String^ unicodeString = L"This unicode string contains two characters with codes outside an 8-bit code range, Pi (\u03a0) and Sigma (\u03a3).";
   Console::WriteLine(unicodeString);

   // Encode the string.
   array<Byte>^encodedBytes = utf8->GetBytes(unicodeString);

   // Get pointer to unmanaged char array
   int size = Marshal::SizeOf(encodedBytes[0]) * encodedBytes->Length;
   IntPtr pnt = Marshal::AllocHGlobal(size);
   Marshal::Copy(encodedBytes, 0, pnt, encodedBytes->Length);

   // Ugly, but necessary?
   char *charPnt= (char *)pnt.ToPointer();
   MyTest(charPnt);
   Marshal::FreeHGlobal(pnt);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:09:32+00:00Added an answer on May 23, 2026 at 1:09 pm
    1. You don’t need to create an encoder instance, you can use the static instances.

    2. If the called function doesn’t expect a pointer to the HGlobal heap you can just use plain C/C++ memory allocation (new or malloc) for the buffer.

    3. In your example the function doesn’t take ownership so you don’t need a copy at all, just pin the buffer.

    Something like:

    // Encode the text as UTF8
    array<Byte>^ encodedBytes = Encoding::UTF8->GetBytes(unicodeString);
    
    // prevent GC moving the bytes around while this variable is on the stack
    pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
    
    // Call the function, typecast from byte* -> char* is required
    MyTest(reinterpret_cast<char*>(pinnedBytes), encodedBytes->Length);
    

    Or if you need the string zero-terminated like most C functions (including the example in the OP) then you should probably add a zero byte.

    // Encode the text as UTF8, making sure the array is zero terminated
    array<Byte>^ encodedBytes = Encoding::UTF8->GetBytes(unicodeString + "\0");
    
    // prevent GC moving the bytes around while this variable is on the stack
    pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
    
    // Call the function, typecast from byte* -> char* is required
    MyTest(reinterpret_cast<char*>(pinnedBytes));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wrapping existing C++ code from a BSD project in our own custom
I'm having some problems wrapping my head around how to do this. I have
Greetings! I'm working on wrapping my head around LINQ. If I had some XML
I am wrapping up a project in which I used jQuery for the first
I am having a problem with links wrapping. How do I prevent this? ![Wordwrap][1]
I'm using a 3rd party DLL written in unmanaged C++ that controls some hardware
I have been tasked with maintaining a legacy unmanaged c++ system. I do not
I'm still wrapping my head around some of the nuances of memory management in
I'm wrapping a C++ library in PHP using SWIG and there have been some
I often see web applications where a program is basically some javascript objects wrapping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.