????? ??????? Visual C++ ????? ????? ???

?????? ????????? ?????? ?????????
???? ???????: 307395 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ????? ????? ????? ??? ?? ???? ??????? ????? ??. ???? ??? ??????? ????? ????? ????? ???? ???? ?? ????? ???? ??? "???????" ? ??? ??? ???? ?? ???? ??????? ??? ???????.

???? ?????? ???????

??? ????? ??????? ??????. ???? ??? ??????? ??? ????????? ?? ?????? ?????? ? ???????? ???? ?? ????? ???? ??? ?? ??????.

????? ????? ????? ?? ???? ???????? ???????? ????? ????? ??????? GetProcessByName. ?? ??? ????????? ???????? "procName" ?? ????? ????? ?????? ???????? ??? ???????:
private:
   Process *processes[];
   static String *procName = S"Notepad";

?????? ??? ????? ?????? ???????

??? ??????? ????????? ???????? ??????? ?????? ????? BuildList ?? ??? ??? ????? ???? ???? ?? ???????. ???? ??? ?????? ?????? ??????? ?? ???? ???? ListView. ??? ????? ??? ?? ??? ??????? ?? ?????? ?????? ???? ???? ????? ???? ???? ???? ListView.

????? ?????? ????? ?? ??? ???????? ???????? ?? ??????? ??????? GetProcessByName ????? ???????. ??? ??????? ?????? ???? ?????? ???? ??????? ???? ???? ?? ???? iterated ?? ???? ??????? ???? For.
ListViewItem *itemAdd ; 
ListView1->Items->Clear();
processes = Process::GetProcessesByName(procName);
for (int i = 0; i < processes->Length; i++)
{
   itemAdd = ListView1->Items->Add(processes[i]->MainWindowTitle);
   itemAdd->SubItems->Add(processes[i]->Id.ToString());
}

????? ???? ???? ?? ?????

????? ???? ???? ?????? ?????? ?? ??? ????????? ??? ???????, ??? ??? ???? ?? ????? ???? ???? ??? ?? ??????? ??? ??? ????????. ?????? ??? ?????? ????? ???? ??????? ??? ????????. ??? ????? ???? ???????? MainWindowTitle ?? ???? ???? ListView.

???? ??? ???????? ???????? ??? ?????? ?????? ?????? ?? ???? ???? ListView ???? ??? ???? ??? ??????? ?????? ??????? ??????? GetProcessById ??? ?????? ?? ?? ????? ??? ??????? ?????? ??????? ??????? CloseMainWindow. ?????? ?? ???? ????? ??????? ??? ?? ?? ????? ??????? ??????? ??? ????? WaitForExit ??? ?????? ???? ???. ????
try
{         
   ListViewItem *lvi = ListView1->SelectedItems->get_Item(0);
   ListViewItem::ListViewSubItem *lvsi = lvi->SubItems->get_Item(1);
   int procID = System::Convert::ToInt32(lvsi->Text);
   Process *tempProc=Process::GetProcessById(procID);
   tempProc->CloseMainWindow();
   tempProc->WaitForExit();
   BuildList();
}
catch(...)
{
   MessageBox::Show(S"Select a process in the ListView.");
}
				

?? ?????? ???? ?????? ?????

?????? ???? ?????? ????? ???? ???? ?????? ????? ??????? GetProcessByName ??? ?? ??????? ??????? CloseMainWindow ??? ?? ???? ???????. ????
try
{
   for (int i = 0; i < processes->Length; i++)
   {
      processes[i]->CloseMainWindow();
   }

   ListView1->Items->Clear();
}
catch (System::NullReferenceException *)
{
   MessageBox::Show("No instances of Notepad running.");
}
				

