PInvokeStackImbalance was detected.

G

Guest

The MDA appears on the call to OpenDesktop.

I have tried a variety of approaches (including specifying the A and W
variant with ExactSpelling=true, and PreserveSig=true) but none of them seem
to work. This code was copied from a 1.1 sample that purported to work
without an error.


public static class Desktop
{
private const long DESKTOP_SWITCHDESKTOP = 0x0100L;

[DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, int
dwFlags, bool fInherit, long dwDesiredAccess);

[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);

public static bool IsLocked()
{
IntPtr hdt = OpenDesktop("Default", 0, false,
DESKTOP_SWITCHDESKTOP);

if (IntPtr.Zero == hdt)
return false;

return SwitchDesktop(hdt);
}
}
 
M

Mattias Sjögren

John,
[DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, int
dwFlags, bool fInherit, long dwDesiredAccess);
^^^^

Should be a (u)int.


Mattias
 
W

Willy Denoyette [MVP]

| The MDA appears on the call to OpenDesktop.
|
| I have tried a variety of approaches (including specifying the A and W
| variant with ExactSpelling=true, and PreserveSig=true) but none of them
seem
| to work. This code was copied from a 1.1 sample that purported to work
| without an error.
|
|
| public static class Desktop
| {
| private const long DESKTOP_SWITCHDESKTOP = 0x0100L;
|
| [DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
| private static extern IntPtr OpenDesktop(string lpszDesktop, int
| dwFlags, bool fInherit, long dwDesiredAccess);
|
| [DllImport("user32.dll")]
| private static extern bool SwitchDesktop(IntPtr hDesktop);
|
| public static bool IsLocked()
| {
| IntPtr hdt = OpenDesktop("Default", 0, false,
| DESKTOP_SWITCHDESKTOP);
|
| if (IntPtr.Zero == hdt)
| return false;
|
| return SwitchDesktop(hdt);
| }
| }
|

The last argument is not a long it should be an unsigned int.
The MDA did not exist in v1.1, so that means it's only reported now.

Willy.
 
G

Guest

THANKS to both of you! My problem was that ACCESS_MASK (the last param of
OpenDesktop) is unsigned.
 
G

Guest

For those who are interested in the final rewrite, here it is. This code is
running inside a screen saver, and accurately detects whether the Default
desktop has been locked (before, after, or during).

public static class Desktop
{
private const uint DESKTOP_READOBJECTS = 0x0001;
private const uint DESKTOP_WRITEOBJECTS = 0x0080;
private const uint DESKTOP_SWITCHDESKTOP = 0x0100;
private const uint AccessUnlocked = DESKTOP_READOBJECTS |
DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP;

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, int
dwFlags,
bool fInherit, uint dwDesiredAccess);

[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);

public static bool IsNotLocked()
{
IntPtr hdt = OpenDesktop("Default", 0, false, AccessUnlocked);

// If we can make the switch, the desktop is not locked
if (SwitchDesktop(hdt))
return true;

return false;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top