包装类的DLL函数错误

时间:2014-09-29 17:25:44

标签: c++ dll scope stack activation

我有第三方.dll(和相对的.h和.lib)通过USB控制设备。

我想将dll函数用于一个类(AOTF_controller)来实现我想要的行为。 我想做的是:

  1. 连接到设备(connect()类功能);
  2. 初始化它(init()类函数);
  3. 设置一些参数(setScanningFreq()类函数)
  4. 按顺序增加设备的频率(increaseFreq()类功能)
  5. 重置并关闭USB连接。
  6. 当我将dll函数直接用于_tmain()时,我可以获得此行为,因此设备可以正常工作,但是当我将dll函数包装到一个类中并尝试使用该类出错时。 我重复上面的过程(列表项1-5)几次:有时它工作正常,有时程序停止,调试器给我这个错误:

    AOTFcontrollerDebug.exe中0x77533fb7处的第一次机会异常:0xC0150014:正在运行的执行线程的激活上下文激活堆栈已损坏。

    错误似乎是随机的,有时我可以毫无问题地结束扫描80次,有时它会在第一次扫描时给我错误。 我试图搜索该错误,但我找不到任何有用的东西。

    任何人都可以提供帮助?我想可能与我的课程中如何调用dll函数有关?

    这是主要的功能代码:

    // AOTFcontrollerDebug.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include "AOTFcontrollerDebug.h"
    #include "AOTF_Controller.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <AotfLibrary.h>
    
    #define DEFAULT_STARTFREQUENCY 78
    #define DEFAULT_ENDFREQUENCY 95.5
    #define DEFAULT_NUMOFFRAMES 256
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    // The one and only application object
    
    CWinApp theApp;
    
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        int nRetCode = 0;
    
        HMODULE hModule = ::GetModuleHandle(NULL);
    
        if (hModule != NULL)
        {
            // initialize MFC and print and error on failure
            if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
            {
                // TODO: change error code to suit your needs
                _tprintf(_T("Fatal Error: MFC initialization failed\n"));
                nRetCode = 1;
            }
            else
            {
                // TODO: code your application's behavior here.
                std::cout << "-----AOTF Controller Debugging-----"<<endl;
    
                //input of scans to do
                int repeatedScan;
                std::cout << "Type how many repeated scan: "<<endl;
                std::cin >> repeatedScan;
    
                //instance  
                AOTF_Controller m_AOTF_Controller;
                std::cout << "AOTF Controller instance done..."<<endl;
    
                //loop over scans
                for(int i =0;i<repeatedScan;i++)
                {
                    m_AOTF_Controller.connect();
                    std::cout << "AOTF Controller connected..."<<endl;
                    std::cout << "Scan number : "<< (i + 1) <<endl;
    
                    m_AOTF_Controller.init();
                    //set the delta freq to increase at each step
                    m_AOTF_Controller.setScanningFreq(DEFAULT_STARTFREQUENCY, DEFAULT_ENDFREQUENCY, DEFAULT_NUMOFFRAMES);
    
                    //loop over wavelengths
                    int sleep_ms = 4;
                    for (int j =0; j <DEFAULT_NUMOFFRAMES; j++)
                    {
                        Sleep(sleep_ms) ;
                        m_AOTF_Controller.increaseFreq();
                        //std::cout << " "<< (j + 1) ;
                    }
    
                    // terminate scans
                    m_AOTF_Controller.reset();
                    m_AOTF_Controller.disconnect();
                    std::cout << endl <<"Scan number "<< (i + 1) << "terminated successfully" <<endl;
    
                    Sleep(sleep_ms*100) ;
                }
            }       
        }
        else
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
            nRetCode = 1;
        }
    
        return nRetCode;
    }
    

    这里是Aotf_Controller类代码:

    //Aotf_Controller.h
    #pragma once
    
    #include <AotfLibrary.h>
    #include <string> 
    #include <sstream>
    #include <iomanip>
    
    #define CONVERSION_MHZ_TO_HZ 1000000
    
    class AOTF_Controller
    {
    private:
    enum Error {SUCCESSFUL , CONNECTION_ERROR, DISCONNECTION_ERROR, INIT_ERROR, RESET_ERROR ,    SET_ERROR }; // error enum values
    
    HANDLE  hAotfController;
    int currentGain;
    long currentFreq; // current frequency in Hz
    long startFreq, endFreq, deltaFreq; // frequency values for the scanning in Hz
    
    public:
        AOTF_Controller(void);
        ~AOTF_Controller(void);
        AOTF_Controller::Error connect();
        AOTF_Controller::Error disconnect();
        AOTF_Controller::Error init();
        AOTF_Controller::Error setFreq(float freq); // for frequency value in MHZ (precision to the 3rd decimal i.e. KHz)
        AOTF_Controller::Error setFreq(long freq); // for frequency value in Hz 
        AOTF_Controller::Error setGain(int gain);
        AOTF_Controller::Error reset();
        AOTF_Controller::Error setScanningFreq(float _startFreq, float _endFreq, int numOfFrames);
        AOTF_Controller::Error increaseFreq();
        };
    
    //Aotf_Controller.cpp
    
    #include "StdAfx.h"
    #include "AOTF_Controller.h"
    
    AOTF_Controller::AOTF_Controller(void)
    {
        currentGain = 0;
        currentFreq = 0; 
        startFreq = 0;
        endFreq = 0;
        deltaFreq = 0;
        hAotfController = NULL;
    }
    
    AOTF_Controller::~AOTF_Controller(void)
    {
    }
    
    AOTF_Controller::Error AOTF_Controller::connect()
    {
        int iInstance = 0;
        hAotfController = AotfOpen(iInstance);
    
        if (!hAotfController)
        {
            return CONNECTION_ERROR;
        }
        else
        {
            return SUCCESSFUL;
        }
    }
    
    AOTF_Controller::Error AOTF_Controller::disconnect()
    {
        if (!AotfClose (hAotfController))
        {
            return DISCONNECTION_ERROR;
        }
        else
        {
            hAotfController = NULL;
            return SUCCESSFUL;
        }
    }
    
    AOTF_Controller::Error AOTF_Controller::init()
    {
        std::string ampCom="dds a 0 16383\r"; //Command to set the amplitude parameter to the max
        std::string modCom="mod dac * 16383\r";//Command to set the dac parameter to the max
        int gain = 255; // set the gain to the max
    
        if (!AotfWrite(hAotfController, ampCom.length(), (char *)ampCom.c_str()))
        {
            return Error::INIT_ERROR;
        }
    
        if (!AotfWrite(hAotfController, modCom.length(), (char *)modCom.c_str()))
        {
            return INIT_ERROR;
        }
    
        setGain(gain);
    
        return SUCCESSFUL;
    }
    
    AOTF_Controller::Error AOTF_Controller::reset()
    {
        std::string resetCom = "dds reset\r";
    
        if(!AotfWrite(hAotfController, resetCom.length() , (char *)resetCom.c_str())) 
        {
            return RESET_ERROR;
        } 
        return SUCCESSFUL;
    }
    
    AOTF_Controller::Error AOTF_Controller::setFreq(float freq)
    {
        long freqHz = (long)freq*CONVERSION_MHZ_TO_HZ;
        setFreq(freqHz);
        return SUCCESSFUL;
    }
    
    AOTF_Controller::Error AOTF_Controller::setFreq(long freq)
    {
        std::ostringstream oss; //stream to build the string
    
        //building the string for the Frequency 
        oss << "dds f 0 !" << std::fixed << std::setprecision(0) << freq << "\r";
        std::string freqCom = oss.str();
    
        //send command to the AOTF
        if(!AotfWrite(hAotfController, freqCom.length(), (char *) freqCom.c_str())) // set the frequency (80-120)
        {
            return SET_ERROR;
        } 
    
        currentFreq = freq; // update monitoring variable in HZ
    
        return Error::SUCCESSFUL;
    }
    
    AOTF_Controller::Error AOTF_Controller::setGain(int gain)
    {
        std::ostringstream oss; //stream to build the string
    
        //building the string for the Gain
        oss << "dds gain -p* * " << gain << "\r";
        std::string gainCom = oss.str();
    
        //send command to the AOTF
        if(!AotfWrite(hAotfController, gainCom.length(), (char * )gainCom.c_str())) // set the gain (0-255)
        {
            return SET_ERROR;
        } 
    
        currentGain = gain;
    
        return SUCCESSFUL;
    }
    
    AOTF_Controller::Error AOTF_Controller::setScanningFreq(float _startFreq, float _endFreq, int numOfFrames)
    {
        float FreqRange = (_endFreq - _startFreq); //calculate range 
    
        //calculate DeltaFrequency (frequency increase after each captured frame)
        deltaFreq = (long) ((FreqRange/(float)(numOfFrames-1))*(float)CONVERSION_MHZ_TO_HZ); //conversion from MHz to Hz
    
        startFreq = (long) (_startFreq*CONVERSION_MHZ_TO_HZ);
        endFreq = (long) (_endFreq*CONVERSION_MHZ_TO_HZ);
    
        setFreq(_startFreq);
    
        return SUCCESSFUL;
    }
    
    AOTF_Controller::Error AOTF_Controller::increaseFreq()
    {
        if (deltaFreq ==0)
        {
            return SET_ERROR;
        }
    
        long newFreq = currentFreq + deltaFreq;
    
        std::ostringstream oss;
        oss << "dds f 0 !" << std::fixed << std::setprecision(0) << newFreq << "\r";
        std::string freqCom = oss.str();
    
        //send command to the AOTF
        if(!AotfWrite(hAotfController, freqCom.length(), (char *)freqCom.c_str())) // set the frequency (80-120)value 
        {
            return SET_ERROR;
        }
    
        currentFreq = newFreq;
        return SUCCESSFUL;
    }
    

0 个答案:

没有答案