// TLSDLL.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "TLSDLL.h"

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

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CTLSDLLApp

BEGIN_MESSAGE_MAP(CTLSDLLApp, CWinApp)
	//{{AFX_MSG_MAP(CTLSDLLApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTLSDLLApp construction

CTLSDLLApp::CTLSDLLApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTLSDLLApp object

CTLSDLLApp theApp;

__declspec (thread)	int tlsFlag1 = 897;
__declspec (thread)	int tlsFlag2 = 642;

UINT ThreadFunction(LPVOID pParam)
{
	CString str;

	if (tlsFlag1 == 897)	::MessageBox(NULL, _T("tlsFlag1 is ok"), _T("TLSDLL test"), MB_OK);
	if (tlsFlag2 == 642)	::MessageBox(NULL, _T("tlsFlag2 is ok"), _T("TLSDLL test"), MB_OK);

	tlsFlag2 = (int)pParam;

	str.Format(_T("tlsFlag2 is set to %d"), tlsFlag2);
	MessageBox(NULL, str, _T("test"), MB_OK);

	return 0;
}

// This is an example of an exported function.
extern "C" int PASCAL EXPORT fnTLSDLL(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	// normal function body here
	AfxBeginThread(ThreadFunction, (LPVOID)299);
	AfxBeginThread(ThreadFunction, (LPVOID)381);
	return 42;
}