我想列举系统当前的进程及相应exe文件的路径,使用如下API实现(详细过程见后面的代码):CreateToolhelp32Snapshot
Process32First
Process32Next
Module32First
Module32Next我的程序用Vc++6.0(含最新的SDK) Unicode 方式编译,在中文XP下运行一切正常。但当我将“控制面板,区域和语言选项,高级,非Unicode程序的语言” 从“中文(中国)”改为“英语(美国)”的时候,如果进程所在的路径中有中文字符,路径显示就不正常,例如:
c:\Programe Files\软件\a.exe
显示为
c:\Programe Files\??\a.exe////////////////////////////// 代码 ////////////////////////////-----------Button
void CListProcessDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32W pe32;
DWORD dwPriorityClass; // Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
MessageBox(_T("INVALID_HANDLE_VALUE!");
return;
} // Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32W ); // Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32FirstW( hProcessSnap, &pe32 ) )
{
MessageBox(_T("Process32First error!");
CloseHandle( hProcessSnap ); // Must clean up the snapshot object!
return;
} // Now walk the snapshot of processes, and
// display information about each process in turn
do
{
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
if( hProcess == NULL )
MessageBox(_T("hProcess == NULL");
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
MessageBox(_T("!dwPriorityClass");
CloseHandle( hProcess );
} // List the modules and threads associated with this process
ListModules( pe32.th32ProcessID ); } while( Process32NextW( hProcessSnap, &pe32 ) ); CloseHandle( hProcessSnap );
return;
}
//---------显示进程的模块
bool CListProcessDlg:istModules(DWORD dwPID)
{
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32W me32; // Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if( hModuleSnap == INVALID_HANDLE_VALUE )
{
MessageBox(_T("CreateToolhelp32Snapshot error");
return( FALSE );
} // Set the size of the structure before using it.
me32.dwSize = sizeof( MODULEENTRY32W ); // Retrieve information about the first module,
// and exit if unsuccessful
if( !Module32FirstW( hModuleSnap, &me32 ) )
{
MessageBox(_T("Module32First error");
CloseHandle( hModuleSnap ); // Must clean up the snapshot object!
return( FALSE );
} // Now walk the module list of the process,
// and display information about each module
//因为我只需要得到exe文件的路径,所以这个循环不要
//do
{
//----下面这两个地方的显示结果是一样的,有中文字符就显示为 ?
MessageBox(me32.szExePath); //弹出窗口显示exe文件的路径
m_edit.SetWindowText(me32.szExePath); //edit控件中显示exe文件的路径
}
//while( Module32NextW( hModuleSnap, &me32 ) ); CloseHandle( hModuleSnap );
return( TRUE );
}///////////////////////////////////////////////////////////////////////////////////附加说明几点:
1。问题应该有办法解决,因为我在测试的时候发现另外一个软件解决了这个问题。
2。我debug的时候,发现只要有汉字的地方me32.szExePath 的内存都是 3F 00 (?的ascii)!这说明应该不是显示的问题.谢谢解答!
Process32First
Process32Next
Module32First
Module32Next我的程序用Vc++6.0(含最新的SDK) Unicode 方式编译,在中文XP下运行一切正常。但当我将“控制面板,区域和语言选项,高级,非Unicode程序的语言” 从“中文(中国)”改为“英语(美国)”的时候,如果进程所在的路径中有中文字符,路径显示就不正常,例如:
c:\Programe Files\软件\a.exe
显示为
c:\Programe Files\??\a.exe////////////////////////////// 代码 ////////////////////////////-----------Button
void CListProcessDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32W pe32;
DWORD dwPriorityClass; // Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
MessageBox(_T("INVALID_HANDLE_VALUE!");
return;
} // Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32W ); // Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32FirstW( hProcessSnap, &pe32 ) )
{
MessageBox(_T("Process32First error!");
CloseHandle( hProcessSnap ); // Must clean up the snapshot object!
return;
} // Now walk the snapshot of processes, and
// display information about each process in turn
do
{
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
if( hProcess == NULL )
MessageBox(_T("hProcess == NULL");
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
MessageBox(_T("!dwPriorityClass");
CloseHandle( hProcess );
} // List the modules and threads associated with this process
ListModules( pe32.th32ProcessID ); } while( Process32NextW( hProcessSnap, &pe32 ) ); CloseHandle( hProcessSnap );
return;
}
//---------显示进程的模块
bool CListProcessDlg:istModules(DWORD dwPID)
{
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32W me32; // Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if( hModuleSnap == INVALID_HANDLE_VALUE )
{
MessageBox(_T("CreateToolhelp32Snapshot error");
return( FALSE );
} // Set the size of the structure before using it.
me32.dwSize = sizeof( MODULEENTRY32W ); // Retrieve information about the first module,
// and exit if unsuccessful
if( !Module32FirstW( hModuleSnap, &me32 ) )
{
MessageBox(_T("Module32First error");
CloseHandle( hModuleSnap ); // Must clean up the snapshot object!
return( FALSE );
} // Now walk the module list of the process,
// and display information about each module
//因为我只需要得到exe文件的路径,所以这个循环不要
//do
{
//----下面这两个地方的显示结果是一样的,有中文字符就显示为 ?
MessageBox(me32.szExePath); //弹出窗口显示exe文件的路径
m_edit.SetWindowText(me32.szExePath); //edit控件中显示exe文件的路径
}
//while( Module32NextW( hModuleSnap, &me32 ) ); CloseHandle( hModuleSnap );
return( TRUE );
}///////////////////////////////////////////////////////////////////////////////////附加说明几点:
1。问题应该有办法解决,因为我在测试的时候发现另外一个软件解决了这个问题。
2。我debug的时候,发现只要有汉字的地方me32.szExePath 的内存都是 3F 00 (?的ascii)!这说明应该不是显示的问题.谢谢解答!
http://zhaomeng-blog.blog.sohu.com/45299954.html