예제 #1
0
        public static unsafe bool GetToolbarButton(IntPtr hToolbar, int i, ref TBBUTTON tbButton)
        {
            // One page
            const int BUFFER_SIZE = 0x1000;

            byte[] localBuffer = new byte[BUFFER_SIZE];

            UInt32 processId = 0;
            UInt32 threadId  = WinAPI.GetWindowThreadProcessId(hToolbar, out processId);

            IntPtr hProcess = WinAPI.OpenProcess((int)WinAPI.ProcessAccessFlags.All, false, processId);

            if (hProcess == IntPtr.Zero)
            {
                throw new ArgumentException("OpenProcess failed");
            }

            IntPtr ipRemoteBuffer = WinAPI.VirtualAllocEx(hProcess, IntPtr.Zero, BUFFER_SIZE, WinAPI.AllocationType.Commit, WinAPI.MemoryProtection.ExecuteReadWrite);

            if (ipRemoteBuffer == IntPtr.Zero)
            {
                throw new ArgumentException("VirtualAllocEx failed");
            }

            // TBButton
            fixed(TBBUTTON *pTBButton = &tbButton)
            {
                IntPtr ipTBButton = new IntPtr(pTBButton);

                int b = (int)WinAPI.SendMessage(hToolbar, (int)WinAPI.ToolbarMessage.TB_GETBUTTON, (IntPtr)i, ipRemoteBuffer);

                if (b == 0)
                {
                    throw new ArgumentException("SendMessage TB_GETBUTTON failed");
                }

                // this is fixed
                int ipBytesRead = 0;

                bool b2 = WinAPI.ReadProcessMemory(hProcess, ipRemoteBuffer, ipTBButton, sizeof(TBBUTTON), out ipBytesRead);

                if (!b2)
                {
                    throw new ArgumentException("ReadProcessMemory failed");
                }
            }

            WinAPI.VirtualFreeEx(
                hProcess,
                ipRemoteBuffer,
                0,
                WinAPI.AllocationType.Release);

            WinAPI.CloseHandle(hProcess);

            return(true);
        }
예제 #2
0
        internal static string GetTabCtrlItemText(IntPtr _hWnd, int i)
        {
            const int dwBufferSize       = 2048;
            int       bytesWrittenOrRead = 0;

            WinAPI.TCITEM lvCol;
            string        retval;
            bool          bSuccess;

            System.IntPtr hProcess       = System.IntPtr.Zero;
            System.IntPtr lpRemoteBuffer = System.IntPtr.Zero;
            System.IntPtr lpLocalBuffer  = System.IntPtr.Zero;

            try
            {
                uint processid = 0;
                WinAPI.GetWindowThreadProcessId(_hWnd, out processid);

                lvCol         = new WinAPI.TCITEM();
                lpLocalBuffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(dwBufferSize);
                hProcess      = WinAPI.OpenProcess((int)WinAPI.ProcessAccessFlags.All, false, processid);
                if (hProcess == System.IntPtr.Zero)
                {
                    throw new System.ApplicationException("Failed to access process!");
                }

                lpRemoteBuffer = WinAPI.VirtualAllocEx(hProcess, IntPtr.Zero, dwBufferSize, WinAPI.AllocationType.Commit, WinAPI.MemoryProtection.ExecuteReadWrite);
                if (lpRemoteBuffer == System.IntPtr.Zero)
                {
                    throw new System.SystemException("Failed to allocate memory in remote process");
                }

                lvCol.mask = (int)WinAPI.TCITEM.Filters.TCIF_TEXT;
                lvCol.text = (System.IntPtr)(lpRemoteBuffer.ToInt32() + System.Runtime.InteropServices.Marshal.SizeOf(typeof(WinAPI.TCITEM)));
                lvCol.size = dwBufferSize;
                var size = Marshal.SizeOf(lvCol);
                // Both managed and unmanaged buffers required.
                var bytes = new byte[size];
                var ptr   = Marshal.AllocHGlobal(size);
                // Copy object byte-to-byte to unmanaged memory.
                Marshal.StructureToPtr(lvCol, ptr, false);
                // Copy data from unmanaged memory to managed buffer.
                Marshal.Copy(ptr, bytes, 0, size);
                // Release unmanaged memory.
                //Marshal.FreeHGlobal(ptr);

                bSuccess = WinAPI.WriteProcessMemory(hProcess, lpRemoteBuffer, bytes, System.Runtime.InteropServices.Marshal.SizeOf(typeof(WinAPI.TCITEM)), out bytesWrittenOrRead);
                if (!bSuccess)
                {
                    throw new System.SystemException("Failed to write to process memory");
                }


                WinAPI.SendMessage(_hWnd, (int)WinAPI.TabCtrlMessage.TCM_GETITEMA, (System.IntPtr)i, lpRemoteBuffer);

                bSuccess = WinAPI.ReadProcessMemory(hProcess, lpRemoteBuffer, lpLocalBuffer, dwBufferSize, out bytesWrittenOrRead);

                if (!bSuccess)
                {
                    throw new System.SystemException("Failed to read from process memory");
                }

                retval = System.Runtime.InteropServices.Marshal.PtrToStringUni((System.IntPtr)(lpLocalBuffer.ToInt32() + System.Runtime.InteropServices.Marshal.SizeOf(typeof(WinAPI.TCITEM))));
            }
            finally
            {
                if (lpLocalBuffer != System.IntPtr.Zero)
                {
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(lpLocalBuffer);
                }
                if (lpRemoteBuffer != System.IntPtr.Zero)
                {
                    WinAPI.VirtualFreeEx(hProcess, lpRemoteBuffer, 0, WinAPI.AllocationType.Release);
                }
                if (hProcess != System.IntPtr.Zero)
                {
                    WinAPI.CloseHandle(hProcess);
                }
            }

            return(retval);
        }
