I'm targeting .NET 4.6.1, and I have C# code that calls C++/CLI code that calls the native Win32 method EnumProcessModulesEx
, which needs a HANDLE as its first input parameter:
// C#
System.Diagnostics.Process process = // ...
var allModuleNames = ProcessHelper.GetAllModuleNames(process);
// C++/CLI
#include <Psapi.h>
// ...
using namespace System;
using namespace System::Diagnostics;
// ...
array<String^>^ ProcessHelper::GetAllModuleNames(Process^ process)
{
// Should I use a SafeProcessHandle instead?
HANDLE processHandle = static_cast<HANDLE>(process->Handle.ToPointer());
BOOL result = EnumProcessModulesEx(processHandle, /*...*/);
// ...
}
I have control over both the C# and C++/CLI code, and I'm not doing any P/Invoke. My C++/CLI method currently accepts a Process
parameter, but it's only using the Process.Handle
property (and doing a cast to obtain the necessary HANDLE value). Is this safe, or do I need a SafeProcessHandle
somewhere? If so, how do I pass the SafeProcessHandle
value to EnumProcessModulesEx
? Do I have to call SafeHandle.DangerousGetHandle
?