运行函数时检查失败#0

Run-Time Check Failure #0 when running a function

本文关键字:失败 检查 函数 运行      更新时间:2023-10-16

我得到这个错误时,试图使用这个函数

 void WSPAPI GetLspGuid( LPGUID lpGuid )
 {
    memcpy( lpGuid, &gProviderGuid, sizeof( GUID ) );
 }
误差

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

函数通过

调用
HMODULE         hMod = NULL;
LPFN_GETLSPGUID fnGetLspGuid = NULL;
int             retval = SOCKET_ERROR;
// Load teh library
hMod = LoadLibraryA( LspPath );
if ( NULL == hMod )
{
    fprintf( stderr, "RetrieveLspGuid: LoadLibraryA failed: %dn", GetLastError() );
    goto cleanup;
}
// Get a pointer to the LSPs GetLspGuid function
fnGetLspGuid = (LPFN_GETLSPGUID) GetProcAddress( hMod, "GetLspGuid" );
if ( NULL == fnGetLspGuid )
{
    fprintf( stderr, "RetrieveLspGuid: GetProcAddress failed: %dn", GetLastError() );
    goto cleanup;
}
// Retrieve the LSPs GUID
fnGetLspGuid( Guid );

此运行时检查防止函数声明与实际定义之间的不匹配。将代码编译成静态库或DLL时可能发生的意外。常见的不匹配是调用约定或传递的参数的数量或类型。

非常合适,您有一个名为WSPAPI的宏来声明调用约定。它通常展开为__cdecl或__stdcall,通常偏向于__stdcall。所以很有可能这个宏在客户端代码中有错误的值。如果您不知道如何正确设置该宏,请向库作者寻求帮助。


编辑后:附加的失败模式,你正在加载错误版本的DLL。并且LPFN_GETLSPGUID函数指针声明是错误的,缺少WSPAPI宏。我把钱压在那上面,尤其是因为我看不见它。


注释后,信息慢慢流入:

定义为typepedef void (*LPFN_GETLSPGUID) (GUID *lpGuid);

错了,应该是

typedef void (WSPAPI * LPFN_GETLSPGUID)(GUID *lpGuid);

如果没有可用的宏(不太可能),则用__stdcall代替WSPAPI。