???? ???? ?????

  1. ????? "????? C++ ?????" ????? ?? Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005. ?????? ????? ???? ??????? ???????:
    1. ??? ????? Visual Studio .NET ?? Visual Studio 2005.
    2. ?? ??????? ???? ??? ??? ???? ?? ???? ??? ???????.
    3. ?? ???? ?????? ????? ???? ? ???? ??? ?????? Visual C++ ??? ????? Project ??? ?? ???? ??? ????? C++ ????? ?? Visual Studio .NET 2002 ???? ??? ????? ???? ?????? (.NET) ?? Visual Studio .NET 2003 ?? ???? ??? ????? ???? ?????? CLR ?? Visual Studio 2005 ??? ?????.
    4. ???? ??? ??? ?? ??? ????.
  2. ?? "?????? ??????" ??? ??? .cpp ???????.
  3. Replace the code in the Code window with the following:
    #include "stdafx.h"
    #include <windows.h>
    #ifdef MessageBox
    #undef MessageBox
    #endif
    
    #using <mscorlib.dll>
    #using "System.Dll"
    #using "System.Windows.Forms.Dll"
    #using "System.Drawing.Dll"
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Collections;
    using namespace System::ComponentModel;
    using namespace System::Windows::Forms;
    using namespace System::Diagnostics;
    
    namespace CloseApplication_mc
    {
       public __gc class Form1 : public Form
       {
       protected:
          Button *btnCloseAll;
          ColumnHeader *ColumnHeader2;
          ColumnHeader *ColumnHeader1;
          Button *btnLaunch1;
          ListView *ListView1;
          Button *btnClose1;
    
       private:
          Process *processes[];
          static String *procName = S"Notepad";
          static String *specialFolder = Environment::GetFolderPath(Environment::SpecialFolder::System);
    
       public:
          Form1()
          {
             InitForm();
          }
    
       protected:
          void Dispose( bool disposing )
          {
             Form::Dispose(disposing);
          }
    
       private:
          void InitForm()
          {
             btnClose1 = new Button();
             ColumnHeader2 = new ColumnHeader();
             ColumnHeader1 = new ColumnHeader();
             btnCloseAll = new Button();
             btnLaunch1 = new Button();
             ListView1 = new ListView();
             SuspendLayout();
    
             // 
             // btnClose1
             // 
             btnClose1->Location = Point(160, 176);
             btnClose1->Name = S"btnClose1";
             btnClose1->Size = System::Drawing::Size(112, 32);
             btnClose1->TabIndex = 4;
             btnClose1->Text = S"Close Selected Process";
             btnClose1->Click += new System::EventHandler(this, btnClose1_Click);
             Controls->Add(btnClose1);
    
             // 
             // btnCloseAll
             // 
             btnCloseAll->Location = Point(160, 216);
             btnCloseAll->Name = S"btnCloseAll";
             btnCloseAll->Size = System::Drawing::Size(112, 32);
             btnCloseAll->TabIndex = 3;
             btnCloseAll->Text = S"Close All Processes";
             btnCloseAll->Click += new System::EventHandler(this, btnCloseAll_Click);
             Controls->Add(btnCloseAll);
    
             // 
             // btnLaunch1
             // 
             btnLaunch1->Location = Point(32, 176);
             btnLaunch1->Name = S"btnLaunch1";
             btnLaunch1->Size = System::Drawing::Size(112, 72);
             btnLaunch1->TabIndex = 1;
             btnLaunch1->Text = S"Start Notepad";
             btnLaunch1->Click += new System::EventHandler(this, btnLaunch1_Click);
             Controls->Add(btnLaunch1);
    
             // 
             // ColumnHeader2
             // 
             ColumnHeader2->Text = S"Process ID";
             ColumnHeader2->Width = 85;
    
             // 
             // ColumnHeader1
             // 
             ColumnHeader1->Text = S"Window Title";
             ColumnHeader1->Width = 160;
    
             // 
             // ListView1
             // 
             ListView1->Columns->Add(ColumnHeader1);
             ListView1->Columns->Add(ColumnHeader2);
             ListView1->Location = Point(22, 8);
             ListView1->MultiSelect = false;
             ListView1->Name = S"ListView1";
             ListView1->Size = System::Drawing::Size(250, 152);
             ListView1->TabIndex = 7;
             ListView1->View = View::Details;
             Controls->Add(ListView1);
    
             // 
             // Form1
             // 
             AutoScaleBaseSize = System::Drawing::Size(5, 13);
             ClientSize = System::Drawing::Size(292, 266);
             Name = S"Form1";
             Text = S"Process Example";
             Closing += new System::ComponentModel::CancelEventHandler(this, closing);
             Load += new System::EventHandler(this, Form1_Load);
             ResumeLayout(false);
          }
    
          void BuildList()
          {
             ListViewItem *itemAdd ; 
             ListView1->Items->Clear();
             processes = Process::GetProcessesByName(procName);
             for (int i = 0; i < processes->Length; i++)
             {
                itemAdd = ListView1->Items->Add(processes[i]->MainWindowTitle);
                itemAdd->SubItems->Add(processes[i]->Id.ToString());
             }
          }
    
          void btnLaunch1_Click(Object *sender, EventArgs *e)
          {
             ProcessStartInfo *p = new ProcessStartInfo();
             p->FileName = String::Concat(specialFolder, S"\\eula.txt");
             p->WindowStyle = ProcessWindowStyle::Minimized ;
             Process *proc = Process::Start(p);
             proc->WaitForInputIdle();
             BuildList();
          }
    
          void Form1_Load(Object *sender, EventArgs *e)
          {
          }
    
          void btnClose1_Click(Object *sender, EventArgs *e)
          {
             try
             {         
                ListViewItem *lvi = ListView1->SelectedItems->get_Item(0);
                ListViewItem::ListViewSubItem *lvsi = lvi->SubItems->get_Item(1);
                int procID = System::Convert::ToInt32(lvsi->Text);
                Process *tempProc=Process::GetProcessById(procID);
                tempProc->CloseMainWindow();
                tempProc->WaitForExit();
                BuildList();
             }
             catch(...)
             {
                MessageBox::Show(S"Select a process in the ListView before clicking this button.");
             }
          }
    
          void btnCloseAll_Click(Object *sender, EventArgs *e)
          {
             if (processes != 0)
             {
                for (int i = 0; i < processes->Length; i++)
                {
                   processes[i]->CloseMainWindow();
                }
           
                ListView1->Items->Clear();
             }
          }
    
          void closing(Object *sender, System::ComponentModel::CancelEventArgs *e)
          {
             btnCloseAll_Click(this,e);
          }
       };
    } 
    
    using namespace CloseApplication_mc;
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
       Form1 *FormInstance = new Form1;
       Application::Run(FormInstance);
    
       return 0;
    }
    					
  4. ????? ?? ?? ?????? ???????.
  5. ???? ??? ???? ??????? ??? ????? ?? ????.
  6. ???? ??? ???? ??????? ?? ?????? ListView ?? ???? ??? ????? ?????. ??? ????? ?????? ?????? ??????? ???? ??????. ????? ????? ????? ??? ????? ???? ???????? ?????? ???? ???????? ?? ??????? ??? ???????.
?????? ?????? ??? ??????? ????? ???? ??? ?????? ??????? ??? ?????? ???????. ????? ??????? ???? ?? ??? ??? ?????? ??? ????? ???? ?????? ?????. ????? ??????? ??????? WindowHandle ?????? ????? ?????? ??? ??? ???????. ??? ????? ?? ??? ????? ??????? ????? ???? ??? ??? ?????? ???? ??????. ??? ?? ??? ???? ????? ??????? ?? ??? ??? ???? ???? ?????? ??????? ??????? ? ????? ????? ??????? ??????? MainWindowTitle ???????? ?? ?????? ??? ?????? ??????. ??? ???? ???? ?? ???? ?? ???? ??????? MainWindowTitle ???? ? ?? ???? ??? ??????? ??? ???? ??????.

?????

????? ?? ?????????? ???? ?????? "?????? ???" ??? ???? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsProcessClassTopic.asp

???????

???? ???????: 307395 - ????? ??? ??????: 04/?? ?????/1426 - ??????: 3.0
????? ???
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
????? ??????: 
kbmt kbhowtomaster kbhowto KB307395 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????307395

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com