如何在非托管 c++ 中查找调用者程序集名称动态链接库
我有一个非托管 c++ dll。我正在从 c# (.net 3.5) 调用此非托管 dll 的外部方法
我正在寻找一种方法来查找 witch c# 程序集正在调用我的非托管 c++ dll (进入我的 c++ dll)(至少,程序集的名称)
当然,我不想将任何附加参数传递给方法。
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这需要堆栈遍历。在托管代码中运行良好,这就是代码访问安全性的实现方式。当有本机堆栈帧需要遍历时,不工作得很好吗?您可以在本机代码中尝试 StackWalk64()。价格昂贵,而且效果不太好,特别是在 .NET 4.0 中,CLR 不再伪造模块。请非常警惕帧指针省略优化选项。
我想说,不要这样做。只需让托管代码传递一个额外的参数,解决问题就容易得多。
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.
最后我找到了解决方案。
我正在寻找一种方法来限制不允许访问我的非托管 dll。因此,我爬取了调用者程序集位置的堆栈跟踪。
最后我决定检查调用程序集的公钥令牌(通过这种方式找到)并验证它。
感谢您的时间和答复...
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...
你不可能知道。您的外部方法可以由任何 C 兼容语言调用,因此 Windows 和 CRT 不存储任何有关 CLR 语言的额外信息。
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.