Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Система";
Текущий архив: 2003.09.22;
Скачать: [xml.tar.bz2];

Вниз

Процессы в NT4   Найти похожие ветки 

 
MakNik   (2003-07-11 09:41) [0]

Перепробовал разные варианты и все безрезультатно.... Подскажите, пожалуйста, может кто знает, как получить список процессов в NT4??


 
MBo   (2003-07-11 09:45) [1]

Даже и не знаю, что сказать...
Тебе несколько раз говорили о PSAPI (см. psapi.pas), а что толку...


 
MakNik   (2003-07-11 18:17) [2]

... Я конечно порошу прощения за глупые и надоедливые вопросы...
но я же чайник... помогите разобраться... всего лишь маленький примерчик... Смотрел я psapi.pas еще в первый раз, но еще больше запутался... Покажите, пожалуйста, новичку как мне в Memo получить ID всех процессов в NT4??? Заранее спасибо!!!


 
jonni   (2003-07-11 18:56) [3]

mozet tebe vot eto pomozet.....
//---------------------------------------------------------------------------
// EnumProcesses_PsApi
//
// Enumerates all processes in the system using PSAPI.
//
// Parameters:
// pszMachineName - must be NULL
// pfnEnumProc - address of the function to call for each process
// lParam - additional parameter to pass to the function
//
// Returns:
// TRUE, if successful, FALSE - otherwise.
//
BOOL
WINAPI
EnumProcesses_PsApi(
IN LPCTSTR pszMachineName,
IN PFNENUMPROC pfnEnumProc,
IN LPARAM lParam
)
{
_UNUSED(pszMachineName);

_ASSERTE(pfnEnumProc != NULL);
_ASSERTE(pszMachineName == NULL);

HINSTANCE hPsApi;
BOOL (WINAPI * _EnumProcesses)(DWORD *, DWORD, DWORD *);
BOOL (WINAPI * _EnumProcessModules)(HANDLE, HMODULE *, DWORD, DWORD *);
BOOL (WINAPI * _GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD);

// load PSAPI.DLL
hPsApi = LoadLibrary(_T("psapi.dll"));
if (hPsApi == NULL)
return FALSE;

// find necessary entry points in PSAPI.DLL
*(FARPROC *)&_EnumProcesses =
GetProcAddress(hPsApi, "EnumProcesses");
*(FARPROC *)&_EnumProcessModules =
GetProcAddress(hPsApi, "EnumProcessModules");
#ifdef UNICODE
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExW");
#else
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExA");
#endif

if (_EnumProcesses == NULL ||
_EnumProcessModules == NULL ||
_GetModuleFileNameEx == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_PROC_NOT_FOUND), FALSE;
}

// obtain a handle to the default process heap
HANDLE hHeap = GetProcessHeap();

DWORD dwError;
DWORD cbReturned;
DWORD cbAlloc = 128;
DWORD * pdwIds = NULL;

OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
GetVersionEx(&osvi);

DWORD dwSystemId = (osvi.dwMajorVersion < 5) ? 2 : 8;

do
{
cbAlloc *= 2;

if (pdwIds != NULL)
HeapFree(hHeap, 0, pdwIds);

// allocate memory for the array of process identifiers
pdwIds = (DWORD *)HeapAlloc(hHeap, 0, cbAlloc);
if (pdwIds == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_NOT_ENOUGH_MEMORY), FALSE;
}

// retrieve process identifiers
if (!_EnumProcesses(pdwIds, cbAlloc, &cbReturned))
{
dwError = GetLastError();

HeapFree(hHeap, 0, pdwIds);
FreeLibrary(hPsApi);
return SetLastError(dwError), FALSE;
}
}
while (cbReturned == cbAlloc);

// now loop through the process identifiers and call the callback
// function for each process identifier obtained
for (DWORD i = 0; i < cbReturned / sizeof(DWORD); i++)
{
BOOL bContinue;
DWORD dwProcessId = pdwIds[i];

// handle two special cases: Idle process (0) and System process
if (dwProcessId == 0)
{
bContinue = pfnEnumProc(0, _T("Idle"), lParam);
}
else if (dwProcessId == dwSystemId)
{
bContinue = pfnEnumProc(dwSystemId, _T("System"), lParam);
}
else
{
HANDLE hProcess;
HMODULE hExeModule;
DWORD cbNeeded;
TCHAR szModulePath[MAX_PATH];
LPTSTR pszProcessName = NULL;

// open the process handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE, dwProcessId);
if (hProcess != NULL)
{
if (_EnumProcessModules(hProcess, &hExeModule,
sizeof(HMODULE), &cbNeeded))
{
if (_GetModuleFileNameEx(hProcess, hExeModule,
szModulePath, MAX_PATH))
{
pszProcessName = _tcsrchr(szModulePath, _T("\\"));
if (pszProcessName == NULL)
pszProcessName = szModulePath;
else
pszProcessName++;
}
}
}

CloseHandle(hProcess);

// enumerate this process
bContinue = pfnEnumProc(dwProcessId, pszProcessName, lParam);
}

if (!bContinue)
( hHeap, 0, pdwIds) mozet tebe vot eto pomozet.....
//---------------------------------------------------------------------------
// EnumProcesses_PsApi
//
// Enumerates all processes in the system using PSAPI.
//
// Parameters:
// pszMachineName - must be NULL
// pfnEnumProc - address of the function to call for each process
// lParam - additional parameter to pass to the function
//
// Returns:
// TRUE, if successful, FALSE - otherwise.
//
BOOL
WINAPI
EnumProcesses_PsApi(
IN LPCTSTR pszMachineName,
IN PFNENUMPROC pfnEnumProc,
IN LPARAM lParam
)
{
_UNUSED(pszMachineName);

_ASSERTE(pfnEnumProc != NULL);
_ASSERTE(pszMachineName == NULL);

HINSTANCE hPsApi;
BOOL (WINAPI * _EnumProcesses)(DWORD *, DWORD, DWORD *);
BOOL (WINAPI * _EnumProcessModules)(HANDLE, HMODULE *, DWORD, DWORD *);
BOOL (WINAPI * _GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD);

// load PSAPI.DLL
hPsApi = LoadLibrary(_T("psapi.dll"));
if (hPsApi == NULL)
return FALSE;

// find necessary entry points in PSAPI.DLL
*(FARPROC *)&_EnumProcesses =
GetProcAddress(hPsApi, "EnumProcesses");
*(FARPROC *)&_EnumProcessModules =
GetProcAddress(hPsApi, "EnumProcessModules");
#ifdef UNICODE
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExW");
#else
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExA");
#endif

if (_EnumProcesses == NULL ||
_EnumProcessModules == NULL ||
_GetModuleFileNameEx == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_PROC_NOT_FOUND), FALSE;
}

// obtain a handle to the default process heap
HANDLE hHeap = GetProcessHeap();

DWORD dwError;
DWORD cbReturned;
DWORD cbAlloc = 128;
DWORD * pdwIds = NULL;

OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
GetVersionEx(&osvi);

DWORD dwSystemId = (osvi.dwMajorVersion < 5) ? 2 : 8;

do
{
cbAlloc *= 2;

if (pdwIds != NULL)
HeapFree(hHeap, 0, pdwIds);

// allocate memory for the array of process identifiers
pdwIds = (DWORD *)HeapAlloc(hHeap, 0, cbAlloc);
if (pdwIds == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_NOT_ENOUGH_MEMORY), FALSE;
}

// retrieve process identifiers
if (!_EnumProcesses(pdwIds, cbAlloc, &cbReturned))
{
dwError = GetLastError();

HeapFree(hHeap, 0, pdwIds);
FreeLibrary(hPsApi);
return SetLastError(dwError), FALSE;
}
}
while (cbReturned == cbAlloc);

