Хакер, прошу, воздержись от 'горячих' высказываний....
Я правда, в С++ полный ноль. Сильно не ругай за то, что получилось на VB.
Сначала вот что покажу:
- Код: Выделить всё
System.AccessViolationException: Попытка чтения или записи в защищенную память. Это часто свидетельствует о том, что другая память повреждена.
в System.Runtime.InteropServices.Marshal.CopyToNative(Object source, Int32 startIndex, IntPtr destination, Int32 length)
в System.Windows.Forms.Control.MarshalStringToMessage(String value, Message& m)
в System.Windows.Forms.Control.WmGetControlName(Message& m)
в System.Windows.Forms.Control.WndProc(Message& m)
в System.Windows.Forms.ButtonBase.WndProc(Message& m)
в System.Windows.Forms.Button.WndProc(Message& m)
Так крякнулась софтина на .NET, кнопу из которой я дёрнул.
А вот мой код. Прости, Господи...
- Код: Выделить всё
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As String, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByVal lpBuffer As Long, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const MEM_COMMIT As Long = &H1000
Private Const PAGE_READWRITE As Long = &H4
Private Const GWL_ID = (-12)
Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const SMTO_ABORTIFHUNG = &H2
Private Const SMTO_BLOCK = &H1
Public Function GetControlName(ByVal CtrlHandle As Long, ByRef BufName As String, Optional ByVal BufSize As Long = 1024) As String
Dim ProcessID As Long
Dim NumRead As Long
Dim GetName As Long
Dim dwResult As Long
Dim hProcess As Long
Dim OtherMem As Long
Dim lpOtherMem As Long
Dim SendFlags As Long
Dim lResult As Long
Dim bResult As Boolean
GetName = RegisterWindowMessage("WM_GETCONTROLNAME")
dwResult = GetWindowThreadProcessId(CtrlHandle, ProcessID)
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)
OtherMem = VirtualAllocEx(hProcess, 0, BufSize, MEM_COMMIT, PAGE_READWRITE)
lpOtherMem = OtherMem
SendFlags = SMTO_ABORTIFHUNG Or SMTO_BLOCK
lResult = SendMessageTimeout(CtrlHandle, GetName, BufSize, lpOtherMem, SendFlags, 5000, NumRead)
bResult = ReadProcessMemory(hProcess, OtherMem, BufName, BufSize, NumRead)
bResult = CloseHandle(hProcess)
End Function
Я вот эту строку не понял:
- Код: Выделить всё
LPARAM lpOtherMem = reinterpret_cast<LPARAM>(OtherMem);
Гугл выдал, что это преобразование данных. Но я не понял, во что нужно их преобразовывать. Так что просто присвоил.
VirtualAllocEx ни чего не вернул и потом упало тестовое приложение.
Спаси и сохрани...
Причиной ошибок в коде служит давление со стороны руководства и жесткие временные рамки, им установленные.