gpt4 book ai didi

c# - 调用 SendMessage (P/Invoke) 一直崩溃

转载 作者:行者123 更新时间:2023-11-30 12:37:01 25 4
gpt4 key购买 nike

我必须编写一个与第三方程序通信的应用程序(AOL,对不起。:()

通过大量研究,我找到了一些使用 P/Invoke 来做到这一点的方法,并且在大多数情况下它工作没问题,但它会在随后的试验中崩溃,特别是 SendMessage。我在下面概述了崩溃代码。

所有这些都被移植到 .NET来自旧的、旧的 Visual Basic 文件。它可能已经过时了,如果它不可行,我也能理解 - 我只是希望有比 Visual Basic 4.0 更好的方法来完成这项工作。

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter,
string lpszClass,
string lpszWindow);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle,
IntPtr childAfter,
string className,
IntPtr windowTitle);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(HandleRef hWnd,
UInt32 Msg,
IntPtr wParam,
IntPtr lParam);

[DllImport("user32.dll", EntryPoint="SendMessageW")]
public static extern IntPtr SendMessageByString(HandleRef hWnd,
UInt32 Msg,
IntPtr wParam,
StringBuilder lParam);

[DllImport("user32.dll", CharSet = CharSet.Unicode , EntryPoint = "SendMessageW")]
public static extern IntPtr SendMessageByString(HandleRef hWnd,
UInt32 Msg,
IntPtr wParam,
String lParam);

public IntPtr FindClientWindow()
{
IntPtr aol = IntPtr.Zero;
IntPtr mdi = IntPtr.Zero;
IntPtr child = IntPtr.Zero;
IntPtr rich = IntPtr.Zero;
IntPtr aollist = IntPtr.Zero;
IntPtr aolicon = IntPtr.Zero;
IntPtr aolstatic = IntPtr.Zero;

aol = Invoke.FindWindow("AOL Frame25", null);
mdi = Invoke.FindWindowEx(aol, IntPtr.Zero, "MDIClient", null);
child = Invoke.FindWindowEx(mdi, IntPtr.Zero, "AOL Child", null);
rich = Invoke.FindWindowEx(child, IntPtr.Zero, "RICHCNTL", null);
aollist = Invoke.FindWindowEx(child, IntPtr.Zero, "_AOL_Listbox", null);
aolicon = Invoke.FindWindowEx(child, IntPtr.Zero, "_AOL_Icon", null);
aolstatic = Invoke.FindWindowEx(child, IntPtr.Zero, "_AOL_Static", null);

if (rich != IntPtr.Zero &&
aollist != IntPtr.Zero &&
aolicon != IntPtr.Zero &&
aolstatic != IntPtr.Zero)

return child;
do
{
child = Invoke.FindWindowEx(mdi, child, "AOL Child", null);
rich = Invoke.FindWindowEx(child, IntPtr.Zero, "RICHCNTL", null);
aollist = Invoke.FindWindowEx(child, IntPtr.Zero, "_AOL_Listbox", null);
aolicon = Invoke.FindWindowEx(child, IntPtr.Zero, "_AOL_Icon", null);
aolstatic = Invoke.FindWindowEx(child, IntPtr.Zero, "_AOL_Static", null);

if (rich != IntPtr.Zero &&
aollist != IntPtr.Zero &&
aolicon != IntPtr.Zero &&
aolstatic != IntPtr.Zero)

return child;
}
while (child != IntPtr.Zero)
;

return child;
}

IntPtr room = IntPtr.Zero;
IntPtr child = IntPtr.Zero;
IntPtr length = IntPtr.Zero;
IntPtr roomHandle = IntPtr.Zero;

child = FindClientWindow();
room = FindChildByClass(child, "RICHCNTLREADONLY");

HandleRef n = new HandleRef(IntPtr.Zero, room);

length = SendMessage(n, 0x000E, IntPtr.Zero, IntPtr.Zero);

// This is the line that keeps crashing on me.
SendMessageByString(n, 0x000D, new IntPtr( length.ToInt32() + 1 ), str);

public IntPtr FindChildByClass(IntPtr parent, string child)
{
return Invoke.FindWindowEx(parent, IntPtr.Zero, child, null);
}

最佳答案

您正在使用宽字节 SendMessage..ie 用于宽字符,您是否尝试过正常的 Sendmessage..public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);

我还注意到,就好像您正试图根据 richtextbox 控件的句柄更改值,因此在另一个进程中四处寻找 AOL 的客户端窗口……是否正确?

这可能是问题的根源,直接修改属于不属于您的窗口的控件(您的程序是托管的,修改非托管进程的窗口)......这可以解释它崩溃的原因。你能解释一下十六进制常量的用途吗?

编辑:当您使用 WM_GETTEXTLENGTH 和 WM_GETTEXT 时,它们是 Windows 消息的一部分,用于从控件中检索文本长度和实际文本。如果你看here看看 pinvoke.net 对它们有什么看法。当您使用 WM_GETTEXTLENGTH 和 WM_GETTEXT 发出“SendMessage”时,您是在告诉 Windows -“嘿,给我获取我提供的关联句柄中文本的长度你在参数 n 中。我刚刚想到,值得一试......我会摆脱那些 SendMessage pinvokes 并只使用这个......

[DllImport("user32.dll", CharSet = CharSet.Auto)]static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);//If you use '[Out] StringBuilder', initialize the string builder with proper length first.
child = FindClientWindow();room = FindChildByClass(child, "RICHCNTLREADONLY");length = SendMessage(n, 0x000E, IntPtr.Zero, IntPtr.Zero);StringBuilder sbBuf = new StringBuilder(length);SendMessageByString(room, 0x000D, new IntPtr( length.ToInt32() + 1 ), out sbBuf); // this is the line that keeps crashing on me.

尝试一下然后回到这里... :)

希望对您有所帮助,此致,汤姆。

关于c# - 调用 SendMessage (P/Invoke) 一直崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2180996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com