Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below code works fine in VS2010 . But it is not working in VS2017. Below the code snippet .

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

	FIRERULE pFunc;

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

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

		if (pRuleRet->GetReturnCode() > retVal.GetReturnCode())
		{
			retVal.SetReturnCode(pRuleRet->GetReturnCode());
		}
		if (pRuleRet->GetReturnObject())
		{
			retVal.SetReturnObject(pRuleRet->GetReturnObject());
		}
		retVal.AppendErrors(pRuleRet->GetErrorList());
		delete pRuleRet;		
	}
	else
	{
		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
/Zc:threadSafeInit 
... but getting runtime error
Exception thrown: read access violation.
this was 0x3E8.
Posted
Updated 7-Sep-20 7:48am
v2
Comments
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.

1 solution

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");
 
Share this answer
 

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