472,330 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

Problems with GetMonitorInfoEx Structure

I'm having problems trying to call GetMonitorInfo. I believe the problem has
something to do with the szDeviceName. Passing a MonitorInfo struct works fine. I
added the szDeviceName and the struct is filled with garbage. What am I doing
wrong??

Thanks - JackRazz
Pretty version of this snippet is here: http://rafb.net/paste/results/M3114698.html

private const int CCHDEVICENAME = 32;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class MonitorInfoEx {
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
public string szDeviceName;
}
[DllImport("user32")]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out]
MonitorInfoEx lpmi );
MonitorInfoEx mi = new MonitorInfoEx();
mi.cbSize =(uint) Marshal.SizeOf(mi);

bool result = GetMonitorInfo( hMonitor, mi ) ;
if ( result ) {
Mon.MonitorArea= mi.rcMonitor;
Mon.WorkArea= mi.rcWork;
Mon.Flags = (int) mi.dwFlags;
Mon.hMonitor = hMonitor;
//Mon.deviceName=mi.szDeviceName;
}
else {
Console.WriteLine("Call to GetMonitorInfo failed");
}
Nov 15 '05 #1
5 6497
[DllImport("user32")]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out]
MonitorInfoEx lpmi );


Add CharSet=CharSet.Auto to the DllImport attribute.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
> Add CharSet=CharSet.Auto to the DllImport attribute.
Mattias,
First, thanks for the reply. It didn't work. I worked on it last night to capture
the LastError, which is 127 - 'The parameter is incorrect.' Any other suggestions.
I've updated the listing to show your suggested change with the full callback
procedure.

-------------------------------------------------------------------------------------
----
Pretty print version here --> http://rafb.net/paste/results/p3091664.html
//I'm having problems trying to call GetMonitorInfo. I believe the problem has
//something to do with szDeviceName. Passing a MonitorInfo struct works fine. I
//added the szDeviceName and the struct is filled with garbage.

[DllImport("user32", CharSet = CharSet.Auto, SetLastError=true)]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out] MonitorInfoEx
lpmi );

private const int CCHDEVICENAME = 32;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class MonitorInfoEx {
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
public string szDeviceName;
}
public bool MonitorEnumCallback( IntPtr hMonitor, IntPtr hDC,ref RECT lprcMonitor,
object dwData ) {

CMonitor Mon = new CMonitor();
MonitorInfoEx mi = new MonitorInfoEx();
mi.cbSize =(uint) Marshal.SizeOf(mi);

bool result = GetMonitorInfo( hMonitor, mi ) ;
if ( result ) {
Mon.MonitorArea= mi.rcMonitor;
Mon.WorkArea= mi.rcWork;
Mon.Flags = (int) mi.dwFlags;
Mon.hMonitor = hMonitor;
//Mon.deviceName=mi.szDeviceName;
}
else {
//throw new Win32Exception(Marshal.GetLastWin32Error());
Win32Exception myEx = new Win32Exception(Marshal.GetLastWin32Error());
Console.WriteLine(myEx.ErrorCode);
Console.WriteLine(myEx.Message);
}

mMonitors.Add(Mon);
return result;
}
Nov 15 '05 #3
How does RECT looks like?
It's always good to post complete reproducible code snippets.

Willy.

"JackRazz" <Ja******@NotValid.com> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
Add CharSet=CharSet.Auto to the DllImport attribute.

Mattias,
First, thanks for the reply. It didn't work. I worked on it last night

to capture the LastError, which is 127 - 'The parameter is incorrect.' Any other suggestions. I've updated the listing to show your suggested change with the full callback procedure.

Nov 15 '05 #4
GetMonitorInfo take a handle to a monitor as first argument not a DC handle.
So you nee to call MonitorFromWindow to get this handle.

[DllImport("User32")]
public static extern IntPtr MonitorFromWindow( IntPtr hWnd, int dwFlags );

