Biohazrd icon indicating copy to clipboard operation
Biohazrd copied to clipboard

Provide helpers to get function pointers to C++ functions

Open PathogenDavid opened this issue 3 years ago • 1 comments

In C# it is not possible to get the unmanaged function pointer behind a DllImport. (The function pointer type of a DllImport is managed because it might be a marshal stub.)

While it is not common, sometimes it is necessary to get the function pointer of an unmanaged function. Once such example is getting the function pointer for PxDefaultSimulationFilterShader for use with PxSceneDesc::filterShader.

This could be implemented in one of two ways:

  • Replacing (or including) an additional TranslatedExportedFunctionPointer type to represent the field to be exported.
  • Adding #24 extensible metadata to mark the field as being translated as a function pointer.

In theory we actually could use function pointers instead of DllImport ~~since we never expect to need marshaling to happen~~(this was based on an outdated understanding that function pointers didn't get marshaling. They actually do, and you can't even configure it. See https://github.com/InfectedLibraries/Biohazrd/issues/99), but that would also require callers to be unsafe.

Too bad C# doesn't support functors, we could have a special thin struct that wraps the pointer.

PathogenDavid avatar Oct 22 '20 17:10 PathogenDavid

The cumulative changes being made for https://github.com/InfectedLibraries/Biohazrd/issues/198 simplify our ability to do this since EmitFunctionParameterList is emitting the parameters for vtable function pointers.

PathogenDavid avatar Jul 04 '21 13:07 PathogenDavid