LoadLibrary 的 STATUS_STACK_BUFFER_OVERRUN
当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要指定调用约定:
VS中默认的调用约定是
__cdecl
,Windows API需要__stdcall
。它们的不同之处在于参数堆栈的处理方式,最明显的是 __cdecl 需要调用者进行清理,而 __stdcall 则需要被调用函数进行清理。WINAPI
定义为__stdcall
参见例如 揭秘调用约定
You need to specify calling convention:
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
我的第一个猜测是,您对库的函数使用了错误的调用约定,这可能会导致堆栈损坏(以及其他奇怪的问题,这些问题可能仅在调用后稍后才会出现)。
检查您是否不需要在函数原型中使用 __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..