gpt4 book ai didi

c - 我的 dll 在未经我同意的情况下导出所有功能!

转载 作者:行者123 更新时间:2023-12-04 06:34:03 25 4
gpt4 key购买 nike

我正在使用 mingw 编译器和代码块 IDE。我面临一个问题,我不明白我的 dll 是如何在没有我的情况下导出每个函数的,即使使用 __declspec(dllexport)有了这些功能。这是两个示例文件,main.hmain.cpp :
main.h :

#ifndef __MAIN_H__
#define __MAIN_H__
#include windows.h

#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C"
{
#endif

void SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__
main.cpp :
#include "main.h"


void SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL fxn Message", MB_OK | MB_ICONINFORMATION);
}

bool flag = false;

extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}

flag = true;

return TRUE; // succesful
}

在本例中, SomeFunction已导出,我可以从外部应用程序动态调用它,尽管我没有将其原型(prototype)化为
void __declspec(dllexport) SomeFunction(const LPSTR sometext);

不仅如此,就连全局变量 flag在自动生成的 .def 文件中导出。

这里发生了什么?如果我犯了技术错误,请帮助并纠正我。

最佳答案

MinGW 链接器将所有符号公开,除非您将它们默认隐藏。这可以从 GCC 手册页中被覆盖:

-fvisibility=default|internal|hidden|protected

...
A good explanation of the benefits offered by ensuring ELF symbols
have the correct visibility is given by "How To Write Shared
Libraries" by Ulrich Drepper (which can be found at
<http://people.redhat.com/~drepper/>)---however a superior solution
made possible by this option to marking things hidden when the
default is public is to make the default hidden and mark things
public. This is the norm with DLL's on Windows and with
-fvisibility=hidden and "__attribute__ ((visibility("default")))"
instead of "__declspec(dllexport)" you get almost identical
semantics with identical syntax. This is a great boon to those
working with cross-platform projects.

关于c - 我的 dll 在未经我同意的情况下导出所有功能!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5040342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com