调用DLL函数,出现错误

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.

错误原因:

你定义函数指针原型时出错。

其实你定义的没有错,但是编译器不认识而已,因为你调用的dll函数是一个远函数,而且是一个C函数,你得告诉编译器它是个c函数才行。那么你就可以在定义该函数的时候加上一句话,

FAR PASCAL 或者 __stdcall 这个就OK了。

具体做法:

比如说你要定义一个 返回类型为空,参数为空的函数指针:

typedef void (*LPFUN)(void);

这样确实跟我们dll里的函数匹配了,上面也说了,我们应该添上几个字,告诉编译器这个是一个远的C函数。

typedef void (WINAPI *LPFUN)(void);

typedef void (__stdcall *LPFUN)(void);

typedef void (FAR PASCAL *LPFUN) (void);

像上面这样定义就OK了,如果用的是VC++,那么直接用第一种定义就ok了。

注意,上面是使用 MFC (DLL)的做法。

如果是WIN32 DLL,得相应的去掉WINAPI ,__stdcall ,FAR PASCAL 这几个参数。因为WIN32 DLL 默认的入栈方式为 __cedcall方式,不是__stdcall方式。

具体的组合方式太多了,反正知道错误的原因是声明相应的函数未匹配就行了。

调用DLL里的函数 或 类成员函数 碰到此错误:

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.

函数定义的调用规则,和实际的调用规则不同。如 编译器默认的是__cdecl,而__stdcall 类型的函数却用了 __cdecl 的调用规则,由于编译时不会报错,结果出现了运行时异常。

所以把在函数定义中进行设置调用规则即可解决此问题。

如: typedef void (__stdcall Foo)(int a);

很久没写代码,一天几行的代码:

typedef int ( *PFUN)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

void CTestProcessMonitorDlg::OnBnClickedButton1()

{

// TODO: Add your control notification handler code here

//MessageBox(TEXT("Hello"), TEXT("Test"));

//typedef void (*pfv) ();

HMODULE hmod = ::LoadLibraryExW(TEXT("user32.dll"), NULL, 0);
if (hmod != NULL)
{
PFUN pFun= (PFUN)GetProcAddress(hmod, "MessageBoxW");
if (pFun != NULL)
{
pFun(m_hWnd, TEXT("Hello"), TEXT("Test"), MB_YESNO);
} :FreeLibrary(hmod);
}

}

突然出现以下的错误:

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.

代码改为如下, 则问题没有了

typedef int (WINAPI *PFUN)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

void CTestProcessMonitorDlg::OnBnClickedButton1()

{

// TODO: Add your control notification handler code here

//MessageBox(TEXT("Hello"), TEXT("Test"));

//typedef void (*pfv) ();

HMODULE hmod = ::LoadLibraryExW(TEXT("user32.dll"), NULL, 0);
if (hmod != NULL)
{
PFUN pFun= (PFUN)GetProcAddress(hmod, "MessageBoxW");
if (pFun != NULL)
{
pFun(m_hWnd, TEXT("Hello"), TEXT("Test"), MB_YESNO);
} ::FreeLibrary(hmod);
}

}

Run-Time Check Failure #0,The value of ESP was not properly saved 错误解决的更多相关文章

  1. C++程序在debug模式下遇到Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call问题。

    今天遇到一个Access Violation的crash,只看crash call stack没有找到更多的线索,于是在debug模式下又跑了一遍,遇到了如下的一个debug的错误提示框: 这个是什么 ...

  2. Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. 调用函数约定不同

    Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is ...

  3. 【Dll】Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call

    [问题说明]调试动态库导出的函数时遇到的问题 [解决方法]要么加上__stdcall,对应__stdcall:要么去掉__stdcall,对应_cdecl

  4. Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call错误

    我这边新增的接口之后编译,启动debug后提示这个问题, 在网上找了一段时间,感觉各大神说的都好有道理,但是没有作用 so,尝试对整个工程重新编译(理论上只要重新编译修改的文件影响到的地方)

  5. VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

    调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function c ...

  6. Run-Time Check Failure #0

    Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is ...

  7. windows CE 6.0编译报BLDDEMO: There were errors building MY283错误解决办法

    今天开始正式进入windows ce程序开发. 第一次编译windows ce6.0的系统,25分钟编译后报:BLDDEMO: There were errors building MY283 错误. ...

  8. Run-Time Check Failure #2 - Stack around the variable 'ucPriKey' was corrupt

    Run-Time    Check    Failure    #2        一般是栈被破坏,你的代码可能有缓冲区溢出一类的问题. Run-Time Check Failure #2 - Sta ...

  9. VS2008中Run-Time Check Failure #2 - Stack around the variable 'xxx' was corrupted 错误解决方法

    问题 : 在用VS2008写一段代码,算法都没有问题,但是调试的时候发现出了main之后就报 Stack around the variable 'xxx' was corrupted 的错误,后来发 ...

随机推荐

  1. 设计模式之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式的由来 所有设计模式都是为解决某类问题而产生的,那么简单工厂模式是为解决什么问题呢?我们假设有以下业务场景: 在一个学生选课系统中,文科生用户选课时,我们要获得文科生的所有课程列表:理 ...

  2. php伪协议分析与CTF例题讲解

                本文大量转载于:https://blog.csdn.net/qq_41289254/article/details/81388343 (感谢博主) 一,php://  访问输入 ...

  3. Balanced Diet Gym - 102220B

    题目链接:https://vjudge.net/problem/Gym-102220B 题意:每组数据 给了 N和M表示有M种类型的糖果,这些糖果一共N个.接下了是 M 组数据,表示如果你选第 i 中 ...

  4. python3 int() 各数据类型转int

    print(int('0b1010',0))#二进制数print(int('0xa',0))#十六进制数print(int('0xa',16))print(int('a',16))print(int( ...

  5. Go 语言入门教程,共32讲,6小时(已完结)

    Go语言入门教程,共32讲,6小时(完结) 在B站:https://www.bilibili.com/video/BV1fD4y1m7TD/

  6. ethtool - 命令

    ethtool 导览:     1. 如何查看 Linux 中可用的网卡接口     2. 如何查看 Linux 中网卡信息     3. 如何查看网卡驱动版本以及硬件版本     4. 如何查看网络 ...

  7. 创建ortools的Dockerfile

    技术背景 基于已有的Docker容器镜像,去创建一个本地的镜像,有两种方法:一种是在之前的博客中提到过的,使用docker commit的方案,也就是先进去基础系统镜像内部完成所需的修改,然后comm ...

  8. Flutter原理简介

    Flutter 是怎么运转的? 与用于构建移动应用程序的其他大多数框架不同,Flutter 是重写了一整套包括底层渲染逻辑和上层开发语言的完整解决方案.这样不仅可以保证视图渲染在 Android 和 ...

  9. 02_利用numpy解决线性回归问题

    02_利用numpy解决线性回归问题 目录 一.引言 二.线性回归简单介绍 2.1 线性回归三要素 2.2 损失函数 2.3 梯度下降 三.解决线性回归问题的五个步骤 四.利用Numpy实战解决线性回 ...

  10. django-数据库的增删改查操作

    django-数据库的增删改查操作 1.添加用户记录 def login(request): 增加用户记录 使用save()方法来增加记录 username = UserInfo(username=' ...