473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Anyone Know How to Control The Windows by C#

Atran
319 100+
Hello Everybody.
I want to know How to Control The Windows by C# Language Programming, that I can shutdown the Windows, or restart the windows, and more more controls programmed by C#, Can Anyone show me The Code.

Atran
May 7 '07 #1
4 1411
Well I think your best bet would be the API world. You have to import external functions from a dll. For example to close a Windows application programmatically, you should import the functions

Expand|Select|Wrap|Line Numbers
  1. [DllImport("user32.dll")]
  2.         public static extern int FindWindow(
  3.         string lpClassName, // class name
  4.         string lpWindowName // window name
  5.         );
  6. [DllImport("user32.dll")]
  7.         public static extern int SendMessage(
  8.         int hWnd, // handle to destination window
  9.         uint Msg, // message
  10.         int wParam, // first message parameter
  11.         int lParam // second message parameter
  12.         );
Findwindow gets the Window's handle of the window that has the close function, and then SendMessage sends these weird cryptic messages that can be anything from WM_CLOSE to a keypress simulation.

And for capturing the startup/shutdown of windows my guess is you have to find the appropriate function that is acpi triggered.

Is this the sort of thing you are wanting to do? I myself only just learned about using API calls successfully, so I may not be the best source, but at least it gives you some google search terms.
May 7 '07 #2
Atran
319 100+
Thanks you.
May 7 '07 #3
SammyB
807 Expert 512MB
Hello Everybody.
I want to know How to Control The Windows by C# Language Programming, that I can shutdown the Windows, or restart the windows, and more more controls programmed by C#, Can Anyone show me The Code.

Atran
In .Net, you can also use Win32Shutdown. There is sample code at the very bottom of this page: http://www.craigmurphy.com/blog/?s=WMI
I'm going to try the code, but I better post first.
May 8 '07 #4
SammyB
807 Expert 512MB
That is scary code! No notices, no warnings, down-boy! Plus, the code is UGLY. I like the API solution better. You can even add the API solution as an Excel macro. I did it once, LOL!

I made several changes: straightened the quotes, qualified the enum, and commented that you must add a reference to System.Management:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Management;        // Must add a reference to System.Management in Solution Explorer
  9. namespace ShutDown
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public enum ShutDown
  14.         {
  15.             LogOff = 0,
  16.             Shutdown = 1,
  17.             Reboot = 2,
  18.             ForcedLogOff = 4,
  19.             ForcedShutdown = 5,
  20.             ForcedReboot = 6,
  21.             PowerOff = 8,
  22.             ForcedPowerOff = 12
  23.         }
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.         private void button1_Click(object sender, EventArgs e)
  29.         {
  30.             ManagementClass W32_OS = new ManagementClass("Win32_OperatingSystem");
  31.             ManagementBaseObject inParams, outParams;
  32.             int result;
  33.             W32_OS.Scope.Options.EnablePrivileges = true;
  34.             foreach (ManagementObject obj in W32_OS.GetInstances())
  35.             {
  36.                 inParams = obj.GetMethodParameters("Win32Shutdown");
  37.                 inParams["Flags"] = ShutDown.ForcedShutdown;
  38.                 inParams["Reserved"] = 0;
  39.                 outParams = obj.InvokeMethod("Win32Shutdown", inParams, null);
  40.                 result = Convert.ToInt32(outParams["returnValue"]);
  41.                 if (result != 0) throw new Win32Exception(result);
  42.             }
  43.         }
  44.     }
  45. }
May 8 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Skip Montanaro | last post by:
I wrote PEP 304, "Controlling Generation of Bytecode Files": http://www.python.org/peps/pep-0304.html quite awhile ago. The first version appeared in January 2003 in response to questions...
2
by: Programatix | last post by:
Hi, I'm working on a project which includes WebServices and Windows Form application. The Windows Form application will call the WebServices to retrieve data from database. The data will be...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
4
by: Ersin Gençtürk | last post by:
Server : WEB System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown. ---> System.Web.HttpException: The View State is invalid for this page and might be...
1
by: Flack | last post by:
Hey guys, Here is whats happening. I have a StringBuilder, a TextBox, and a TabControl with one TabPage. On my main form, I created and displayed a fairly big maze. While the app is solving...
2
by: progrock | last post by:
Hey All, I'm attempting to put together a "secure" ASP.NET 2.0 application with one requirement that has given me a bit of grief: In a nutshell, if the user's session expires then they should be...
13
by: Snis Pilbor | last post by:
Hello, Here is an idea I've been toying with to speed up programs but still keep them portable. It's just a very handwavey rough description right now since I haven't worked out details. The...
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.