读书人

再发帖:枚举系统当前进程的有关问题(

发布时间: 2012-02-01 16:58:19 作者: rapoo

再发帖:枚举系统当前进程的问题(附代码)!
我想列举系统当前的进程及相应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)!这说明应该不是显示的问题.

谢谢解答!

[解决办法]
帮顶
[解决办法]
混合编码解释是按照当前系统定义的语言来显示的,因为混合编码各个语言有交集,由于容易产生乱码,所以后来来了个unicode统一编码。楼主要在非中文语言下显示中文,只有将混合编码转成unicode显示。使用函数MultiByteToWideChar,第一个参数就是ANSI Code Page选择你的字符集,一般第一个参数使用CP_ACP(#define CP_ACP 0 // default to ANSI code page),他是按照当前语言转的,所以你使用这个变量是显示不了汉字的,中国简体(936),中国繁体(950),英语德语等(1252)等等,将第一个参数设成括号的值(936)就可以了。怎样显示unicode不用我说了吧。
[解决办法]
unicode的问题.转换后就行了.

曾经修改过一个程序,就是因为WideCharToMultiByte使用错了,导致程序出现错误,你可以参照一下:
http://zhaomeng-blog.blog.sohu.com/45299954.html

读书人网 >VC/MFC

热点推荐