简体   繁体   English

带有LoadLibrary的STATUS_STACK_BUFFER_OVERRUN

[英]STATUS_STACK_BUFFER_OVERRUN with LoadLibrary

When I load iphlpapi.dll with LoadLibrary my stack buffer overrun! 当我用LoadLibrary加载iphlpapi.dll时,我的堆栈缓冲区溢出了! 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! lib函数的加载和执行工作正常,但有时我的程序抛出STATUS_STACK_BUFFER_OVERRUN异常! (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! 当我不使用LoadLibrary和GetProcAddress(静态绑定)时->没有缓冲区溢出!

Thanks and greets, 谢谢并打招呼,

leon22 利昂22

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 . VS中的默认调用约定是__cdecl ,Windows API需要__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. 这些在处理参数堆栈的方式上有所不同,最值得注意的是__cdecl需要清除调用方,而__stdcall需要清除被调用函数。

WINAPI is defined as __stdcall WINAPI被定义为__stdcall

See eg Calling Conventions Demystified 参见例如, 神秘的呼叫惯例

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.. 检查是否不需要在函数原型中使用__stdcall或其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM