比如要启动a.exe的时候判断b.exe有没有在运行,如果b.exe已经启动则a.exe就不能启动,没有则可以启动。
解决方案 »
- 电脑声卡的MIC/SPK进行音频采集与分析
- 初始化对话框
- 存储HBITMAP到BMP文件出错(附源码)
- 某大型系统的多语言切换外包开发
- 如何用VC调用其它语言写的DLL文件,此DLL应该是个COM,最好能给个例子
- InstallShieldl----> setup.rul里的 svResult = sdLoadString(IFX_MAINTUI_MSG)
- 如何更改list control中最上面一行标题头的字体大小?(报表样式)
- Jpeg编码中控制压缩品质的是哪一个步骤
- boost asio服务器端调用close后,客户端为什么还挂在接收函数中?
- 麻烦您了,介绍一本好的VC的书!
- 我这里有段代码,贴出来的图填充客户区,如何不填充,我的图是小图?
- 请问一下让浏览器跳转除了BHO还有什么方法可以实现?
void CloseProgram(CString strProgram)
{
HANDLE handle; //定义CreateToolhelp32Snapshot系统快照句柄
HANDLE handle1; //定义要结束进程句柄
handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//获得系统快照句柄
PROCESSENTRY32 *info; //定义PROCESSENTRY32结构字指
//PROCESSENTRY32 结构的 dwSize 成员设置成 sizeof(PROCESSENTRY32) info = new PROCESSENTRY32;
info->dwSize = sizeof(PROCESSENTRY32);
//调用一次 Process32First 函数,从快照中获取进程列表
Process32First(handle, info);
//重复调用 Process32Next,直到函数返回 FALSE 为止
while(Process32Next(handle, info) != FALSE)
{
CString strTmp = info->szExeFile; //指向进程名字
//strcmp字符串比较函数同要结束相同
//if(strcmp(c, info->szExeFile) == 0 )
if (strProgram.CompareNoCase(info->szExeFile) == 0 )
{
//PROCESS_TERMINATE表示为结束操作打开,FALSE=可继承,info->th32ProcessID=进程ID
handle1 = OpenProcess(PROCESS_TERMINATE, FALSE, info->th32ProcessID);
//结束进程
TerminateProcess(handle1, 0);
}
}
delete info; CloseHandle(handle);
}
#include <stdio.h>
#include <tchar.h>
#include "psapi.h"void PrintProcessNameAndID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID ); // Get the process name. if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
} // Print the process name and identifier. _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID ); CloseHandle( hProcess );
}void main( )
{
// Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the name and process identifier for each process. for ( i = 0; i < cProcesses; i++ )
PrintProcessNameAndID( aProcesses[i] );
}
如果a、b都是你的程序,或者说是单实例程序,就生成一个GUID创建Mutex就是了,只要系统中有进程创建过了,CreateMutex后GetLastError的值就是ERROR_ALREADY_EXISTS
{
PROCESSENTRY32 pe32;
pe32.dwSize=sizeof(pe32);
HANDLE hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap==INVALID_HANDLE_VALUE)
{
printf("CreatToolhelp32Snapeshot调用失败!\n");
return -1;
}
BOOL bMore=Process32First(hProcessSnap,&pe32);//获得第一个进程的句柄。注意是第一个所以还要while()
while(bMore)
{
bMore=Process32Next(hProcessSnap,&pe32);//这里要注意,必须循环,因为你只运行一次函数,只能取到进程
CString strFindFile;
strFindFile.Format(_T("%s"), strProcess);
CString strExeFile;
strExeFile.Format(_T("%s"), pe32.szExeFile);
if (strExeFile.CompareNoCase(strFindFile) == 0)
{
CloseHandle(hProcessSnap);
return true;
}
}
CloseHandle(hProcessSnap);
return false;
}
PROCESSENTRY32* info=new PROCESSENTRY32;
info->dwSize=sizeof(PROCESSENTRY32);
int i=0;
if(Process32First(handle,info))
{
if(GetLastError()==ERROR_NO_MORE_FILES )
{
AfxMessageBox("No More Process");
}
else
{
CString ProName;
ProName.Format("%s",info->szExeFile);
if(ProName == "b.exe")
{ 你要干的事! }
}
}