By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

PInvokeStackImbalance was detected.

P: n/a
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);
}
}

Jan 26 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 26 '06 #2

P: n/a

"John Sudds" <John Su***@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
| 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.


Jan 26 '06 #3

P: n/a

THANKS to both of you! My problem was that ACCESS_MASK (the last param of
OpenDesktop) is unsigned.
"John Sudds" wrote:
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);
}
}

Jan 26 '06 #4

P: n/a
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;
}
}

Jan 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.