LoadLibrary 的 STATUS_STACK_BUFFER_OVERRUN

发布于 2024-10-21 02:59:26 字数 851 浏览 7 评论 0原文

当我使用 LoadLibrary 加载 iphlpapi.dll 时,我的堆栈缓冲区溢出!我该如何解决 这个问题?!

typedef DWORD (*GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG);   
GetExtendedTcpTable _GetExtendedTcpTable;

// load function at runtime 
HINSTANCE hstLibrary = LoadLibrary("C:\\Windows\\System32\\Iphlpapi.dll");

if(!hstLibrary)
{
    ::MessageBox(NULL,"Can't load Iphlpapi.dll!\n","Error",
            MB_OK + MB_ICONEXCLAMATION + MB_TASKMODAL);

    FreeLibrary(hstLibrary); // free memory

    exit(0);
}

// load function address from dll
_GetExtendedTcpTable = (GetExtendedTcpTable)GetProcAddress(hstLibrary, "GetExtendedTcpTable");

lib 函数的加载和执行工作正常,但在某些时候我的程序会抛出 STATUS_STACK_BUFFER_OVERRUN 异常! (某些点:当我注释字符串操作时,错误会在几行后发生)

当我不使用 LoadLibrary 和 GetProcAddress(静态绑定)时 ->没有缓冲区溢出!

谢谢并问候,

leon22

When I load iphlpapi.dll with LoadLibrary my stack buffer overrun! How can I solve
this problem?!

typedef DWORD (*GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG);   
GetExtendedTcpTable _GetExtendedTcpTable;

// load function at runtime 
HINSTANCE hstLibrary = LoadLibrary("C:\\Windows\\System32\\Iphlpapi.dll");

if(!hstLibrary)
{
    ::MessageBox(NULL,"Can't load Iphlpapi.dll!\n","Error",
            MB_OK + MB_ICONEXCLAMATION + MB_TASKMODAL);

    FreeLibrary(hstLibrary); // free memory

    exit(0);
}

// load function address from dll
_GetExtendedTcpTable = (GetExtendedTcpTable)GetProcAddress(hstLibrary, "GetExtendedTcpTable");

The loading of the lib function and executing is working fine but at some point my program throws the STATUS_STACK_BUFFER_OVERRUN exception!
(some point: when I comment the string operation the error occur few lines later)

When I don't use LoadLibrary and GetProcAddress(static binding) -> no buffer overrun!

Thanks and greets,

leon22

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笔绘流年 2024-10-28 02:59:26

您需要指定调用约定:

typedef DWORD (WINAPI * GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG);   

VS中默认的调用约定是__cdecl,Windows API需要__stdcall。它们的不同之处在于参数堆栈的处理方式,最明显的是 __cdecl 需要调用者进行清理,而 __stdcall 则需要被调用函数进行清理。

WINAPI 定义为 __stdcall

参见例如 揭秘调用约定

You need to specify calling convention:

typedef DWORD (WINAPI * GetExtendedTcpTable)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG);   

The default calling convention in VS is __cdecl, Windows API requires __stdcall. These differ in how the stack for arguments is handled, most notably __cdecl requires the caller to clean up whereas __stdcall requires the called function to clean up.

WINAPI is defined as __stdcall

See e.g. Calling Conventions Demystified

笑脸一如从前 2024-10-28 02:59:26

我的第一个猜测是,您对库的函数使用了错误的调用约定,这可能会导致堆栈损坏(以及其他奇怪的问题,这些问题可能仅在调用后稍后才会出现)。
检查您是否不需要在函数原型中使用 __stdcall 或其他内容。

My first guess is that you are using the wrong calling convention for the function of the library which can then lead to stack corruptions (among other strange problems that may show up only later, after the call was made).
Check if you don't need to used __stdcall or something else in your function prototype..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文