Click here to Skip to main content
15,891,033 members
Articles / Desktop Programming / MFC

Develop MFC Doc/View Application Which Supports Any Number Document Template

Rate me:
Please Sign up or sign in to vote.
1.57/5 (3 votes)
2 Jun 20052 min read 36.5K   1.7K   26  
This article provides an introduction to TangramLittle, a C++ Framework for MFC and the .NET Framework. Knowledge in MFC and the .NET Framework is assumed.
// [!output PROJECT_NAME].cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
[!if DLL_TYPE_EXTENSION]
#include <afxdllx.h>
[!else]
#include "[!output PROJECT_NAME].h"
[!endif]

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

[!if DLL_TYPE_REGULAR || DLL_TYPE_REGULAR_STATIC]
//
//	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.
//
[!endif]

[!if DLL_TYPE_EXTENSION]
static AFX_EXTENSION_MODULE [!output SAFE_PROJECT_NAME]DLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	// Remove this if you use lpReserved
	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("[!output SAFE_PROJECT_NAME].DLL Initializing!\n");
		
		// Extension DLL one-time initialization
		if (!AfxInitExtensionModule([!output SAFE_PROJECT_NAME]DLL, hInstance))
			return 0;

		// Insert this DLL into the resource chain
		// NOTE: If this Extension DLL is being implicitly linked to by
		//  an MFC Regular DLL (such as an ActiveX Control)
		//  instead of an MFC application, then you will want to
		//  remove this line from DllMain and put it in a separate
		//  function exported from this Extension DLL.  The Regular DLL
		//  that uses this Extension DLL should then explicitly call that
		//  function to initialize this Extension DLL.  Otherwise,
		//  the CDynLinkLibrary object will not be attached to the
		//  Regular DLL's resource chain, and serious problems will
		//  result.

		new CDynLinkLibrary([!output SAFE_PROJECT_NAME]DLL);

[!if SOCKETS]
		// Sockets initialization
		// NOTE: If this Extension DLL is being implicitly linked to by
		//  an MFC Regular DLL (such as an ActiveX Control)
		//  instead of an MFC application, then you will want to
		//  remove the following lines from DllMain and put them in a separate
		//  function exported from this Extension DLL.  The Regular DLL
		//  that uses this Extension DLL should then explicitly call that
		//  function to initialize this Extension DLL.
		if (!AfxSocketInit())
		{
			return FALSE;
		}
	
[!endif]
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("[!output SAFE_PROJECT_NAME].DLL Terminating!\n");

		// Terminate the library before destructors are called
		AfxTermExtensionModule([!output SAFE_PROJECT_NAME]DLL);
	}
	return 1;   // ok
}
[!else]

// [!output APP_CLASS]

BEGIN_MESSAGE_MAP([!output APP_CLASS], [!output APP_BASE_CLASS])
END_MESSAGE_MAP()


// [!output APP_CLASS] construction

[!output APP_CLASS]::[!output APP_CLASS]()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	m_strCreateInfo = _T("");
}

/*******************************************************************
[!output APP_CLASS]::GetManagedObj���Խ�һ��COM����ӳ��Ϊһ��.NET����
��ϵͳ��Ҫ����.NET����Ķ��ο�������ʱ����Ҫͨ�������������COM������
.NET����֮��Ķ�Ӧ��ϵ��
*******************************************************************/
Object* [!output APP_CLASS]::GetManagedObj(IDispatch* m_pComObjDisp)
{
	CComQIPtr<IDispatch, &IID_IDispatch> m_pDisp(m_pComObjDisp);
	if(m_pDisp)
	{
		Object* pObj = NULL;
		pObj = reinterpret_cast<Object*>(System::Runtime::InteropServices::Marshal::GetObjectForIUnknown(m_pComObjDisp));
		return pObj;
	}
	return NULL;
}

CString [!output APP_CLASS]::StringToCStr(String* str)
{
	USES_CONVERSION;
	System::IntPtr m_pBstr = System::Runtime::InteropServices::Marshal::StringToCoTaskMemUni(str);
#ifndef _UNICODE
	CString strRet = W2T((LPCWSTR)m_pBstr.ToInt64());
#else
	CString strRet = (LPCWSTR)m_pBstr.ToInt64();
#endif	
	System::Runtime::InteropServices::Marshal::FreeCoTaskMem(m_pBstr);
	return strRet;
}

/***************************************************************************
[!output APP_CLASS]::CreateFileFromResource�������������Ա������������ļ���
Ϊ��ԴǶ�뵽�����У�����ʱ�����Խ������������Դ���´���Ϊһ���ļ������磬һ��
��ʱ�ļ���һ��flash�����ȵȣ������Ǹ�ʮ�����õı�̼��ɡ�
***************************************************************************/
BOOL [!output APP_CLASS]::CreateFileFromResource(LPCTSTR lpszResourceName, LPCTSTR lpszType, LPCTSTR lpszTargetName)
{
	ASSERT(lpszResourceName != NULL);

	if(lpszTargetName==_T(""))return false;
	// determine location of the binary resource in resource fork
	HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, lpszType);

	HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, lpszType);

	// If failed, try instance handle
	if (hRsrc == 0)
		hRsrc = ::FindResource(hInst = AfxGetInstanceHandle(), lpszResourceName, lpszType);

	if (hRsrc == NULL)
		return false;

	HGLOBAL hData = LoadResource(hInst, hRsrc);
	if (hData == NULL)
		return false;

	DWORD dwSize = SizeofResource(hInst, hRsrc);
	LPBYTE pData = (LPBYTE) LockResource(hData);
	CFile m_File;
	CFileException e;

	if( !m_File.Open( lpszTargetName, CFile::modeCreate | CFile::modeWrite, &e ) )
	{
		#ifdef _DEBUG
		afxDump << "File could not be opened " << e.m_cause << "\n";
		#endif
		UnlockResource(hData);
		FreeResource(hRsrc);
		return false;
	}
	else
	{
		m_File.Write(pData,dwSize);
		m_File.Close();
	}
	UnlockResource(hData);
	FreeResource(hRsrc);

	return true;
}

// The one and only [!output APP_CLASS] object

[!output APP_CLASS] theApp;

[!if AUTOMATION]
const GUID CDECL BASED_CODE _tlid =
		[!output LIBID_STATIC_CONST_GUID_FORMAT];
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;

[!endif]

// [!output APP_CLASS] initialization

BOOL [!output APP_CLASS]::InitInstance()
{
	[!output APP_BASE_CLASS]::InitInstance();
[!if SOCKETS]

	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}
[!endif]
[!if AUTOMATION]

	// Register all OLE server (factories) as running.  This enables the
	//  OLE libraries to create objects from other applications.
	COleObjectFactory::RegisterAll();
[!endif]

	return TRUE;
}
[!if AUTOMATION]

// DllGetClassObject - Returns class factory

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return AfxDllGetClassObject(rclsid, riid, ppv);
}


// DllCanUnloadNow - Allows COM to unload DLL

STDAPI DllCanUnloadNow(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return AfxDllCanUnloadNow();
}


// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
		return SELFREG_E_TYPELIB;

	if (!COleObjectFactory::UpdateRegistryAll())
		return SELFREG_E_CLASS;

	return S_OK;
}


// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
		return SELFREG_E_TYPELIB;

	if (!COleObjectFactory::UpdateRegistryAll(FALSE))
		return SELFREG_E_CLASS;

	return S_OK;
}
[!endif]
[!endif]

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions