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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:31:59+00:00 2026-06-08T10:31:59+00:00

Just to clarify something first. I am not trying to convert a byte array

  • 0

Just to clarify something first. I am not trying to convert a byte array to a single string. I am trying to convert a byte-array to a string-array.

I am fetching some data from the clipboard using the GetClipboardData API, and then I’m copying the data from the memory as a byte array. When you’re copying multiple files (hence a CF_HDROP clipboard format), I want to convert this byte array into a string array of the files copied.

Here’s my code so far.

//Get pointer to clipboard data in the selected format
var clipboardDataPointer = GetClipboardData(format);

//Do a bunch of crap necessary to copy the data from the memory
//the above pointer points at to a place we can access it.
var length = GlobalSize(clipboardDataPointer);
var @lock = GlobalLock(clipboardDataPointer);

//Init a buffer which will contain the clipboard data
var buffer = new byte[(int)length];

//Copy clipboard data to buffer
Marshal.Copy(@lock, buffer, 0, (int)length);

GlobalUnlock(clipboardDataPointer);

snapshot.InsertData(format, buffer);

Now, here’s my code for reading the buffer data afterwards.

var formatter = new BinaryFormatter();
using (var serializedData = new MemoryStream(buffer))
{
    paths = (string[]) formatter.Deserialize(serializedData);
}

This won’t work, and it’ll crash with an exception saying that the stream doesn’t contain a binary header. I suppose this is because it doesn’t know which type to deserialize into.

I’ve tried looking the Marshal class through. Nothing seems of any relevance.

  • 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-06-08T10:32:01+00:00Added an answer on June 8, 2026 at 10:32 am

    If the data came through the Win32 API then a string array will just be a sequence of null-terminated strings with a double-null-terminator at the end. (Note that the strings will be UTF-16, so two bytes per character). You’ll basically need to pull the strings out one at a time into an array.

    The method you’re looking for here is Marshal.PtrToStringUni, which you should use instead of Marshal.Copy since it works on an IntPtr. It will extract a string, up to the first null character, from your IntPtr and copy it to a string.

    The idea would be to continually extract a single string, then advance the IntPtr past the null byte to the start of the next string, until you run out of buffer. I have not tested this, and it could probably be improved (in particular I think there’s a smarter way to detect the end of the buffer) but the basic idea would be:

    var myptr = GetClipboardData(format);
    var length = GlobalSize(myptr);
    
    var result = new List<string>();
    
    var pos = 0;
    while ( pos < length )
    {
        var str = Marshal.PtrToStringUni(myptr);
        var count = Encoding.Unicode.GetByteCount(str);
    
        myptr = IntPtr.Add(myptr, count + 1);
        pos += count + 1;
    
        result.Add(str);
    }
    
    return result.ToArray();
    

    (By the way: the reason your deserialization doesn’t work is because serializing a string[] doesn’t just write out the characters as bytes; it writes out the structure of a string array, including additional internal bits that .NET uses like the lengths, and a binary header with type information. What you’re getting back from the clipboard has none of that present, so it cannot be deserialized.)

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

Sidebar

Related Questions

Just to clarify, by specifying something like VARCHAR(45) means it can take up to
Just to clarify up-front: I'm talking about unioning geometry, not the SQL keyword UNION
Ok just want to clarify something with my solution. I have a requirement to
I guess with what I'm going to write I should first clarify, it's not
There are similar posts here. I just want to clarify something. I implemented this
Just to clarify, when I say multiple assigment, parallel assignment, destructuring bind I mean
just wanted to ask where I define initial class properties? From other languages I
Just see this: SELECT clientid,clientname,startdate,enddate,age FROM clients WHERE clientid IN (1,2,3,4,5) AND CASE WHEN
I need something real simple, that for some reason I am unable to accomplish
Just to clarify that I also think the title is a bit silly. We

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.