Skip to content

Work with PCZZWSTR #1299

Answered by AArnott
sensslen asked this question in Q&A
Oct 31, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

The simplest way to demonstrate is this:

unsafe
{
    string message = "message1\0message2\0\0";
    fixed (char* pMessage = message)
    {
        PCZZWSTR wstr = new(pMessage);
    }
}

Note that this doesn't copy the memory, it merely points to it. So the wstr I initialize here is only valid while you're inside the fixed block.

If you want to use it longer term, you can allocate native memory:

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Windows.Win32.Foundation;

string[] strings = ["message1", "message2"];
PCZZWSTR list = CreateStringList(strings);
try
{
    // use it here.
}
finally
{
    FreeStringList(ref list);
}

// Alloca…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@sensslen
Comment options

Answer selected by sensslen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants