Changeset 1812
- Timestamp:
- 2/11/2010 2:15:09 AM (3 years ago)
- Location:
- trunk/eraser6/Eraser.Util.Native
- Files:
-
- 4 edited
-
OpenHandle.NameResolver.cpp (modified) (2 diffs)
-
OpenHandle.NameResolver.h (modified) (1 diff)
-
OpenHandle.cpp (modified) (1 diff)
-
OpenHandle.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/eraser6/Eraser.Util.Native/OpenHandle.NameResolver.cpp
r1802 r1812 23 23 #include "OpenHandle.h" 24 24 25 #pragma managed(push)26 25 #pragma unmanaged 27 26 28 27 namespace { 28 HANDLE NameResolutionThread; 29 NameResolutionThreadParams NameResolutionThreadParam; 30 29 31 DWORD __stdcall nameResolutionThread(void* data) 30 32 { … … 86 88 return 0; 87 89 } 90 91 void CreateNameThread(HANDLE& handle, NameResolutionThreadParams& params) 92 { 93 //If the handle is valid terminate the thread 94 if (handle) 95 { 96 TerminateThread(handle, 1); 97 CloseHandle(handle); 98 } 99 100 //Create the thread 101 handle = CreateThread(NULL, 0, nameResolutionThread, ¶ms, 0, NULL); 102 } 103 88 104 } 89 105 90 void CreateNameThread(HANDLE& handle, NameResolutionThreadParams& params)106 std::wstring ResolveHandleName(HANDLE handle, int pid) 91 107 { 92 //If the handle is valid terminate the thread 93 if (handle) 108 //Start a name resolution thread (in case one entry hangs) 109 if (NameResolutionThread == NULL) 110 CreateNameThread(NameResolutionThread, NameResolutionThreadParam); 111 112 //Create a duplicate handle 113 HANDLE localHandle; 114 HANDLE processHandle = OpenProcess(PROCESS_DUP_HANDLE, false, pid); 115 DuplicateHandle(processHandle, static_cast<void*>(handle), GetCurrentProcess(), 116 &localHandle, 0, false, DUPLICATE_SAME_ACCESS); 117 CloseHandle(processHandle); 118 119 //We need a handle 120 if (!localHandle) 121 return std::wstring(); 122 123 //Send the handle to the secondary thread for name resolution 124 NameResult result(localHandle); 125 NameResolutionThreadParam.Input.push_back(&result); 126 ReleaseSemaphore(NameResolutionThreadParam.Semaphore, 1, NULL); 127 128 //Wait for the result 129 if (WaitForSingleObject(result.Event, 50) != WAIT_OBJECT_0) 94 130 { 95 TerminateThread(handle, 1);96 C loseHandle(handle);131 //The wait failed. Terminate the thread and recreate another. 132 CreateNameThread(NameResolutionThread, NameResolutionThreadParam); 97 133 } 98 134 99 //Create the thread 100 handle = CreateThread(NULL, 0, nameResolutionThread, ¶ms, 0, NULL); 135 //Close the handle which we duplicated 136 CloseHandle(localHandle); 137 138 //Return the result 139 return result.Name; 101 140 } -
trunk/eraser6/Eraser.Util.Native/OpenHandle.NameResolver.h
r1802 r1812 81 81 }; 82 82 83 void CreateNameThread(HANDLE& handle, NameResolutionThreadParams& params);83 std::wstring ResolveHandleName(HANDLE handle, int pid); -
trunk/eraser6/Eraser.Util.Native/OpenHandle.cpp
r1802 r1812 73 73 String^ OpenHandle::ResolveHandlePath(IntPtr handle, int pid) 74 74 { 75 //Start a name resolution thread (in case one entry hangs) 76 if (NameResolutionThread == NULL) 77 { 78 NameResolutionThread = new HANDLE(0); 79 NameResolutionThreadParam = new NameResolutionThreadParams; 80 CreateNameThread(*NameResolutionThread, *NameResolutionThreadParam); 81 } 82 83 //Create a duplicate handle 84 HANDLE localHandle(NULL); 85 HANDLE processHandle = OpenProcess(PROCESS_DUP_HANDLE, false, pid); 86 DuplicateHandle(processHandle, static_cast<void*>(handle), GetCurrentProcess(), 87 &localHandle, 0, false, DUPLICATE_SAME_ACCESS); 88 CloseHandle(processHandle); 89 90 //We need a handle 91 if (!localHandle) 92 return nullptr; 93 94 //Send the handle to the secondary thread for name resolution 95 NameResult result(localHandle); 96 NameResolutionThreadParam->Input.push_back(&result); 97 ReleaseSemaphore(NameResolutionThreadParam->Semaphore, 1, NULL); 98 99 //Wait for the result 100 if (WaitForSingleObject(result.Event, 50) != WAIT_OBJECT_0) 101 { 102 //The wait failed. Terminate the thread and recreate another. 103 CreateNameThread(*NameResolutionThread, *NameResolutionThreadParam); 104 } 105 106 //Close the handle which we duplicated 107 CloseHandle(localHandle); 108 109 //Return the result 110 if (result.Name.empty()) 111 return nullptr; 112 else 113 return gcnew String(result.Name.c_str(), 0, 114 static_cast<int>(result.Name.length())); 75 std::wstring result(ResolveHandleName(static_cast<void*>(handle), pid)); 76 return result.empty() ? nullptr : 77 gcnew String(result.c_str(), 0, static_cast<int>(result.length())); 115 78 } 116 79 -
trunk/eraser6/Eraser.Util.Native/OpenHandle.h
r1802 r1812 93 93 94 94 private: 95 static HANDLE* NameResolutionThread;96 static NameResolutionThreadParams* NameResolutionThreadParam;97 98 95 IntPtr handle; 99 96 String^ path;
Note: See TracChangeset
for help on using the changeset viewer.
