C语言 进程间的通信

Meet_sw 2018-12-17 04:24:03
[img=https://img-bbs.csdn.net/upload/201812/17/1545034681_4281.jpg]
如何在缓存区中将任务管理器里面的进程内容读另一个程序中的表格?
void searchproc()//检索进程
{

REQ_PROCLIST * psearch = (REQ_PROCLIST *)pBuf;
int id = psearch->id;

ACK_PROCLIST * psearchack = (ACK_PROCLIST *)pBuf;
psearchack->id = ACKPROCLIST;


char szProcessName[40];
char szProcessPID[20];
char szThreadNum[20];
char szProcessPath[256];
char szSystemPath[256]; //不知道怎么回事,系统进程取不到全路径
char szWindowPath[256];
int nItem = 0; // 项计数

MODULEENTRY32 Mod32;
HANDLE hSnapshot;
PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf("CreateToolhelp32Snapshot error");
return;
}
if(Process32First(hProcessSnap, &pe32))
{
do
{
ZeroMemory(szProcessName,sizeof(szProcessName)/sizeof(CHAR));
ZeroMemory(szProcessPID,sizeof(szProcessPID)/sizeof(CHAR));
ZeroMemory(szThreadNum,sizeof(szThreadNum)/sizeof(CHAR));
ZeroMemory(szProcessPath,sizeof(szProcessPath)/sizeof(CHAR));
ZeroMemory(szWindowPath,sizeof(szWindowPath)/sizeof(CHAR));
ZeroMemory(szSystemPath,sizeof(szSystemPath)/sizeof(CHAR));

sprintf(szProcessName,"%s",pe32.szExeFile);
sprintf(szProcessPID, "%i", pe32.th32ProcessID);
sprintf(szThreadNum,"%i",pe32.cntThreads);


hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE,pe32.th32ProcessID );
Module32First( hSnapshot,&Mod32 );

if (NULL == strrchr(Mod32.szExePath,'\\'))
{
if (0 == lstrcmp(Mod32.szExePath,"System"))
{
sprintf(szProcessPath,"%s\\%s",szWindowPath,Mod32.szExePath);
}
else
{
sprintf(szProcessPath,"%s\\%s",szSystemPath,Mod32.szExePath);
}
}
else
{
sprintf(szProcessPath,"%s",Mod32.szExePath);
}


printf("no: %d szProcessName : %s %s %s %s\n",nItem,szProcessName,szProcessPID,szThreadNum,szProcessPath);
strcpy(psearchack->proclist[nItem].szProcessName,szProcessName);
strcpy(psearchack->proclist[nItem].szProcessPID,szProcessPID);
strcpy(psearchack->proclist[nItem].szThreadNum,szThreadNum);
strcpy(psearchack->proclist[nItem].szProcessPath,szProcessPath);

nItem++;
}
while(Process32Next(hProcessSnap, &pe32));
}
CloseHandle(hProcessSnap);
}



void dokillproc()
{
REQ_KILLPROC * pkill = (REQ_KILLPROC *)pBuf;
int pid = pkill->pid;

ACK_KILLPROC * pkillack = (ACK_KILLPROC *)pBuf;
pkillack->id = ACKKILLPROC;
// 打开目标进程,取得进程句柄
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if(NULL==hProcess)
{
printf("get proc handle failed...\n");
pkillack->res = 0;
return;
}
// 终止进程
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
printf("kill proc success\n");
pkillack->res = 1;
}





void CProjectDlg::OnSearchProcess() //检索进程
{
// TODO: Add your control notification handler code here

REQ_PROCLIST * psearch = (REQ_PROCLIST *)pBuf;
psearch->id= REQPROCLIST;
ReleaseSemaphore(
hBufEmpty, // handle to semaphore
1, // increase count by one
NULL);
WaitForSingleObject(
hBufFull,
INFINITE);
ACK_PROCLIST * psearchack = (ACK_PROCLIST *)pBuf;

char szProcessName[40];
char szProcessPID[20];
char szThreadNum[20];
char szProcessPath[256];
char szSystemPath[256]; //不知道怎么回事,系统进程取不到全路径
char szWindowPath[256];
int nItem = 0; // 项计数



if(psearchack->id != ACKPROCLIST)
{
MessageBox((LPCTSTR)"error");
return;
}
else
{
printf("no: %d szProcessName : %s %s %s %s\n",nItem,szProcessName,szProcessPID,szThreadNum,szProcessPath);
strcpy(psearchack->proclist[nItem].szProcessName,szProcessName);
strcpy(psearchack->proclist[nItem].szProcessPID,szProcessPID);
strcpy(psearchack->proclist[nItem].szThreadNum,szThreadNum);
strcpy(psearchack->proclist[nItem].szProcessPath,szProcessPath);
printf("no: %d szProcessName : %s %s %s %s\n",nItem,szProcessName,szProcessPID,szThreadNum,szProcessPath);
nItem++;
}


}

...全文
66 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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