J'essayais d'obtenir du texte de chaque contrôle dans la hiérarchie. Le code suivant fonctionne correctement si j'utilise la méthode unsafe
. Cependant, en utilisant la version non gérée semble briser hWnd
, qui se plaint résultat hWnd = GetAncestor(hWnd, GetAncestorFlags.GA_PARENT)
:Quel est le problème avec ce code non géré en C#?
System.AccessViolationException: « Tentative de lecture ou d'écriture de mémoire protégée. C'est souvent une indication que l'autre mémoire est corrompue.
J'ai vérifié hWnd
n'a pas été changé après le retour de la fonction GetWindowTextRaw
, et si je commente la deuxième SendMessage
dans cette fonction ne causera pas la question (bien qu'il se clairement pas le texte de la fenêtre).
(PS: J'utilise PInvoke.User32 dans NuGet)
// using static PInvoke.User32;
public static string GetWindowTextRaw(IntPtr hWnd) {
// Allocate correct string length first
int length = (int)SendMessage(hWnd, WindowMessage.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
char[] buff = new char[length + 1];
IntPtr iptr = Marshal.AllocHGlobal(buff.Length);
SendMessage(hWnd, WindowMessage.WM_GETTEXT, (IntPtr)(length + 1), iptr);
Marshal.Copy(iptr, buff, 0, length + 1);
Marshal.FreeHGlobal(iptr);
//unsafe
//{
// fixed (char* p = buff)
// SendMessage(hWnd, WindowMessage.WM_GETTEXT, (IntPtr)(length + 1), (IntPtr)p);
//}
return new string(buff).TrimEnd('\0');
}
private void button1_Click(object sender, EventArgs {
POINT p;
IntPtr hWnd;
//while (true)
if (GetCursorPos(out p)) {
hWnd = WindowFromPoint(p); ;
Debug.Print($"{p.x} {p.y} 0x{(int)hWnd:x8}");
while (hWnd != IntPtr.Zero) {
Debug.Print($"{GetWindowTextRaw(hWnd)}");
hWnd = GetAncestor(hWnd, GetAncestorFlags.GA_PARENT);
}
Thread.Sleep(500);
}
}
Vous pouvez envisager d'utiliser C++/CLI pour le code d'interopérabilité. –
Copie possible de [Tentative de lecture ou d'écriture de la mémoire protégée. C'est souvent une indication que l'autre mémoire est corrompue] (http://stackoverflow.com/questions/4074585/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that- ot) – ThePerplexedOne
@ Howаn Comment? Des références? Merci! –