const int MONITOR_DEFAULTTOPRIMARY = 1;
.....
result = GetMonitorInfoEx( MonitorFromWindow( IntPtr.Zero,
MONITOR_DEFAULTTOPRIMARY) , ref mi );
if ( result ) {
.....

Willy.
"JackRazz" <Ja******@NotValid.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
How does RECT looks like? The RECT members are int's.
It's always good to post complete reproducible code snippets.


Agreed, thanks for offering to look at it. I reduced it to a simple

CMonitorInfo class that does everything when an instance is created in Form_Load. I have a break set in the callback, just load and run.

Just to reiterate, I'm having problems trying to call GetMonitorInfo. I believe the problem has something to do with szDeviceName. Passing a MonitorInfo struct works, MonitorInfoEx doesn't.

JackRazz
MS reference on MonitorInfoEx:
http://msdn.microsoft.com/library/de...nitor_81v6.asp
Latest Pretty Print version of the CMonitorInfo class included in the class http://rafb.net/paste/results/r3091643.html

Nov 15 '05 #5
Forgot the most important, change MonitorInfoEx and RECT into a struct i.s.o
a class....

Here is a working console sample.

using System;
using System.Runtime.InteropServices;

class Tester {
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("User32")]
public static extern IntPtr MonitorFromWindow( IntPtr hWnd, int dwFlags );

[DllImport("user32",EntryPoint="GetMonitorInfo", CharSet = CharSet.Auto,
SetLastError=true)]
internal static extern bool GetMonitorInfoEx( IntPtr hMonitor, ref
MonitorInfoEx lpmi );

private const int CCHDEVICENAME = 32;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct MonitorInfoEx {
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
public string szDeviceName;
}

static void Main()
{
const int MONITOR_DEFAULTTOPRIMARY = 1;
bool result;
MonitorInfoEx mi = new MonitorInfoEx();
mi.cbSize = Marshal.SizeOf(mi);
result = GetMonitorInfoEx( MonitorFromWindow(
IntPtr.Zero,MONITOR_DEFAULTTOPRIMARY) , ref mi );
if ( result ) {
Console.WriteLine(mi.rcMonitor.left);
Console.WriteLine(mi.rcMonitor.right);
Console.WriteLine(mi.dwFlags.ToString());
Console.WriteLine(mi.szDeviceName);
}
else {
Console.WriteLine(Marshal.GetLastWin32Error());

}
}
}

Willy.

"JackRazz" <Ja******@NotValid.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
How does RECT looks like? The RECT members are int's.
It's always good to post complete reproducible code snippets.


Agreed, thanks for offering to look at it. I reduced it to a simple

CMonitorInfo class that does everything when an instance is created in Form_Load. I have a break set in the callback, just load and run.

Just to reiterate, I'm having problems trying to call GetMonitorInfo. I believe the problem has something to do with szDeviceName. Passing a MonitorInfo struct works, MonitorInfoEx doesn't.

JackRazz
MS reference on MonitorInfoEx:
http://msdn.microsoft.com/library/de...nitor_81v6.asp
Latest Pretty Print version of the CMonitorInfo class included in the class http://rafb.net/paste/results/r3091643.html

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would...
2
by: BigC | last post by:
Hi, I am having problems converting from a Structure to a ByteArray. The main problem being that the structure contains a variable length array,...
1
by: Falko Wagner | last post by:
Hi there, I am currently translating a VB 6.0 application to .NET and have the following problem: The data structure I need to pass to a DLL...
2
by: Jack Fox | last post by:
We are encountering a couple of problems with our ASP.NET / IIS 6.0 applications: In each of 3 production environments we maintain a Windows...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory...
0
by: MikeCS | last post by:
Hi all I would like some help with this issue. I am new to VB 2005 (OK with VB6) My problem is that I cannot seem to return a structure from a...
8
webroten
by: webroten | last post by:
I've been working through trying to access a C DLL from VB.NET. I've read many online postings, but I'm still having problems. Now, my error is the...
3
by: =?ISO-8859-1?Q?Jo=E3o_Maia?= | last post by:
Hi there, I am having a weird problem in trying to use a ASP.NET menu inside a custom web part. I am developing a custom web part that has a menu...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.