// now loop through the process identifiers and call the callback
// function for each process identifier obtained
for (DWORD i = 0; i < cbReturned / sizeof(DWORD); i++)
{
BOOL bContinue;
DWORD dwProcessId = pdwIds[i];

// handle two special cases: Idle process (0) and System process
if (dwProcessId == 0)
{
bContinue = pfnEnumProc(0, _T("Idle"), lParam);
}
else if (dwProcessId == dwSystemId)
{
bContinue = pfnEnumProc(dwSystemId, _T("System"), lParam);
}
else
{
HANDLE hProcess;
HMODULE hExeModule;
DWORD cbNeeded;
TCHAR szModulePath[MAX_PATH];
LPTSTR pszProcessName = NULL;

// open the process handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE, dwProcessId);
if (hProcess != NULL)
{
if (_EnumProcessModules(hProcess, &hExeModule,
sizeof(HMODULE), &cbNeeded))
{
if (_GetModuleFileNameEx(hProcess, hExeModule,
szModulePath, MAX_PATH))
{
pszProcessName = _tcsrchr(szModulePath, _T("\\"));
if (pszProcessName == NULL)
pszProcessName = szModulePath;
else
pszProcessName++;
}
}
}

CloseHandle(hProcess);

// enumerate this process
bContinue = pfnEnumProc(dwProcessId, pszProcessName, lParam);
}

if (!bContinue)
break;
}

HeapFree(hHeap, 0, pdwIds);
FreeLibrary(hPsApi);

return TRUE;
}


 
jonni   (2003-07-11 18:56) [4]

mozet tebe vot eto pomozet.....
//---------------------------------------------------------------------------
// EnumProcesses_PsApi
//
// Enumerates all processes in the system using PSAPI.
//
// Parameters:
// pszMachineName - must be NULL
// pfnEnumProc - address of the function to call for each process
// lParam - additional parameter to pass to the function
//
// Returns:
// TRUE, if successful, FALSE - otherwise.
//
BOOL
WINAPI
EnumProcesses_PsApi(
IN LPCTSTR pszMachineName,
IN PFNENUMPROC pfnEnumProc,
IN LPARAM lParam
)
{
_UNUSED(pszMachineName);

_ASSERTE(pfnEnumProc != NULL);
_ASSERTE(pszMachineName == NULL);

HINSTANCE hPsApi;
BOOL (WINAPI * _EnumProcesses)(DWORD *, DWORD, DWORD *);
BOOL (WINAPI * _EnumProcessModules)(HANDLE, HMODULE *, DWORD, DWORD *);
BOOL (WINAPI * _GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD);

// load PSAPI.DLL
hPsApi = LoadLibrary(_T("psapi.dll"));
if (hPsApi == NULL)
return FALSE;

// find necessary entry points in PSAPI.DLL
*(FARPROC *)&_EnumProcesses =
GetProcAddress(hPsApi, "EnumProcesses");
*(FARPROC *)&_EnumProcessModules =
GetProcAddress(hPsApi, "EnumProcessModules");
#ifdef UNICODE
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExW");
#else
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExA");
#endif

if (_EnumProcesses == NULL ||
_EnumProcessModules == NULL ||
_GetModuleFileNameEx == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_PROC_NOT_FOUND), FALSE;
}

// obtain a handle to the default process heap
HANDLE hHeap = GetProcessHeap();

DWORD dwError;
DWORD cbReturned;
DWORD cbAlloc = 128;
DWORD * pdwIds = NULL;

OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
GetVersionEx(&osvi);

DWORD dwSystemId = (osvi.dwMajorVersion < 5) ? 2 : 8;

do
{
cbAlloc *= 2;

if (pdwIds != NULL)
HeapFree(hHeap, 0, pdwIds);

// allocate memory for the array of process identifiers
pdwIds = (DWORD *)HeapAlloc(hHeap, 0, cbAlloc);
if (pdwIds == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_NOT_ENOUGH_MEMORY), FALSE;
}

