Can i shut windows down from my C# application?

  • Thread starter Thread starter Bredal Jensen
  • Start date Start date
Bredal,

The framework doesn't expose anything like this, but you can shut down
windows by making a call to the ExitWindows or ExitWindowsEx API functions
through the P/Invoke layer.

The declaration for ExitWindows is as follows:

[DllImport("user32.dll", SetLastError=true)]
public static extern bool ExitWindows(
[MarshalAs(UnmanagedType.U4)]
int dwReserved,
[MarshalAs(UnmanagedType.U4)]
int uReserved);

Hope this helps.
 
great , but i do i import "mscorlib.dll"?

i wonder why this :#using <mscorlib.dll>

bothers my compiler?
 
Bredal,

#using <mscorlib.dll> doesn't so anything for you. Basically, that's a
namespace declaration, and there is no namespace named "mscorlib.dll". In
order to set a reference, you have to right click on the references folder
and then select "add reference". By default though, mscorlib is added as a
reference.
 
so why i'm getting the following error?


...\Form1.cs(23): The type or namespace name 'DllImport' could not be found
(are you missing a using directive or an assembly reference?)

Nicholas Paldino said:
Bredal,

#using <mscorlib.dll> doesn't so anything for you. Basically, that's a
namespace declaration, and there is no namespace named "mscorlib.dll". In
order to set a reference, you have to right click on the references folder
and then select "add reference". By default though, mscorlib is added as a
reference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Bredal Jensen said:
great , but i do i import "mscorlib.dll"?

i wonder why this :#using <mscorlib.dll>

bothers my compiler?
 
Bredal,

You need to place the following at the top of the file:

#using System.Runtime.InteropServices;


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bredal Jensen said:
so why i'm getting the following error?


..\Form1.cs(23): The type or namespace name 'DllImport' could not be found
(are you missing a using directive or an assembly reference?)

in
message news:[email protected]...
Bredal,

#using <mscorlib.dll> doesn't so anything for you. Basically, that's a
namespace declaration, and there is no namespace named "mscorlib.dll".
In
order to set a reference, you have to right click on the references
folder
and then select "add reference". By default though, mscorlib is added as a
reference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Bredal Jensen said:
great , but i do i import "mscorlib.dll"?

i wonder why this :#using <mscorlib.dll>

bothers my compiler?




I'm thinking of soething equivalent to "exitwindows"
 
Bredal,

Sorry, that is:

using System.Runtime.InteropServices;

(no sharp symbol).



--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bredal Jensen said:
so why i'm getting the following error?


..\Form1.cs(23): The type or namespace name 'DllImport' could not be found
(are you missing a using directive or an assembly reference?)

in
message news:[email protected]...
Bredal,

#using <mscorlib.dll> doesn't so anything for you. Basically, that's a
namespace declaration, and there is no namespace named "mscorlib.dll".
In
order to set a reference, you have to right click on the references
folder
and then select "add reference". By default though, mscorlib is added as a
reference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Bredal Jensen said:
great , but i do i import "mscorlib.dll"?

i wonder why this :#using <mscorlib.dll>

bothers my compiler?




I'm thinking of soething equivalent to "exitwindows"
 
Bredal Jensen said:
so why i'm getting the following error?


..\Form1.cs(23): The type or namespace name 'DllImport' could not be found
(are you missing a using directive or an assembly reference?)

Because the DllImportAttribute type is in the
System.Runtime.InteropServices namespace, and I suspect you haven't got

using System.Runtime.InteropServices;

in your list of using directives.
Nicholas Paldino said:
Bredal,

#using <mscorlib.dll> doesn't so anything for you. Basically, that's a
namespace declaration, and there is no namespace named "mscorlib.dll". In
order to set a reference, you have to right click on the references folder
and then select "add reference". By default though, mscorlib is added as a
reference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Bredal Jensen said:
great , but i do i import "mscorlib.dll"?

i wonder why this :#using <mscorlib.dll>

bothers my compiler?




I'm thinking of soething equivalent to "exitwindows"
 
Great, it compiles fine.



Thank you so much..




Nicholas Paldino said:
Bredal,

Sorry, that is:

using System.Runtime.InteropServices;

(no sharp symbol).



--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bredal Jensen said:
so why i'm getting the following error?


..\Form1.cs(23): The type or namespace name 'DllImport' could not be found
(are you missing a using directive or an assembly reference?)

in
message news:[email protected]...
Bredal,

#using <mscorlib.dll> doesn't so anything for you. Basically,
that's
a
namespace declaration, and there is no namespace named "mscorlib.dll".
In
order to set a reference, you have to right click on the references
folder
and then select "add reference". By default though, mscorlib is added
as
a
reference.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


great , but i do i import "mscorlib.dll"?

i wonder why this :#using <mscorlib.dll>

bothers my compiler?




I'm thinking of soething equivalent to "exitwindows"
 
The framework exposes this through the System.Management classes and WMI's
class Win32_OperatingSystem.
In general using this is the prefered way to shutdown a system on systems
running W2K or higher.

Willy.

Nicholas Paldino said:
Bredal,

The framework doesn't expose anything like this, but you can shut down
windows by making a call to the ExitWindows or ExitWindowsEx API functions
through the P/Invoke layer.

The declaration for ExitWindows is as follows:

[DllImport("user32.dll", SetLastError=true)]
public static extern bool ExitWindows(
[MarshalAs(UnmanagedType.U4)]
int dwReserved,
[MarshalAs(UnmanagedType.U4)]
int uReserved);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Bredal Jensen said:
I'm thinking of soething equivalent to "exitwindows"
 
Great great ...

Thank you...


Willy Denoyette said:
The framework exposes this through the System.Management classes and WMI's
class Win32_OperatingSystem.
In general using this is the prefered way to shutdown a system on systems
running W2K or higher.

Willy.

message news:[email protected]...
Bredal,

The framework doesn't expose anything like this, but you can shut down
windows by making a call to the ExitWindows or ExitWindowsEx API functions
through the P/Invoke layer.

The declaration for ExitWindows is as follows:

[DllImport("user32.dll", SetLastError=true)]
public static extern bool ExitWindows(
[MarshalAs(UnmanagedType.U4)]
int dwReserved,
[MarshalAs(UnmanagedType.U4)]
int uReserved);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Bredal Jensen said:
I'm thinking of soething equivalent to "exitwindows"
 
I was not able to locate

the System.Management namespace










Willy Denoyette said:
The framework exposes this through the System.Management classes and WMI's
class Win32_OperatingSystem.
In general using this is the prefered way to shutdown a system on systems
running W2K or higher.

Willy.

message news:[email protected]...
Bredal,

The framework doesn't expose anything like this, but you can shut down
windows by making a call to the ExitWindows or ExitWindowsEx API functions
through the P/Invoke layer.

The declaration for ExitWindows is as follows:

[DllImport("user32.dll", SetLastError=true)]
public static extern bool ExitWindows(
[MarshalAs(UnmanagedType.U4)]
int dwReserved,
[MarshalAs(UnmanagedType.U4)]
int uReserved);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Bredal Jensen said:
I'm thinking of soething equivalent to "exitwindows"
 
Back
Top