Search code examples
.netclrunmanagedmanaged

Determine which .NET assembly calling unmanaged code


I have .NET assembly that calling functions from unmanaged DLL written in C (without CLR support). Is there any way that unmanaged DLL can determine which assembly calls their code? I'm looking for way without enabling CLR support in unmanaged DLL.


Solution

  • No, you cannot safely perform stackwalks in native code. That's only possible in managed code, it provides stack frame layout guarantees. Required by the garbage collector and CAS checks. C compilers have optimizers that remove such guarantees. They can because the language doesn't support nor require stackwalks.

    Even if you de-tune the optimizer, you'll still have the significant obstacle of walking through the stack frames of the pinvoke marshaller and mapping a guessed address of just-in-time generated machine code to an assembly. Only the CLR knows how to do that. You cannot make this work.