364,033 Members | 4770 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

PInvokeStackImbalance was detected.

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


Mattias Sjögren
P: n/a
Mattias Sjögren
John,
[color=blue]
> [DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
> private static extern IntPtr OpenDesktop(string lpszDesktop, int
>dwFlags, bool fInherit, long dwDesiredAccess);[/color]
^^^^

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

Willy Denoyette [MVP]
P: n/a
Willy Denoyette [MVP]

"John Sudds" <John Sudds@discussions.microsoft.com> wrote in message
news:98CA64FC-90B5-4F16-B8F9-54C4677DB54A@microsoft.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

John Sudds
P: n/a
John Sudds

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


"John Sudds" wrote:
[color=blue]
> 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);
> }
> }
>[/color]
Jan 26 '06 #4

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

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp pinvokestackimbalance