Anyone Know How to Control The Windows by C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atran
    Contributor
    • May 2007
    • 319

    #1

    Anyone Know How to Control The Windows by C#

    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
  • NVergunst
    New Member
    • Apr 2007
    • 18

    #2
    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 programmaticall y, you should import the functions

    Code:
    [DllImport("user32.dll")]
            public static extern int FindWindow(
            string lpClassName, // class name
            string lpWindowName // window name
            );
    [DllImport("user32.dll")]
            public static extern int SendMessage(
            int hWnd, // handle to destination window
            uint Msg, // message
            int wParam, // first message parameter
            int lParam // second message parameter
            );
    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.

    Comment

    • Atran
      Contributor
      • May 2007
      • 319

      #3
      Thanks you.

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        Originally posted by Atran
        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.

        Comment

        • SammyB
          Recognized Expert Contributor
          • Mar 2007
          • 807

          #5
          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.Manageme nt:
          Code:
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Text;
          using System.Windows.Forms;
          using System.Management;		// Must add a reference to System.Management in Solution Explorer
          namespace ShutDown
          {
          	public partial class Form1 : Form
          	{
          		public enum ShutDown
          		{
          			LogOff = 0,
          			Shutdown = 1,
          			Reboot = 2,
          			ForcedLogOff = 4,
          			ForcedShutdown = 5,
          			ForcedReboot = 6,
          			PowerOff = 8,
          			ForcedPowerOff = 12
          		}
          		public Form1()
          		{
          			InitializeComponent();
          		}
          		private void button1_Click(object sender, EventArgs e)
          		{
          			ManagementClass W32_OS = new ManagementClass("Win32_OperatingSystem");
          			ManagementBaseObject inParams, outParams;
          			int result;
          			W32_OS.Scope.Options.EnablePrivileges = true;
          			foreach (ManagementObject obj in W32_OS.GetInstances())
          			{
          				inParams = obj.GetMethodParameters("Win32Shutdown");
          				inParams["Flags"] = ShutDown.ForcedShutdown;
          				inParams["Reserved"] = 0;
          				outParams = obj.InvokeMethod("Win32Shutdown", inParams, null);
          				result = Convert.ToInt32(outParams["returnValue"]);
          				if (result != 0) throw new Win32Exception(result);
          			}
          		}
          	}
          }

          Comment

          Working...