MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Имя процесса по HANDLE возвращаемому CreateFile"
Answer 1180508
Начать с того, что если файл заблокирован в буквальном смысле, т.е. открыт с параметром FileShare=0, то вы и HANDLE получить не сможете.
Ну а по имени файла список процессов, которые его открыли, можно получить с помощью RmGetList:
#include <stdio.h> #include <Windows.h> #include <restartmanager.h> #pragma comment(lib, "Rstrtmgr.lib") void PrintProcesses(HANDLE hFile) { DWORD hSess = 0; WCHAR buffer[1000]; WCHAR key[50] = L"MySession"; DWORD res; res=GetFinalPathNameByHandleW(hFile, buffer, 1000, 0); if (res == 0) { wprintf(L"GetFinalPathNameByHandleW failed\n"); return; } res = RmStartSession(&hSess, 0, key); if (res != 0) { wprintf(L"Cannot start session\n"); return; } LPCWSTR filenames[] = { buffer }; res = RmRegisterResources(hSess, 1, filenames, 0, NULL, 0, NULL); if (res != 0) { wprintf(L"Cannot register resource\n"); return; } UINT procInfoNeeded; UINT procInfo = 10; DWORD rebootReasons; RM_PROCESS_INFO info[10]; res = RmGetList(hSess, &procInfoNeeded, &procInfo, info, &rebootReasons); if (res != 0) { wprintf(L"Cannot get list of processes\n"); return; } for (int i = 0; i < procInfo; i++) wprintf(L"%u: %s\n", info[i].Process.dwProcessId, info[i].strAppName); }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.