How to find caller assembly name in unmanaged c++ dll

空扰寡人 提交于 2019-12-21 06:24:42

问题


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.



来源:https://stackoverflow.com/questions/5459526/how-to-find-caller-assembly-name-in-unmanaged-c-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!