Skip to content

Getting the Value from PWSTR #181

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

You must be logged in to vote

You have to initialize the PWSTR to actually point to a character buffer, otherwise you effectively pass in NULL.
But if you forget the PWSTR part and just use char*, it gets a bit simpler. This works, with added error checking:

    public void GetAllWindowsInfo()
    {
        bool windowReturn = PInvoke.EnumWindows(
            (HWND handle, LPARAM customParam) =>
            {
                int bufferSize = PInvoke.GetWindowTextLength(handle) + 1;
                unsafe
                {
                    fixed (char* windowNameChars = new char[bufferSize])
                    {
                        if (PInvoke.GetWindowText(handle, windowNameChars, bufferSize) == 0)

Replies: 4 comments 14 replies

Comment options

You must be logged in to vote
4 replies
@JesseAtExitNorth
Comment options

@jnm2
Comment options

@Al-Lo1
Comment options

@jnm2
Comment options

Answer selected by JesseAtExitNorth
Comment options

You must be logged in to vote
2 replies
@jnm2
Comment options

@Al-Lo1
Comment options

Comment options

You must be logged in to vote
6 replies
@netcorefan1
Comment options

@jnm2
Comment options

@netcorefan1
Comment options

@jnm2
Comment options

@tannergooding
Comment options

Comment options

You must be logged in to vote
2 replies
@jnm2
Comment options

@netcorefan1
Comment options

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