Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is the code of DLL (Compiled in VS2019 v142, SDK 10,Debug mode) compiled successfully. Loading this DLL using LoadLibrary()in MFC program(Compiled in VS2019 v142 , Sdk 10,Debug Mode). Both code works fine in VS2010. But not woking in VS2017.

#include "stdafx.h"
#include "SiTempJCL.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;


	// TODO: add construction code here,
	// Place all significant initialization in InitInstance

SiJCLTemplateDfltApp theApp;

extern "C" __declspec( dllexport )

CSResult* FireRule(CObList& list)
	AFX_MAINTAIN_STATE2 *pState = new AFX_MAINTAIN_STATE2(AfxGetStaticModuleState());
		CString *sJobTemplate;
		CString sDBUseCode="M";
		if((sDBUseCode == "M") || (sDBUseCode == "P")) {
			sJobTemplate = new CString("JOBM");
		else {
			sJobTemplate = new CString("JOBC");

		// Return JCL job template id
		CSResult *ruleRet = new CSResult();
		ruleRet->SetReturnObject((CObject *) sJobTemplate);

		delete pState;
		return ruleRet;
	catch (...)
		delete pState;

What I have tried:

Code for Calling above DLL:
HINSTANCE         hModule;
hModule = LoadLibrary("SiJCLTemplate.dll");

SiLauncherApp* pSiBatchLauncherApp1 = (SiLauncherApp*)AfxGetApp();

I have checked hModule is not null, loading the dll and calling firerule function.
but i think it is not loading properly. as when i check
object after loadlibrary, it shows me
{SiJCLTemplateDfltApp.dll!SiJCLTemplateDfltApp theApp<NULL>};

I am assuming there is some problem in DLL code . may be little change is required which is not supportive in MS2017.

Please guide.
Updated 13-Jul-20 20:00pm

The only thing I see is that your dll is missing its DllMain function, as described at DllMain entry point (Process.h) - Win32 apps | Microsoft Docs[^].
Share this answer
Shao Voon Wong 14-Jul-20 3:44am    
Richard MacCutchan 14-Jul-20 4:12am    
Although from further comments it appears that this is not necessary. Maybe it is added automatically by MFC.
CString is not derived from CObject so you cannot cast its pointer to CObject*.
CString* p_str = new CString(L"Hello");
CObject* p_obj = p_str;

I got this error from VC++ 2019:
error C2440: 'initializing': cannot convert from 'CString *' to 'CObject *'

Highly probable, CString used to inherit from CObject in older VC++ but not now.
Share this answer
Member 13261094 14-Jul-20 2:02am    
Not getting any compilation error. It loading library and executing the dll function also. Only thing It corrupts the pSiBatchLauncherApp1 object.
Shao Voon Wong 14-Jul-20 3:19am    
It is because your C-style cast silence the compiler. No compilation error for the code below. This is why C-style cast is discouraged.

CString* p_str = new CString(L"Hello");
CObject* p_obj = (CObject*)p_str;

But this original code will have a compilation error.

CString* p_str = new CString(L"Hello");
CObject* p_obj = p_str;

Shao Voon Wong 14-Jul-20 3:56am    
Remember to make the function and the function pointer the same calling convention, as mentioned in your previous question thread.

The function declaration should be

siRuleReturn* WINAPI FireRule(CObList& list);

And the function pointer declaration should be

siRuleReturn* (WINAPI* FIRERULE)(CObList& list);
Shao Voon Wong 14-Jul-20 4:07am    
You can do the cause elimination by commenting out the lines starting from the bottom until the crash does not happen. Then you know the recent commented-out line is the cause.

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