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 6175287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:53:58+00:00 2026-05-23T23:53:58+00:00

I tested the code from Stack Overflow question Marshaling unmanaged char** to managed string[]

  • 0

I tested the code from Stack Overflow question Marshaling unmanaged char** to managed string[], and it works well.

I tried to convert it to Unicode, and then I start getting “The handle is invalid”. Why?

My modified code:

_declspec(dllexport) void TestArray(wchar_t** OutBuff, int Count, int MaxLength)
{
    for(int i=0; i<Count; i++)
    {
        wchar_t buff[25];
        _itow(i, buff, 10);
        wcsncpy(OutBuff[i], buff, MaxLength);
    }
}

And the C# wrapper:

class Program
{
    [DllImport("Native.dll", EntryPoint = "?TestArray@@YAXPAPA_WHH@Z", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    private static extern void TestArray([MarshalAs(UnmanagedType.LPArray)]
    IntPtr[] OutBuff, int Count, int MaxLength);

    static void Main(string[] args)
    {
        int count = 10;
        int maxLen = 50;
        IntPtr[] buffer = new IntPtr[maxLen];
        for (int i = 0; i < count; i++)
            buffer[i] = Marshal.AllocHGlobal(maxLen);

        TestArray(buffer, count, maxLen);

        string[] output = new string[count];
        for (int i = 0; i < count; i++)
        {
            output[i] = Marshal.PtrToStringUni(buffer[i]);
            Marshal.FreeHGlobal(buffer[i]); // Crash is here, when count is 1.
            Console.WriteLine(output[i]);
        }
        Console.ReadKey();
    }
}
  • 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-23T23:53:59+00:00Added an answer on May 23, 2026 at 11:53 pm

    You are lucky to get the Windows heap manager to catch the bug, you must be using Vista or Windows 7. It is a subtle one and would have been easily missed if you hadn’t used wcsncpy(). The problem is that you allocated maxLen bytes for the string. But you need to allocate maxLen characters. Not the same thing when you use Unicode strings, one character is two bytes. You also got the buffer allocation wrong. Fix:

            IntPtr[] buffer = new IntPtr[count];   // NOTE: not maxLen
            for (int i = 0; i < count; i++) 
                buffer[i] = Marshal.AllocHGlobal(maxLen * sizeof(Char)); 
    

    The heap corruption occurs because of the way wcsncpy() works. If fills the remainder of the buffer with zeros, unlike wcscpy_s(). Since your buffer is too small, that overwrites and corrupts the heap. Windows stepped in when it noticed that the internal heap structure was compromised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The calculations in my code are well-tested, but because there is so much GUI
I just tested which type of code is executed by the WinForms Designer in
Why doesn't following code work correctly in FireFox 3.6? I have tested in IE7,
have not tested on windows. but in ubuntu when u disconnect from the network,
const char *Greet(const char *c) { string name; if(c) name = c; if (name.empty())
I have a question about SSJS. With SSJS, is it possible to hide code
I've tested it and it works in opera, safair, IE, and firefox. I've read
The following code works fine: typedef struct node_s { void *data; struct node_s *next;
Well this is not the first time im asking these question here but I
QA tester was reading HTML/JS code to write a functional test of a web

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.