public void Select(string value) { Log.INFO(string.Format("Select TabCtrl:[{0}] value:[{1}]", _Text, value)); //Console.WriteLine(string.Format("{0} {1}", hWnd.ToString("x8"), winClass)); int index = -1; int rowNum = WinAPI.GetTabCtrlItemNum(_hWnd); for (int i = 0; i < rowNum; i++) { string strText = WinAPI.GetTabCtrlItemText(_hWnd, i); if (strText == value) { index = i; break; } } if (index == -1) { throw new Exception(string.Format("Can not find value[{0}] in TabCtrl:[{1}]", value, _Text)); } WinAPI.SendMessage(_hWnd, (int)WinAPI.TabCtrlMessage.TCM_SETCURFOCUS, (IntPtr)(index), IntPtr.Zero); WinAPI.SendMessage(_hWnd, (int)WinAPI.TabCtrlMessage.TCM_SETCURSEL, (IntPtr)(index), IntPtr.Zero); }
internal static int GetListViewColumnNum(IntPtr hWnd) { IntPtr hWndHdr = WinAPI.SendMessage(hWnd, (int)WinAPI.ListViewMessages.LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero); int count = (int)SendMessage(hWndHdr, (int)WinAPI.ListViewMessages.HDM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero); return(count); }
public void Click() { Log.INFO(string.Format("Click ListBox:[{0}, {1}] win[{2}] ", _Text, _hWnd.ToString("X8"), _hWndWin.ToString("X8"))); IntPtr text = Marshal.StringToCoTaskMemUni(_Text); WinAPI.SendMessage(_hWnd, (int)WinAPI.ListBoxMessages.LB_SELECTSTRING, (IntPtr)(-1), _Text); Marshal.FreeCoTaskMem(text); Thread.Sleep(2000); IntPtr parenthWnd = WinAPI.GetWindowLongPtr(_hWnd, (int)WinAPI.GWL.GWL_HWNDPARENT); IntPtr nID = WinAPI.GetWindowLongPtr(_hWnd, (int)WinAPI.GWL.GWL_ID); int ctrl_id = nID.ToInt32(); WinAPI.PostMessage(parenthWnd, (int)WinAPI.WMMessage.WM_COMMAND, (IntPtr)WinAPI.MakeWParam(ctrl_id, (int)WinAPI.ListBoxMessages.LBN_SELCHANGE), _hWnd); //WinAPI.LVITEM lvi = new WinAPI.LVITEM(); //lvi.stateMask = (uint)WinAPI.LVITEM.STATE.LVIS_SELECTED; //lvi.state = (uint)WinAPI.LVITEM.STATE.LVIS_SELECTED; //IntPtr ptrLvi = Marshal.AllocHGlobal(Marshal.SizeOf(lvi)); //Marshal.StructureToPtr(lvi, ptrLvi, false); //int rowId = (int)_param; //WinAPI.SendMessage(_hWnd, (int)WinAPI.ListBoxMessages.LB_SETCURSEL, new IntPtr(rowId), ptrLvi); }
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); }
public void WaitFinish() { while (true) { int value = (int)WinAPI.SendMessage(_hWnd, (int)WinAPI.ProgressBarMessage.PBM_GETPOS, IntPtr.Zero, IntPtr.Zero); Console.WriteLine("Wait ProgressBar Finish" + value); Thread.Sleep(1000); } }
public string GetValue() { Int32 buffer_size = WinAPI.SendMessage(_hWnd, (int)WinAPI.WMMessage.WM_GETTEXTLENGTH, (IntPtr)0, (IntPtr)0).ToInt32(); StringBuilder buffer = new StringBuilder(buffer_size + 1); WinAPI.SendMessage(_hWnd, (int)WinAPI.WMMessage.WM_GETTEXT, (IntPtr)buffer_size + 1, buffer); string text = buffer.ToString(); Log.INFO(string.Format("Get Editor:[{0}, {1}] value:[{2}] Win:[{3}]", _hWnd.ToString("X8"), _Text, text, _hWndWin.ToString("X8"))); return(text); }
public void SetValue(string value) { if (value == "") { Log.INFO(string.Format("Set Editor:[{0}, {1}] with default value:[{2}] Win:[{3}]", _Text, _hWnd.ToString("X8"), GetValue(), _hWndWin.ToString("X8"))); return; } Log.INFO(string.Format("Set Editor:[{0}, {1}] value:[{2}] Win:[{3}]", _Text, _hWnd.ToString("X8"), value, _hWndWin.ToString("X8"))); IntPtr text = Marshal.StringToCoTaskMemUni(value); WinAPI.SendMessage(_hWnd, (int)WinAPI.WMMessage.WM_SETTEXT, (IntPtr)(value.Length + 1), value); Thread.Sleep(2000); Marshal.FreeCoTaskMem(text); }
public void Select(string value) { Log.INFO(string.Format("Select ComboBox:[{0}] value:[{1}] Win:[{2}]", _Text, value, _hWndWin.ToString("X8"))); IntPtr text = Marshal.StringToCoTaskMemUni(value); WinAPI.SendMessage(_hWnd, (int)WinAPI.ComboBoxMessage.CB_SELECTSTRING, (IntPtr)(-1), value); Marshal.FreeCoTaskMem(text); Thread.Sleep(2000); IntPtr parenthWnd = WinAPI.GetWindowLongPtr(_hWnd, (int)WinAPI.GWL.GWL_HWNDPARENT); IntPtr nID = WinAPI.GetWindowLongPtr(_hWnd, (int)WinAPI.GWL.GWL_ID); int ctrl_id = nID.ToInt32(); WinAPI.PostMessage(parenthWnd, (int)WinAPI.WMMessage.WM_COMMAND, (IntPtr)WinAPI.MakeWParam(ctrl_id, (int)WinAPI.ComboBoxMessage.CBN_SELCHANGE), _hWnd); }
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); }
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); } } }