0

I made a .dll file with QT and I load it in my application. When it's about to return from one function, I receive:

The value of ESP was not properly saved across a function call

I start with the DLL project:

This is my device_manager_interface.hpp:

#ifndef __DEVICE_MANAGER_INTERFACE_HPP__
#define __DEVICE_MANAGER_INTERFACE_HPP__

#include <QtCore>

class DeviceManagerInterface
{
public:
    virtual BCR * getDeviceBCR() = 0 ;
    .
    .
    .     
};

QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(DeviceManagerInterface,"some_info");
QT_END_NAMESPACE

#endif //__DEVICE_MANAGER_INTERFACE_HPP__

This my device_manager.hpp:

#ifndef __DEVICE_MANAGER_BASE_HPP__
#define __DEVICE_MANAGER_BASE_HPP__

#include "device_manager_interface.hpp"

class DeviceManager : public DeviceManagerInterface
{
public:
    DeviceManager();
    virtual BCR * getDeviceBCR();
    .
    .
    .
 protected:
    virtual void initilzeAvailableDevices(DeviceList device_list);
    virtual WORD startup();
    .       
    . 
    .    
};

#endif //__DEVICE_MANAGER_BASE_HPP__

This is my device_manager.cpp:

#include "device_manager.hpp"

DeviceManager::DeviceManager()
{

}

void WINAPI DeviceManager::initilzeAvailableDevices(DeviceList device_list)
{
    WORD wfs_version = startup();
    .
    .
    .
}

WORD DeviceManager::startup()
{
    WFSVERSION wfs_version;
    HRESULT hRes;

    hRes = WFSStartUp(SUPPORTED_VERSIONS, &wfs_version);

    WORD version = wfs_version.wVersion;

    return version;
}

WFSStartUp is function containing in xfsapi.h and defined like this.

HRESULT extern WINAPI WFSStartUp ( DWORD dwVersionsRequired, LPWFSVERSION lpWFSVersion);

This is my device_manager_impl.hpp:

#ifndef __DEVICE_MANAGER_WINCORE_HPP__
#define __DEVICE_MANAGER_WINCORE_HPP__

#include "device_manager.hpp"

class DeviceManagerImpl : public QObject, DeviceManager
{
    Q_OBJECT
    Q_INTERFACES(DeviceManagerInterface)
public:
    DeviceManagerImpl();

protected:
    .
    .
    .
};

#endif //__DEVICE_MANAGER_WINCORE_HPP__

This is my device_manager_impl.cpp:

#include "device_manager_impl.hpp"

#include "xfsapi.h"

#define BRAND_NAME "WINCORE"

DeviceManagerImpl::DeviceManagerImpl()
{
    m_brand_name = BRAND_NAME;
    fill_map_logical_names();
    DeviceList device_list = detectAvailableDevices();
    DeviceManager::initilzeAvailableDevices(device_list);
}

Q_EXPORT_PLUGIN2(device_manager_impl, DeviceManagerImpl);

This project provides a DLL file called WINCORE.dll.

This is how I load this .dll:

QPluginLoader* pluginLoader = new QPluginLoader(filename);

QObject *plugin = pluginLoader->instance();

The filename contains the WINCORE.dll path and it's correct. My problem is startup() in the DeviceManager class. When it wants to return version, I get the error. What am I doing wrong?

Pang
  • 9,564
  • 146
  • 81
  • 122
M.H.
  • 223
  • 3
  • 10
  • 23
  • 1
    The problem seems to be caused by the use of different calling conventions. Why is DeviceManager::initilzeAvailableDevices defined to have WINAPI calling convention. The declaration of the method does not reflect this. Is this really necessary? – pag3faul7 Sep 04 '13 at 14:07
  • possible duplicate of [Weird MSC 8.0 error: "The value of ESP was not properly saved across a function call..."](http://stackoverflow.com/questions/142644/weird-msc-8-0-error-the-value-of-esp-was-not-properly-saved-across-a-function) – sashoalm Sep 05 '13 at 09:11
  • @pag3faul7, I was testing all way i found out to fix this error and thats why i added WINAPI to DeviceManager::initilzeAvailableDevices , Even without it i still have the same problem – M.H. Sep 07 '13 at 03:39

1 Answers1

2

I finally found how to fix this problem!

Right click on your Project , Choose Properties , Go to Configuration Properties , Select C/C++ then select Code Generation .

Change Basic Run Time Check to Default.

Change Struct Member Alliance to 1 Byte (/Zp1) .

I really hope this work for you guys too.

Best Regards

M.H.
  • 223
  • 3
  • 10
  • 23
  • I very much doubt this was the solution to your problem. The "Basic Run Time Check" you mention is only turned on in debug mode. So what you've done there is tantamount to simply running in release mode and ignoring the debug assertion. It's possible that your code will crash in a less obvious place in release mode. – Benj Jan 12 '16 at 09:54