예제 #3
0
        public void Click()
        {
            Log.INFO(string.Format("Click ListItem:[{0}, {1}] win[{2}] ", _Text, _hWnd.ToString("X8"), _hWndWin.ToString("X8")));

            const int dwBufferSize       = 2048;
            int       bytesWrittenOrRead = 0;

            WinAPI.LVITEM lvCol;
            string        retval;
            bool          bSuccess;

            System.IntPtr hProcess       = System.IntPtr.Zero;
            System.IntPtr lpRemoteBuffer = System.IntPtr.Zero;
            System.IntPtr lpLocalBuffer  = System.IntPtr.Zero;

            try
            {
                uint processid = 0;
                WinAPI.GetWindowThreadProcessId(_hWnd, out processid);

                lvCol         = new WinAPI.LVITEM();
                lpLocalBuffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(dwBufferSize);
                hProcess      = WinAPI.OpenProcess((int)WinAPI.ProcessAccessFlags.All, false, processid);
                if (hProcess == System.IntPtr.Zero)
                {
                    throw new System.ApplicationException("Failed to access process!");
                }

                lpRemoteBuffer = WinAPI.VirtualAllocEx(hProcess, IntPtr.Zero, dwBufferSize, WinAPI.AllocationType.Commit, WinAPI.MemoryProtection.ExecuteReadWrite);
                if (lpRemoteBuffer == System.IntPtr.Zero)
                {
                    throw new System.SystemException("Failed to allocate memory in remote process");
                }

                lvCol.iItem     = (int)_param;
                lvCol.iSubItem  = 0;
                lvCol.stateMask = (uint)(WinAPI.LVITEM.STATE.LVIS_SELECTED | WinAPI.LVITEM.STATE.LVIS_FOCUSED);
                lvCol.state     = lvCol.stateMask;
                lvCol.iSubItem  = 0;
                lvCol.mask      = 0x0008;
                var size = Marshal.SizeOf(lvCol);
                // Both managed and unmanaged buffers required.
                var bytes = new byte[size];
                var ptr   = Marshal.AllocHGlobal(size);
                // Copy object byte-to-byte to unmanaged memory.
                Marshal.StructureToPtr(lvCol, ptr, false);
                // Copy data from unmanaged memory to managed buffer.
                Marshal.Copy(ptr, bytes, 0, size);
                // Release unmanaged memory.
                //Marshal.FreeHGlobal(ptr);

                bSuccess = WinAPI.WriteProcessMemory(hProcess, lpRemoteBuffer, bytes, System.Runtime.InteropServices.Marshal.SizeOf(typeof(WinAPI.LVITEM)), out bytesWrittenOrRead);
                if (!bSuccess)
                {
                    throw new System.SystemException("Failed to write to process memory");
                }


                int rowId = (int)_param;
                WinAPI.SendMessage(_hWnd, (int)WinAPI.ListViewMessages.LVM_SETITEMSTATE, new IntPtr(rowId), lpRemoteBuffer);
            }
            finally
            {
                if (lpLocalBuffer != System.IntPtr.Zero)
                {
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(lpLocalBuffer);
                }
                if (lpRemoteBuffer != System.IntPtr.Zero)
                {
                    WinAPI.VirtualFreeEx(hProcess, lpRemoteBuffer, 0, WinAPI.AllocationType.Release);
                }
                if (hProcess != System.IntPtr.Zero)
                {
                    WinAPI.CloseHandle(hProcess);
                }
            }
        }