Skip to content

Marshalling the lpDesktop field of the STARTUPINFOW structure #769

Answered by AArnott
vitkuz573 asked this question in Q&A
Discussion options

You must be logged in to vote

There are two sides to your question:

Is it possible to marshall the lpDesktop field of the STARTUPINFOW structure to string?

Yes. This is easy: just call PWSTR.ToString().

But your code snippet asks a different question:

var si = new STARTUPINFOW();
si.lpDesktop = @"winsta0\" + desktopName;

This requires marshaling a string to a PWSTR, which is a bit trickier, because now you have to manage pinned memory while you use the struct. Here's how to do it:

fixed (char* pDesktopName = @"winsta0\" + desktopName)
{
    STARTUPINFOW si = default;
    si.lpDesktop = pDesktopName;

    // Also *pass* STARTUPINFOW to any function that needs it within this fixed block so the pointer remains valid.
}

A…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

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

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

This discussion was converted from issue #768 on November 12, 2022 00:29.