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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:02:22+00:00 2026-05-12T22:02:22+00:00

I’d like to persist pidl s between sessions, so that my application can remember

  • 0

I’d like to persist pidls between sessions, so that my application can remember the users’ folder selections, wherever they may be in the namespace, even if they’re not file-system folders.

I have a feeling that the way to do this is to write out the binary contents of the ITEMIDLIST itself, but I can’t confirm this for sure, since these contents are supposed to be opaque, and are up to the provider. I don’t know if after a reboot, or even in another process, if this data is valid. It could contain pointers, for all I know.

What is the proper way to persist and later reconstruct a pidl?

Update:

Jerry Coffin has suggested a pair of functions that seem to do exactly what I asked. One question remains, however.

As Joel Spolsky points out, Raymond Chen seems to imply that saving the binary contents of the ITEMIDLIST is indeed the proper way to persist a pidl, from which one can infer that ILSaveToStream and ILLoadFromStream are helper functions that do just that.

I can’t find documentation that proves this, however. Since this project is in C#, I’d prefer to avoid having to interop up an IStream for the IL... functions and just persist the binary data myself if possible. Can anybody confirm that this is correct?

Solution notes:

Looking at the docs for ILSaveToStream and ILLoadFromStream, I see that these functions didn’t even exist until version 5.0 of the shell (Windows 2000). So how was this done prior to Win2K? After some testing, I conclude that, as I suspected and Joel Spolsky postulated, writing out the raw ITEMIDLIST is the way to go.

A simple implementation in C# follows:

unsafe{
    byte* start = (byte*)pidl.ToPointer();
    byte* ptr   = start;
    ushort* length;

    do{
        length = (ushort*)ptr;
        ptr += *length;
    }while(*length != 0);

    byte[] rtn = new byte[ptr + 2 - start];
    Marshal.Copy(pidl, rtn, 0, rtn.Length);
    return rtn;
}

Of course, this could be done without pointers using Marshal.ReadInt16:

int offset = 0;
int length;

do{
    length = Marshal.ReadInt16(pidl, offset);
    offset += length;
}while(length != 0);

byte[] rtn = new byte[offset + 2];
Marshal.Copy(pidl, rtn, 0, rtn.Length);
return rtn;

It only costs a few more clock cycles, but it still requires full trust, so it doesn’t really buy much besides staying away from scaaaary-looking pointers.

Reconstructing the pidl is even easier, since the total length of the data is already known, and it doesn’t even need any pointers:

byte[] itemidlist = ReadPidl();
IntPtr pidl = Marshal.AllocCoTaskMem(itemidlist.Length);
Marshal.Copy(itemidlist, 0, pidl, itemidlist.Length);

Persisting and reconstructing pidls in this way worked in all of my tests across processes, and, in limited scenarios, even across machines. I haven’t yet tested across reboots, as I’m loath to close everything and restart my machine, but given the apparent cross-machine compatibility, I’m confident in this solution.

I’m accepting Joel Spolsky’s answer as the solution, but do want to give a caveat for future passersby: Joel talks about writing out a SHITEMID structure, but this is not the whole story. An ITEMIDLIST (which is what a pidl points to) is actually a null-terminated list of these variable-length SHITEMID structures, and the whole list must be persisted. This is why the code above executes a loop to determine the total length. It jumps from element to element in this list, reading the length of each element to find out the offset to the next one. Only after an element length of zero is read is the length of the entire list known.

  • 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-12T22:02:22+00:00Added an answer on May 12, 2026 at 10:02 pm

    According to Raymond Chen you can persist a pidl — or, more specifically, a SHITEMID structure, just by writing out length of the item and then the bytes.

    Notice that this struct is a typical Windows variable length struct, with a cb (“count of bytes”) element specifying the length of the structure in bytes, followed by the rest of the data. In other words, to write the structure, you need to write cb bytes. To read it, you need to allocated cb bytes of memory and set the cb field.

    Be careful not to use sizeof(SHITEMID) because the way it’s declared assumes only one byte for the abID field so that won’t be big enough.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
For some reason, after submitting a string like this Jack’s Spindle from a text
I have text I am displaying in SIlverlight that is coming from a CMS
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.