OK, I've stripped this down to the bare bones and LoadLibrary() still fails after 20-30 calls:
stdafx.h:
Code:
#if !defined(AFX_STDAFX_H__7B54890C_3655_4A2C_BA76_444CC4D20F7E__INCLUDED_)
#define AFX_STDAFX_H__7B54890C_3655_4A2C_BA76_444CC4D20F7E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Insert your headers here
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <tchar.h>
#endif // !defined(AFX_STDAFX_H__7B54890C_3655_4A2C_BA76_444CC4D20F7E__INCLUDED_)
UExit.h:
Code:
#ifndef UEXIT_H_INCLUDED_JULY_2_2008
#define UEXIT_H_INCLUDED_JULY_2_2008
#ifdef _XDLL
# define UEXIT_API __declspec(dllexport)
#else
# define UEXIT_API __declspec(dllimport)
#endif
#endif // UEXIT_H_INCLUDED_JULY_2_2008
UExit.cpp:
Code:
#include "stdafx.h"
#include "UExit.h"
BOOL APIENTRY
DllMain( HANDLE /*hModule*/,
DWORD ul_reason_for_call,
LPVOID /*lpReserved*/
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
TestHarness.cpp:
Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
HMODULE
LoadDLL()
{
HMODULE hDLL = LoadLibrary( _T("UExit.dll") );
if ( hDLL == NULL )
{
cout << "Error loading DLL!" << endl;
exit( 1 );
}
else
{
cout << "DLL Loaded successfully." << endl;
}
return hDLL;
}
void
UnloadDLL( HMODULE& hDLL )
{
if ( hDLL == NULL )
{
cout << "The DLL isn't loaded!" << endl;
}
else
{
if ( FreeLibrary( hDLL ) == FALSE )
{
cout << "Error unloading DLL!" << endl;
}
else
{
cout << "DLL unloaded successfully." << endl;
hDLL = NULL;
}
}
}
int main()
{
HMODULE hDLL = NULL;
for ( int i = 0; i < 1000; ++i )
{
cout << "*** " << i << endl;
Sleep( 50 );
hDLL = LoadDLL();
Sleep( 50 );
UnloadDLL( hDLL );
}
return 0;
}
How can THIS possibly be failing???