GetMonitorInfo C# 调用问题

beatleslp 2011-06-22 04:58:26

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
static class Program
{
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int EnumDisplayMonitors(int hdc, int lprcClip, deleMonitorEnumProc lpfnEnum, int dwData);

private delegate int deleMonitorEnumProc(int hMonitor, int hdcMonitor, RECT lprcMonitor, int dwData);

[DllImport("user32.dll", EntryPoint = "GetMonitorInfoA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern bool GetMonitorInfo(int hMonitor, MONITORINFOEX lpmi);
private struct RECT
{
public int Left_Renamed;
public int Top;
public int Right_Renamed;
public int Bottom;
}
private struct MONITORINFOEX
{
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public int dwFlags;
public string szDevice;
}

public static string aa = null;

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static void test(){

EnumDisplayMonitors(0, 0, new deleMonitorEnumProc(MonitorEnumProc), 0);
}

private static int MonitorEnumProc(int hMonitor, int hdcMonitor, RECT lprcMonitor, int dwData)
{
MONITORINFOEX udtMonInf = new MONITORINFOEX();
udtMonInf.cbSize =72;

if (GetMonitorInfo(hMonitor, udtMonInf))
{

}
else
{
}

return 1;
}

}
}

执行到 if (GetMonitorInfo(hMonitor, udtMonInf))
时出System.AccessViolationException异常 请大家帮看看 结构体参数问题??
...全文
445 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
beatleslp 2011-06-22
  • 打赏
  • 举报
回复
额 弄好了 private struct MONITORINFOEX
{
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public int dwFlags;
//public string szDevice;
}

最后一个参数多余
beatleslp 2011-06-22
  • 打赏
  • 举报
回复
private static extern bool GetMonitorInfo(int hMonitor, out MONITORINFOEX lpmi);

参数改成OUT不出异常了 但是返回值为 FALSE。。。。。。额额额
beatleslp 2011-06-22
  • 打赏
  • 举报
回复
= =讨厌
三岁、就很帅 2011-06-22
  • 打赏
  • 举报
回复

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