Question

I have written MFC dll having 3 methods are exported. I have declared class object as global and initialized it in first method then second and third method use and process it. Issue is that class obeject's value is not getting persisting throughout the file. when second or third method gets call from C# client application, class object value is getting NULL.

Could anybody tell me why this is happening! I have tried this same scnaerio in another application but this issue is not reproduced.

Code: Interface File:

#include "StdAfx.h"
#define DLLEXPORT __declspec(dllexport)

using namespace nsAnalyzer;
static CWindowsAnalyzer *pWindowsAnalyzer = NULL;

extern "C" DLLEXPORT void Init( const wchar_t       *sCurrentUserDataDir,
                                const wchar_t       *sMachineName,
                                const wchar_t       *sMacId )
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    try
    {
        nsAnalyzer::CWindowsAnalyzer *pWindowsAnalyzer = new CWindowsAnalyzer(  CString(sCurrentUserDataDir),
                                                                                CString(sMachineName),
                                                                                CString(sMacId) );

        if(pWindowsAnalyzer)
        {
            pWindowsAnalyzer->Init();
        }
    }
    catch(const std::exception& e)
    {
        cout<<"Error: Exception occured in Init: "<<e.what()<<endl;
    }
}

extern "C" DLLEXPORT bool Analyze()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    bool bResult = false;

    try
    {
        if(pWindowsAnalyzer->ConsolidateRawActivities())
        {
            cout<<"ConsolidateRawActivities succeed"<<endl;
            bResult = true;
        }
        else
        {
            cout<<"ConsolidateRawActivities failed"<<endl;
            bResult = false;
        }
    }
    catch(const std::exception& e)
    {
        cout<<"Error: Exception occured in Analyze: "<<e.what()<<endl;
    }

    return bResult;
}

extern "C" DLLEXPORT void Dispose()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    try
    {
        // Disponse windows analyzer
        if(pWindowsAnalyzer)
        {
            delete pWindowsAnalyzer;
        }

        // Dispose Logger
        CLogger::DisposeInstance();
    }
    catch(const std::exception& e)
    {
        cout<<"Error: Exception occured in Dispose: "<<e.what()<<endl;
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top