私有 DLL 函数可公开访问
我在 Visual Studio 2008 中使用 C++ 生成了一个 Win32 DLL。我的所有函数都在新 DLL 的唯一本机类中定义。有些函数被声明为私有
,而其他函数则被声明为公共
。我正在使用 Dependency Walker 客观地验证 DLL 中函数的可访问性。我的类声明为 __declspec(dllexport) MyClass { /* ... */ };
。这是否意味着所有类函数都可以公开访问,无论其权限如何?
我是否需要设置一个编译/项目选项来在编译的 DLL 中强制执行隐私权限?
I have generated a Win32 DLL using C++ in Visual Studio 2008. All of my functions are defined within the only class native to the new DLL. Some functions are declared as private
, and others are public
. I'm using Dependency Walker to objectively verify the accessibility of the functions in my DLL. My class is declared as __declspec(dllexport) MyClass { /* ... */ };
. Does this mean that all the class functions are going to be publicly accessible, regardless of their permissions?
Is there perhaps a compile/project option that I need to set to enforce privacy permissions in the compiled DLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,编译器依赖 .h 文件与类定义的完整性来强制执行类成员的隐私。如果有人修改了类定义,加载器将非常乐意链接这些私有函数。
Yes, the compiler depends on the integrity of the .h file with the class definition to enforce the privacy of class members. If someone modifies the class definition, the loader will be more than happy to link those private functions.
访问说明符是一个纯粹的编译器前端功能。它们不会出现在生成的代码中。
为了在代码中使用您的类,用户需要类定义,通常以一个或多个
.h
文件的形式提供。访问说明符就在那里,它阻止类的用户访问私有部分 - 除非他们修改标头。您的类必须导出所有成员函数,甚至是私有函数,因为它们可以从公共内联函数中引用。内联函数的代码被扩展为调用者的代码。因此,在机器代码级别,您的类的用户实际上可能必须能够调用私有函数。
Access specifiers are a pure compiler front-end feature. They won't show up in generated code.
In order to use your class in their code, users need the class definition, usually provided in form of one or more
.h
files. Access specifiers are in there, which prevents users of the class to access private parts - unless they modify the header.Your class has to export all member functions, even the private ones, because they could be referenced from public inlined functions. The code of inlined functions is expanded into the caller's code. So, at the machine code level, users of your class might actually have to be able to call private functions.
否 - 只有使用 __declspec(dllexport) 导出的类的公共成员从外部可见。
http://msdn.microsoft.com/en-us/library/a90k134d.aspx
No - only public members of a class exported using
__declspec(dllexport)
will be visible from outside.http://msdn.microsoft.com/en-us/library/a90k134d.aspx