Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
LoadLibrary nullify the mainframe object. This code works fine in VS2010.

//Code for header file..
#if !defined(AFX_SIRRETVALUE_H__EF8A65E0_26FD_11D7_BA6A_4000040D08A6__INCLUDED_)
#define AFX_SIRRETVALUE_H__EF8A65E0_26FD_11D7_BA6A_4000040D08A6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"		// main symbols
class CSIRRETVALUEApp : public CWinApp
{
public:
	CSIRRETVALUEApp();
	DECLARE_MESSAGE_MAP()
};

#endif // !defined(AFX_SIRRETVALUE_H__EF8A65E0_26FD_11D7_BA6A_4000040D08A6__INCLUDED_)

***************************************************************************************************************************************************************


//DLL Code

#include "stdafx.h"
#include "SiRValue.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CSIRRETVALUEApp, CWinApp)
	//{{AFX_MSG_MAP(CSiRValueApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CSIRRETVALUEApp::CSIRRETVALUEApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


CSIRRETVALUEApp theApp;

extern "C" __declspec( dllexport )
String *FireRule( CObList& list )
{
	AFX_MAINTAIN_STATE2 *pState = new AFX_MAINTAIN_STATE2(AfxGetStaticModuleState());

	return (CString*)"Test";

} // End of FireRule function


***************************************************************************************************************************************************************************************


//Calling code written in MFC program.....
ConvFrameWnd* pFrame1 = (ConvFrameWnd*)AfxGetApp()->m_pMainWnd;
	HINSTANCE         hModule;	
	hModule = LoadLibrary("SiRValue.Dll");

	ConvFrameWnd* pFrame = (ConvFrameWnd*)AfxGetApp()->m_pMainWnd;


What I have tried:

In the calling(mfc)program , pFrame1 is initialized correctly.hModule is also showing me value(not null). But pFRame object is null just after loadlibrary. I am not able to understand the reasaon , why pFrame is null.
Posted
Updated 14-Jul-20 3:05am
Comments
Richard MacCutchan 14-Jul-20 8:58am    
You are creating a new application object in the dll which is probably causing problems. Your dll code looks more like a main program, I cannot see any purpose to it being built as a dll.
[no name] 15-Aug-20 15:54pm    
MFC DLLs are actually required to be a CWinApp derived class.
Richard MacCutchan 16-Aug-20 4:26am    
It's a long timed since I used MFC so things have obviously changed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900