如何用c++远程获取进程信息

xiaokang007 2012-04-10 03:17:07
需求如下:
客户端电脑:C1,C2,C3(OS:Windows2000,Windows2003);
服务器端Server(OS:Windows2003)。

C1、C2、C3上运行PP进程的不同版本,客户端PP进程的自动更新功能被人为关闭,PP进程会向Server传输数据。要求在Server上实现一个检测程序,来监控客户端PP程序,如果不是最新版本,则发送Close消息给客户端的PP进程。

大概思路:
1.获取客户端PP进程版本信息
2.与Server最新版本信息比对
3.发送Close消息给客户端PP进程


以下是MSDN上远程获取进程信息的参考代码,不知道还有无其他可行的方法来实现,请大家帮忙,谢谢
VC
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{

// Get the current process.
Process^ currentProcess = Process::GetCurrentProcess();

// Get all instances of Notepad running on the local
// computer.
array<Process^>^localByName = Process::GetProcessesByName( "notepad" );

// Get all instances of Notepad running on the specific
// computer.
// 1. Using the computer alias (do not precede with "\\").
array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );

// 2. Using an IP address to specify the machineName parameter.
array<Process^>^ipByName = Process::GetProcessesByName( "notepad", "169.0.0.0" );

// Get all processes running on the local computer.
array<Process^>^localAll = Process::GetProcesses();

// Get all processes running on the remote computer.
array<Process^>^remoteAll = Process::GetProcesses( "myComputer" );

// Get a process on the local computer, using the process id.
Process^ localById = Process::GetProcessById( 1234 );

// Get a process on a remote computer, using the process id.
Process^ remoteById = Process::GetProcessById( 2345, "myComputer" );
}

看不懂Process^的用法,求解,谢谢


C#参考代码
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{



void BindToRunningProcesses()
{
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();


// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");


// Get all instances of Notepad running on the specifiec
// computer.
// 1. Using the computer alias (do not precede with "\\").
Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

// 2. Using an IP address to specify the machineName parameter.
Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");


// Get all processes running on the local computer.
Process [] localAll = Process.GetProcesses();


// Get all processes running on the remote computer.
Process [] remoteAll = Process.GetProcesses("myComputer");


// Get a process on the local computer, using the process id.
Process localById = Process.GetProcessById(1234);


// Get a process on a remote computer, using the process id.
Process remoteById = Process.GetProcessById(2345, "myComputer");

}



static void Main()
{

MyProcess myProcess = new MyProcess();


myProcess.BindToRunningProcesses();

}
}
}

C#代码GetProcessesByName在远程操作时会报异常,需要.net环境支持。
...全文
274 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tongzhipeng5699 2012-08-12
  • 打赏
  • 举报
回复
WMI 可以查询远程机器进程信息,下面有个WMI的用法示例,希望对你有帮助
http://www.codeproject.com/Articles/10539/Making-WMI-Queries-In-C
qing2005cheng 2012-08-12
  • 打赏
  • 举报
回复
// processname.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <Psapi.h>
#pragma comment(lib,"Psapi.lib")
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char buff[300];
HMODULE module;
while(true)
{
HWND m_Hwnd = ::GetForegroundWindow();
DWORD my;
DWORD need;
::GetWindowThreadProcessId(m_Hwnd,&my);
HANDLE hand = ::OpenProcess(PROCESS_ALL_ACCESS,false,my);
if(::EnumProcessModules(hand,&module,sizeof(module),&need))
{
::GetModuleBaseName(hand,module,buff,sizeof(buff));
}
//GetWindowModuleFileName(m_Hwnd,buff,300);
//::GetWindowModuleFileNameW(
cout << buff<<endl;
Sleep(3000);
}
return 0;
}
xiaokang007 2012-04-11
  • 打赏
  • 举报
回复
是我的思路有問題么,這樣根本行不通?
看到網絡上的更新都存在客戶端和服務端兩個程式來實現的

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