Click here to Skip to main content
14,640,250 members
Rate this:
Please Sign up or sign in to vote.
See more:
Below code works fine in VS2010 . But it is not working in VS2017. Below the code snippet .

void ResultRule::ExecuteFireRule()
	HINSTANCE         hModule;
	CSiRuleReturn *pRuleRet = NULL;


	hModule = LoadLibrary("siJCLJOb.dll");
	pFunc = (FIRERULE) GetProcAddress(hModule, "FireRule");

	if (pFunc != NULL)
		try {
			pRuleRet = pFunc(inputList);
		catch (CException *)

		if (pRuleRet->GetReturnCode() > retVal.GetReturnCode())
		if (pRuleRet->GetReturnObject())
		delete pRuleRet;		
		CString msg;
		msg.Format("The result rule defined on line %d of %s referenced result module [%s], which has no exported FireRules function.", m_fileLineNumber, m_ruleFile, m_moduleName);
		CSiException *pEx = new CSiException("CSiResultRule::FireRule", msg);
		throw pEx;

Calling Above code:
//Calling above ExecuteFireRule(), which will dynamically load firerule function of siJCLJOb.dll.

CSiRuleReturn ruleRet = iRulesProcessor::GetInstance()->FireRequestRule("JCLCARD", NULL, "");
CString *pJobTemplate = (CString*)ruleRet.GetReturnObject(); // gives me the correct values after executing FireRule function of siJCLJob.dll

CLauncherApp* Obj = (CLauncherApp*)AfxGetApp(); // after dynmic execution , it corrupts the memory /main thread . it show me the object siJCLJob.dll, when i place my cursor to Obj

What I have tried:

We are facing same issue in all MFC dynamic loaded objects. We have tried to compile the code by adding
... but getting runtime error
Exception thrown: read access violation.
this was 0x3E8.
Updated 7-Sep-20 7:48am
Rick York 10-Jul-20 11:11am
My guess is you have a version mismatch problem with MFC DLLs. Try rebuilding the DLLs. If you can't do that then you probably need to stay with VS2010.
Member 13261094 13-Jul-20 1:44am
All MFC and DLL are compiled using VS2017 in debug mode.
KarstenK 10-Jul-20 12:10pm
Rick is right, it best to build all dll with the same VS version. Another way is to make a static build.
Shao Voon Wong 11-Jul-20 5:46am
Check whether hModule and pFunc is NULL. Check siJCLJOb.dll is in the executable path. If they are correct, then it could be the VC runtime for siJCLJOb.dll is not found/installed. Try installing VS2010 runtime.
Member 13261094 13-Jul-20 1:45am
I have debugged the code and It's loading the siJCLJOB.dll and pFunc is not null. After execution of siJCLJOB.dll function, it corrupts the main memory/thread.
Shao Voon Wong 13-Jul-20 2:03am
Probably, FireRule() and its function pointer, FIRERULE declaration mismatched? Please show the 2 declarations.
Member 13261094 13-Jul-20 5:42am
FireRule() is working fine... It executes the code written in SiJCLJob.dll and return me the correct value in a string . but after that code doesn't work.. it nullify the main thread.

siRuleReturn* FireRule(CObList& list)// in SiJCJoB
Shao Voon Wong 13-Jul-20 6:27am
The function declaration should be

siRuleReturn* WINAPI FireRule(CObList& list);

And the function pointer declaration should be

siRuleReturn* (WINAPI* FIRERULE)(CObList& list);

In the past I encounter code worked in older VC++ but crashed in the newer VC++. Turns out the bug is in our code because the function accessed an element past the end of the vector. It did not crash in the old VC++ because there is nothing at the end of the vector to corrupt. Code-review the FireRule code is correct and is not accessing out of bounds.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

When you use
hModule = LoadLibrary("siJCLJOb.dll");

pFunc = (FIRERULE) GetProcAddress(hModule, "FireRule");

There is the option that LoadLibrary will fail, and if it does, the next line will cause an Access Violation.

Change it to:

hModule = LoadLibrary("siJCLJOb.dll");

if(hModule) pFunc = (FIRERULE) GetProcAddress(hModule, "FireRule");

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100