// retrieve process identifiers
if (!_EnumProcesses(pdwIds, cbAlloc, &cbReturned))
{
dwError = GetLastError();

HeapFree(hHeap, 0, pdwIds);
FreeLibrary(hPsApi);
return SetLastError(dwError), FALSE;
}
}
while (cbReturned == cbAlloc);

// now loop through the process identifiers and call the callback
// function for each process identifier obtained
for (DWORD i = 0; i < cbReturned / sizeof(DWORD); i++)
{
BOOL bContinue;
DWORD dwProcessId = pdwIds[i];

// handle two special cases: Idle process (0) and System process
if (dwProcessId == 0)
{
bContinue = pfnEnumProc(0, _T("Idle"), lParam);
}
else if (dwProcessId == dwSystemId)
{
bContinue = pfnEnumProc(dwSystemId, _T("System"), lParam);
}
else
{
HANDLE hProcess;
HMODULE hExeModule;
DWORD cbNeeded;
TCHAR szModulePath[MAX_PATH];
LPTSTR pszProcessName = NULL;

// open the process handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE, dwProcessId);
if (hProcess != NULL)
{
if (_EnumProcessModules(hProcess, &hExeModule,
sizeof(HMODULE), &cbNeeded))
{
if (_GetModuleFileNameEx(hProcess, hExeModule,
szModulePath, MAX_PATH))
{
pszProcessName = _tcsrchr(szModulePath, _T("\\"));
if (pszProcessName == NULL)
pszProcessName = szModulePath;
else
pszProcessName++;
}
}
}

CloseHandle(hProcess);

// enumerate this process
bContinue = pfnEnumProc(dwProcessId, pszProcessName, lParam);
}

if (!bContinue)
( hHeap, 0, pdwIds) mozet tebe vot eto pomozet.....
//---------------------------------------------------------------------------
// EnumProcesses_PsApi
//
// Enumerates all processes in the system using PSAPI.
//
// Parameters:
// pszMachineName - must be NULL
// pfnEnumProc - address of the function to call for each process
// lParam - additional parameter to pass to the function
//
// Returns:
// TRUE, if successful, FALSE - otherwise.
//
BOOL
WINAPI
EnumProcesses_PsApi(
IN LPCTSTR pszMachineName,
IN PFNENUMPROC pfnEnumProc,
IN LPARAM lParam
)
{
_UNUSED(pszMachineName);

_ASSERTE(pfnEnumProc != NULL);
_ASSERTE(pszMachineName == NULL);

HINSTANCE hPsApi;
BOOL (WINAPI * _EnumProcesses)(DWORD *, DWORD, DWORD *);
BOOL (WINAPI * _EnumProcessModules)(HANDLE, HMODULE *, DWORD, DWORD *);
BOOL (WINAPI * _GetModuleFileNameEx)(HANDLE, HMODULE, LPTSTR, DWORD);

// load PSAPI.DLL
hPsApi = LoadLibrary(_T("psapi.dll"));
if (hPsApi == NULL)
return FALSE;

// find necessary entry points in PSAPI.DLL
*(FARPROC *)&_EnumProcesses =
GetProcAddress(hPsApi, "EnumProcesses");
*(FARPROC *)&_EnumProcessModules =
GetProcAddress(hPsApi, "EnumProcessModules");
#ifdef UNICODE
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExW");
#else
*(FARPROC *)&_GetModuleFileNameEx =
GetProcAddress(hPsApi, "GetModuleFileNameExA");
#endif

if (_EnumProcesses == NULL ||
_EnumProcessModules == NULL ||
_GetModuleFileNameEx == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_PROC_NOT_FOUND), FALSE;
}

// obtain a handle to the default process heap
HANDLE hHeap = GetProcessHeap();

DWORD dwError;
DWORD cbReturned;
DWORD cbAlloc = 128;
DWORD * pdwIds = NULL;

OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
GetVersionEx(&osvi);

DWORD dwSystemId = (osvi.dwMajorVersion < 5) ? 2 : 8;

do
{
cbAlloc *= 2;

if (pdwIds != NULL)
HeapFree(hHeap, 0, pdwIds);

// allocate memory for the array of process identifiers
pdwIds = (DWORD *)HeapAlloc(hHeap, 0, cbAlloc);
if (pdwIds == NULL)
{
FreeLibrary(hPsApi);
return SetLastError(ERROR_NOT_ENOUGH_MEMORY), FALSE;
}

// retrieve process identifiers
if (!_EnumProcesses(pdwIds, cbAlloc, &cbReturned))
{
dwError = GetLastError();

HeapFree(hHeap, 0, pdwIds);
FreeLibrary(hPsApi);
return SetLastError(dwError), FALSE;
}
}
while (cbReturned == cbAlloc);

// now loop through the process identifiers and call the callback
// function for each process identifier obtained
for (DWORD i = 0; i < cbReturned / sizeof(DWORD); i++)
{
BOOL bContinue;
DWORD dwProcessId = pdwIds[i];

// handle two special cases: Idle process (0) and System process
if (dwProcessId == 0)
{
bContinue = pfnEnumProc(0, _T("Idle"), lParam);
}
else if (dwProcessId == dwSystemId)
{
bContinue = pfnEnumProc(dwSystemId, _T("System"), lParam);
}
else
{
HANDLE hProcess;
HMODULE hExeModule;
DWORD cbNeeded;
TCHAR szModulePath[MAX_PATH];
LPTSTR pszProcessName = NULL;

// open the process handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE, dwProcessId);
if (hProcess != NULL)
{
if (_EnumProcessModules(hProcess, &hExeModule,
sizeof(HMODULE), &cbNeeded))
{
if (_GetModuleFileNameEx(hProcess, hExeModule,
szModulePath, MAX_PATH))
{
pszProcessName = _tcsrchr(szModulePath, _T("\\"));
if (pszProcessName == NULL)
pszProcessName = szModulePath;
else
pszProcessName++;
}
}
}

CloseHandle(hProcess);

// enumerate this process
bContinue = pfnEnumProc(dwProcessId, pszProcessName, lParam);
}

if (!bContinue)
break;
}

HeapFree(hHeap, 0, pdwIds);
FreeLibrary(hPsApi);

return TRUE;
}


 
Burmistroff   (2003-07-11 18:58) [5]

