Question : Adding new button to windows taskbar

I am having trouble adding a new button to the windows taskbar. Can anyone help me with this? I have posted some code that is giving me a problem. The SendMessage command with the INSERT_BUTTON constant is crashing explorer and I can't seem to figure out why. Anyone have any ideas?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
        unsafe public void AddButton(ref Win32API.Structs.TBBUTTON theButtonStruct)
        {
            const int BUFFER_SIZE = 0x1000;
 
            if (!FindTaskBar())
                return;
 
            byte[] bytLocalBuffer = new byte[BUFFER_SIZE];
            UInt32 uInt32ProcessID = 0;
 
            UInt32 uInt32ThreadID = (UInt32)Win32API.Functions.User32.GetWindowThreadProcessId(m_TaskbarWindowHandleIntPtr, out uInt32ProcessID);
            IntPtr intPtrProcessHandle = Win32API.Functions.Kernel32.OpenProcess(Win32API.Constants.ProcessRights.ALL_ACCESS, false, uInt32ProcessID);
 
            if (intPtrProcessHandle == IntPtr.Zero)
                return;
 
            IntPtr intPtrRemoteBuffer = Win32API.Functions.Kernel32.VirtualAllocEx(intPtrProcessHandle, IntPtr.Zero, new UIntPtr(BUFFER_SIZE), Win32API.Constants.MemoryAllocationType.COMMIT, Win32API.Constants.MemoryProtection.PAGE_READ_WRITE);
 
            if (intPtrRemoteBuffer == IntPtr.Zero)
                return;
 
            fixed (Win32API.Structs.TBBUTTON* ptrButton = &theButtonStruct)
            {
                IntPtr intPtrButton = new IntPtr(ptrButton);
 
                Int32 int32BytesWritten = 0;
                IntPtr intPtrBytesWritten = new IntPtr(&int32BytesWritten);
 
                uint uIntWrite = 0;
                IntPtr intPtrWrite = new IntPtr(&uIntWrite);
                string strText = "Test";
 
                byte[] byt = Encoding.ASCII.GetBytes(strText);
 
                bool blnReturn = Win32API.Functions.Kernel32.WriteProcessMemory(intPtrProcessHandle, intPtrRemoteBuffer, byt, new UIntPtr((uint)strText.Length), out intPtrBytesWritten);
 
                theButtonStruct.iString = Win32API.Functions.User32.SendMessage(m_TaskbarWindowHandleIntPtr, Win32API.Constants.ToolbarButtonMessages.ADD_STRING, IntPtr.Zero, intPtrRemoteBuffer);
 
                blnReturn = Win32API.Functions.Kernel32.WriteProcessMemory(intPtrProcessHandle, intPtrRemoteBuffer, intPtrButton, new UIntPtr((uint)sizeof(Win32API.Structs.TBBUTTON)), out intPtrBytesWritten);
 
                int intCount = GetButtonCount();
 
                int intTest = Win32API.Functions.User32.SendMessage(m_TaskbarWindowHandleIntPtr, Win32API.Constants.ToolbarButtonMessages.INSERT_BUTTON, (IntPtr)(intCount-1), intPtrRemoteBuffer);
            }
 
            Win32API.Functions.Kernel32.VirtualFreeEx(intPtrProcessHandle, intPtrRemoteBuffer, UIntPtr.Zero, Win32API.Constants.MemoryAllocationType.RELEASE);
            Win32API.Functions.Kernel32.CloseHandle(intPtrProcessHandle);
        }

Answer : Adding new button to windows taskbar

If tasked to solve the same problem, I think I'd write a little application program that did the organizing and selection itself -- outside of the Explorer/Taskbar system.  

You will always be going uphill when trying to modify the behavior of a system tool that does not provide specific documentation describing how to make it work in a non-standard way.

Random Solutions  
 
programming4us programming4us