How to find caller assembly name in unmanaged c++

2019-04-01 19:49发布

问题:

I have an unmanaged c++ dll. I am calling external methods of this unmanaged dll from c# (.net 3.5)

I am looking for a way to find witch c# assembly is calling my unmanaged c++ dll (into my c++ dll) (at least, name of assembly)

And sure, I don't want to pass any additional parameter to methods.

Thanks in advance

回答1:

This requires a stack walk. Works well in managed code, that's how code access security is implemented. Does not work so well when there are native stack frames to walk. You can try StackWalk64() in your native code. Expensive and not that likely to work out well, especially in .NET 4.0 where the CLR no longer fakes the module. Be very wary of the frame pointer omission optimization option.

Don't do this, I'd say. It is so much easier to solve simply by letting the managed code pass an extra argument.



回答2:

Finally I found the solution.

I was looking for a way to restrict not allowed access to my unmanaged dll. So I crawled the stack trace for my caller assembly location.

Finally I decided to check public key token of caller assembly (found this way) and validate it.

Thanks from you time and answers...



回答3:

You can't know. Your external methods could be called by any C-compatible language and as such Windows and the CRT store nothing extra about CLR languages.