花之剑'HOME

一朵飘舞在风中的雪花,挣扎着,不想被融化。

Win Api

Posted on 2008-09-24 14:52 花之剑 阅读(153) 评论(0)  编辑  收藏 所属分类: c/c++ & algorithm
   出于win和linux下兼容代码书写的问题,我决定研究研究win Api的用法,之前很长的一段时间,我认为我不会做win 下开发,现在看来,我错了,做linux下开发同样也要了解win 下API,在很多跨平台的的代码中,使用了可移植技术,因此我决定双管齐下,正所谓下课不仅要会耍剑,咱好要耍刀。
 在陈年的的book下找到了与unix高级环境编程同样经典的书籍。
   //获取本进程中所包含的module(dll),
    GetModuleFileName(HMOUDLE hModule,LPTSTR lpFilename, DWORD nSize )
如果hModule 为null,返回的是本执行文件的路径全名,否则为指定Module的名字(带路径)
//获取其他进程中module名字
GetModuleFileName(HANDLE,hProcessHMOUDLE hModule,LPTSTR lpFilename, DWORD nSize)
如果hModule 为null,返回的是参数一指定进程的路径全名,否则为指定Module的名字(带路径)


EnumProcessModules
BOOLEnumProcessModules(HANDLEhProcess,
HMODULE*lphModule,
DWORDcb,
LPDWORDlpcbNeeded);

Parameters

hProcess
[in] Handle to the process.
lphModule
[out] Pointer to the array that receives the list of module handles.
cb
[in] Size of the lphModule array, in bytes.
lpcbNeeded
[out] Number of bytes required to store all module handles in the lphModule array.


The OpenProcess function opens an existing process object.

HANDLEOpenProcess(DWORDdwDesiredAccess,
BOOLbInheritHandle,
DWORDdwProcessId);

Parameters

dwDesiredAccess
[in] Access to the process object. This access right is checked against any security descriptor for the process. This parameter can be one or more of the process access rights.
bInheritHandle
[in] If this parameter is TRUE, the handle is inheritable. If the parameter is FALSE, the handle cannot be inherited.
dwProcessId
[in] Identifier of the process to open.

EnumProcesses

The EnumProcesses function retrieves the process identifier for each process object in the system.

BOOLEnumProcesses(DWORD*pProcessIds,
DWORDcb,
DWORD*pBytesReturned);

Parameters

pProcessIds
[out] Pointer to an array that receives the list of process identifiers.
cb
[in] Size of the pProcessIds array, in bytes.
pBytesReturned
[out] Number of bytes returned in the pProcessIds array.



#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "psapi.h"

void PrintProcessNameAndID( DWORD processID )
{
 TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
 HMODULE hMod[1024];
 // Get a handle to the process.
 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
  PROCESS_VM_READ,
  FALSE, processID );
 // Get the process name.
 if (hProcess == NULL)
 {
  return;
 }
 DWORD cbNeeded;
 if ( EnumProcessModules( hProcess, hMod, sizeof(hMod),
  &cbNeeded) )
 {
  int nC = cbNeeded/sizeof(HMODULE);
  for (int i =0; i < nC; i++)
  {
   GetModuleFileNameEx(hProcess, hMod[i], szProcessName,
    sizeof(szProcessName)/sizeof(TCHAR) );
   _tprintf( TEXT("------%s  (PID: %u)\n"), szProcessName, processID );
  }
 }
 // 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] );
 getchar();
}



只有注册用户登录后才能发表评论。


网站导航: