请教各位out string做参数为什么会崩溃,多谢
请问各位out string做参数为什么会崩溃,谢谢
ps:****怎么这么卡,页面总是打不开,版主求解答呀
------解决思路----------------------
------解决思路----------------------
string没有必要用out
直接传就可以了,也可以用stringbuilder。
------解决思路----------------------
GetWindowText 第二个参数是存放结果空间, 必须预先分配第三个参数指定的大小,不然就覆盖到未知区域了.
public MainWindow()
{
InitializeComponent();
TestEnumWindow();
}
[DllImport("User32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd,out string text, int maxCount);// 这里加out 就崩溃的原因是?,去掉就可以
public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
public string szText;
}
public void TestEnumWindow()
{
EnumWindowsProc enumWndProc = new EnumWindowsProc(EnumWindowsProc_t);
EnumWindows(enumWndProc, 0);
}
private bool EnumWindowsProc_t(IntPtr hWnd, int lParam)
{
if (!IsWindowVisible(hWnd))
{
return true;
}
//STRINGBUFFER str = new STRINGBUFFER();
string str = "";
int n = GetWindowText(hWnd,out str, 512);
//if (n == 0)
//{
// MessageBox.Show("not get title");
// Application.Current.Shutdown();
//}
MessageBox.Show(str);
return true;
}
}
ps:****怎么这么卡,页面总是打不开,版主求解答呀
------解决思路----------------------
要这样定义
/// <summary>
/// 取窗口文本
/// </summary>
/// <param name="hWnd"></param>
/// <param name="title"></param>
/// <param name="size"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
然后这样调用
StringBuilder text = new StringBuilder(512);
int n =GetWindowText(hWnd, text, 512);
------解决思路----------------------
string没有必要用out
直接传就可以了,也可以用stringbuilder。
------解决思路----------------------
GetWindowText 第二个参数是存放结果空间, 必须预先分配第三个参数指定的大小,不然就覆盖到未知区域了.