Issue with SHAppBarMessage for TaskBar Position on Windows Server 2008R2
Asked Answered
B

1

1

I am having a really strange issue with SHAppBarMessage, have been trying out different things for few hours now but i am not getting it. I am trying to get the TaskBar Position. It works fine on Windows 8 but on Windows Server the return value is SHAppBarMessage.

The following code from an answer works from fine on Windows 8, but on Windows 2008R2 it has a strange behavior.

    public static System.Drawing.Rectangle GetTaskbarPosition()
    {
        var data = new APPBARDATA();
        data.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(data);
        IntPtr retval = SHAppBarMessage(ABM_GETTASKBARPOS, ref data);
        if (retval == IntPtr.Zero) throw new Exception("Please re-install Windows");
        return new System.Drawing.Rectangle(data.rc.left, data.rc.top,
            data.rc.right - data.rc.left, data.rc.bottom - data.rc.top);
    }

    // P/Invoke goo:
    private const int ABM_GETTASKBARPOS = 5;
    [System.Runtime.InteropServices.DllImport("shell32.dll")]
    private static extern IntPtr SHAppBarMessage(int msg, ref APPBARDATA data);
    private struct APPBARDATA
    {
        public int cbSize;
        public IntPtr hWnd;
        public int uCallbackMessage;
        public int uEdge;
        public RECT rc;
        public IntPtr lParam;
    }
    private struct RECT
    {
        public int left, top, right, bottom;
    }

I am totally out of ideas. As it should work as it is. But i dont know why it is not working. Am i missing something?

Seeing is believing

Bernardobernarr answered 30/11, 2013 at 9:42 Comment(11)
Have you tried re-installing Windows?Koziarz
No not yet was trying to figure it out first.Bernardobernarr
I don't know whether it's the default, but pinvoke.net includes a StructLayoutAttribute on APPBARDATA and RECT pinvoke.net/default.aspx/shell32/APPBARDATA.htmlKoziarz
i did try that as well, Infact initially my call was with StructLayout. As i said, have been looking into it so i have already tried all the possible things that comes to my mind or found it over internet.Bernardobernarr
What happens if you run it on AnyCPU/x86/x64 architectures explicitly?Koziarz
On all the Architectures it behaves the same way, infact i tried it on .Net4 and .Net4.5 as well, But no gain.Bernardobernarr
Well I have no ideas left other than really wild guesses. It appears you're using a console app? Put STAThreadAttribute on the Main function. And try the code again, but in a WinForms project. Hook the code up to a button?Koziarz
Embarcadero (the Delphi guys) have an example that explicitly gets the HWND for Shell_TrayWnd first. edn.embarcadero.com/article/26977Koziarz
I better try it on some other machine then.Bernardobernarr
You are violating this site's license. You must include a link to the post where you obtained the code as well as a link to the author of the post.Flourish
I have added the link thanks for mentioning.Bernardobernarr
K
2

According to the documentation for ABM_GETTASKBARPOS:

fResult = (BOOL) SHAppBarMessage(ABM_GETTASKBARPOS, pabd);

pabd

A pointer to an APPBARDATA structure whose rc member receives the bounding rectangle, in screen coordinates, of the taskbar. You must specify the cbSize and hWnd when sending this message; all other members are ignored.

Emphasis mine.

This Delphi code sample suggests as much, it looks for a window by the name of Shell_TrayWnd:

// 'Shell_TrayWnd' is the name of the task bar's window
AppData.Hwnd := FindWindow('Shell_TrayWnd', nil);
Koziarz answered 30/11, 2013 at 10:4 Comment(1)
Yes you are hWnd should be provided, but no gain. My believe is now getting stronger that this is OS installation issue. Something's corrupted may be. But thanks for your held.Bernardobernarr

© 2022 - 2024 — McMap. All rights reserved.