September 11th, 2009 04:28 AM
#1
Process Problem
Hi all,I'm new to CodeGuru,I'm Romanian and I can't speech very good english.I'm using Visual C++ Professional, and CLR.I'm still learning C++.My project is a Server Manager (for HLDS).I'm trying to set "hlds.exe" process to HIGH priority,but first,I check if the process is active.Problem : My function allways return false and I can't see the mistake.
Function :
Code:
bool HLDSIsRunning()
{
unsigned long aProcesses[1024], cbNeeded, cProcesses;
if(EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded) == 0)
return false;
cProcesses = cbNeeded / sizeof(unsigned long);
for(unsigned int i = 0; i < cProcesses; i++)
{
if(aProcesses[i] == 0)
continue;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, aProcesses[i]);
TCHAR buffer[50];
GetModuleBaseName(hProcess, 0, buffer, 50);
DWORD dwExitStatus = 0;
GetExitCodeProcess(hProcess, &dwExitStatus);
CloseHandle(hProcess);
if( buffer == _T("hlds.exe") && dwExitStatus != 0 ) return true;
}
return false;
}
All Code :
Code:
#include <windows.h>
#include <Psapi.h>
#include <tchar.h>
#pragma comment(lib,"Winmm.lib")
#pragma comment(lib,"Psapi.lib")
#pragma once
HANDLE popcorn;
bool HLDSIsRunning()
{
unsigned long aProcesses[1024], cbNeeded, cProcesses;
if(EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded) == 0)
return false;
cProcesses = cbNeeded / sizeof(unsigned long);
for(unsigned int i = 0; i < cProcesses; i++)
{
if(aProcesses[i] == 0)
continue;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, aProcesses[i]);
TCHAR buffer[50];
GetModuleBaseName(hProcess, 0, buffer, 50);
DWORD dwExitStatus = 0;
GetExitCodeProcess(hProcess, &dwExitStatus);
CloseHandle(hProcess);
if( buffer == _T("hlds.exe") && dwExitStatus != 0 ) return true;
}
return false;
}
namespace SManager {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::GroupBox^ groupBox1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::GroupBox^ groupBox2;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button3;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button1 = (gcnew System::Windows::Forms::Button());
this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->groupBox1->SuspendLayout();
this->groupBox2->SuspendLayout();
this->SuspendLayout();
//
// groupBox1
//
this->groupBox1->Controls->Add(this->button2);
this->groupBox1->Controls->Add(this->button1);
this->groupBox1->Location = System::Drawing::Point(12, 12);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size = System::Drawing::Size(182, 50);
this->groupBox1->TabIndex = 0;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"Booster";
//
// button2
//
this->button2->Location = System::Drawing::Point(100, 19);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Stop";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button1
//
this->button1->Location = System::Drawing::Point(6, 19);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Start";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// groupBox2
//
this->groupBox2->Controls->Add(this->button5);
this->groupBox2->Controls->Add(this->button4);
this->groupBox2->Controls->Add(this->button3);
this->groupBox2->Location = System::Drawing::Point(12, 79);
this->groupBox2->Name = L"groupBox2";
this->groupBox2->Size = System::Drawing::Size(182, 73);
this->groupBox2->TabIndex = 1;
this->groupBox2->TabStop = false;
this->groupBox2->Text = L"Process Manager";
//
// button5
//
this->button5->Location = System::Drawing::Point(52, 45);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(75, 23);
this->button5->TabIndex = 2;
this->button5->Text = L"Normal";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
//
// button4
//
this->button4->Location = System::Drawing::Point(100, 16);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(75, 23);
this->button4->TabIndex = 1;
this->button4->Text = L"RealTime";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(6, 16);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 0;
this->button3->Text = L"High";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(207, 273);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->groupBox1);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->Name = L"Form1";
this->Text = L"SManager";
this->groupBox1->ResumeLayout(false);
this->groupBox2->ResumeLayout(false);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
timeBeginPeriod(1);
MessageBox::Show( "Booster Started !", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Information );
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
timeEndPeriod(1);
MessageBox::Show( "Booster Started !", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Information );
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
if( HLDSIsRunning() ) {
SetPriorityClass(popcorn,HIGH_PRIORITY_CLASS);
MessageBox::Show( "HLDS is now high !", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Information );
}
else MessageBox::Show( "HLDS is not active !", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Warning );
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
if( HLDSIsRunning() ) {
SetPriorityClass(popcorn,REALTIME_PRIORITY_CLASS);
MessageBox::Show( "HLDS is now RealTime !", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Information );
}
else MessageBox::Show( "HLDS is not active !", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Warning );
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
{
if( HLDSIsRunning() ) {
SetPriorityClass(popcorn,NORMAL_PRIORITY_CLASS);
MessageBox::Show( "HLDS is now Normal!", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Information );
}
else MessageBox::Show( "HLDS is not active !", "SManager",
MessageBoxButtons::OK, MessageBoxIcon::Warning );
}
};
}
Thanks .
September 11th, 2009 05:07 AM
#2
Re: Process Problem
Code:
buffer == _T("hlds.exe")
You are comparing the pointers, not the text itself.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks