Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crash when function with dynamic call returns

Status
Not open for further replies.

snuv

Programmer
Oct 30, 2001
751
GB
I have a dynamic call to a DLL from another DLL that is statically linked to the calling application

In debug mode the call works fine
When I run the code in release mode I get a crash when the QueryLoginDynamicCall function returns


snippet from Test.dll

Code:
#include "Permission.h"

#define DLLExport _declspec( dllexport )
extern "C"
{
	DLLExport int GetUserLoginAuto(char * pszUserName, BOOL* pbPermission);
}

int GetUserLoginAuto(char * pszUserName, BOOL* pbPermission)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

	CPermission test;
	test.m_strUserName = pszUserName;
	int nRet = test.DoModal();
	*pbPermission = test.m_bPermissionToLogin;
	if (nRet !=IDOK)
		test.m_nErrorCode = nRet;
	return test.m_nErrorCode;
}


snippet from calling DLL

If I remove the line where the function is actually called, the code doesn't crash.
eg use nRetVal = 0; //pUserCheckLogin(pszUserName,pbResult);

Code:
int LoginForm::QueryLoginDynamicCall(char *pszUserName, BOOL *pbResult)
{
	int nRetVal = -1;
	*pbResult = FALSE;

	HMODULE hTest = LoadLibrary("test.dll");
	if (hTest)
	{
		LPFNDLLFUNC1 pUserCheckLogin = (LPFNDLLFUNC1)GetProcAddress(hTest,"GetUserLoginAuto");
		if (pUserCheckLogin)
			nRetVal = pUserCheckLogin(pszUserName,pbResult);
		else
			nRetVal = -1;
		FreeLibrary(hTest);
	}
	return nRetVal;
}


Can anyone tell me what Im doing wrong?

Go not to cats for advice for they will just walk over the keyboard
 
Is "test.dll" in the same directory as your executable? Or is it in a directory defined by the "PATH" environment variable? If executing the code from dev studio, you may need to put a copy of the file in your project directory (a mfc issue)

Matt
 
test.dll is in the same directory as the dll that calls it
the directory is in the path(first listed)

The call in application ie callingApp->LoginDLL->testDLL is not in the same directory

It crashes whether I run the code through msdev or just running the calling app

Go not to cats for advice for they will just walk over the keyboard
 
It appears to be caused by the optimisations in the intermediate DLL

It appears that the system gets confused and treats the integer return value as a function pointer when the optimisation is set to 'maximise speed'. It's then trying to call a function at 0x00000000 at which point the software crashes.

Changing the optimisations to 'default' solves the problem




Go not to cats for advice for they will just walk over the keyboard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top