Не поверю, что ни в одном FAQ этого нельзя найти.

Можешь попробовать посмотреть ListProcX в procman.pas на
http://maxcomputing.narod.ru/dev.html


 
Spawn   (2003-07-11 21:06) [6]

procedure TForm1.Button1Click(Sender: TObject);
var
lpDWORD,val:PDWORD;
size,needed:cardinal;
i, base:integer;
str:string;
begin
try
size:=1024;
needed:=0;
GetMem(lpDWORD,size);
EnumProcesses(lpDWORD,size,needed);
for i:=1 to needed div SizeOf(DWORD) do
begin
ListBox1.Items.Add(IntToStr(lpDWORD^));
Inc(lpDWORD)
end;
finally
FreeMem(lpDWORD);
end;
end;


Только почему то вызов FreeMem приводит к ошибке


 
Spawn   (2003-07-11 21:36) [7]

А ну все правильно)) Ошибка из за того что я сместил указатель и пытаюсь освободить данные по другому алресу. Нужно сначала сделать:

Dec(lpDWord,needed div SizeOf(DWORD))



Страницы: 1 вся ветка

Форум: "Система";
Текущий архив: 2003.09.22;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.5 MB
Время: 0.01 c
14-71961
Denkop
2003-08-27 09:27
2003.09.22
Надумал покупать цифровой фотик


6-71785
s002156Shurik
2003-07-01 16:11
2003.09.22
Как послать SMS с телефона подключенного к компьютеру?


1-71630
HeZgox
2003-09-08 23:59
2003.09.22
как отсортировать StringGrid по любой колонке??


3-71517
Partizan
2003-09-01 01:23
2003.09.22
TDBgrid


3-71541
pashtet
2003-08-29 17:11
2003.09.22
DBGrid





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский