win32resourcebundle.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 809 行 · 第 1/2 页

CPP
809
字号
//Win32ResourceBundle.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/FoundationKit/FoundationKit.h"#include "vcf/FoundationKit/FoundationKitPrivate.h"#include "vcf/FoundationKit/ResourceBundlePeer.h"#include "vcf/FoundationKit/Win32ResourceBundle.h"#if defined(VCF_BCC)  #include <cstring>  using std::strcmp;#endifusing namespace VCF;#define RT_RCDATA_W           MAKEINTRESOURCEW(10)#define RT_RCDATA_A           MAKEINTRESOURCEA(10)Win32ResourceBundle::Win32ResourceBundle():	foundResName_(false){	}Win32ResourceBundle::~Win32ResourceBundle(){	}String Win32ResourceBundle::getString( const String& resourceName ){	/**	Assume an exe resource by default?	*/	bool failedToFindRes = true;	HINSTANCE hinst = getResourceInstance();	String result;	HRSRC resHandle = NULL;	if ( System::isUnicodeEnabled() ) {		resHandle = ::FindResourceW( hinst, resourceName.c_str(), RT_RCDATA_W );	}	else {		resHandle = ::FindResourceA( hinst, resourceName.ansi_c_str(), RT_RCDATA_A );	}	if ( NULL != resHandle ){		HGLOBAL	data = ::LoadResource( hinst, resHandle );		if ( NULL != data ){			TCHAR* strData = (TCHAR*)::LockResource( data );			result = strData;			failedToFindRes = false;			::FreeResource( data );		}	}	else {				//try and see if the resourceName is an int id and find it via 		//throw exception- resource not found !!!!		uint32 stringID = 0;		try {			stringID = StringUtils::fromStringAsUInt(resourceName);		}		catch( ... ) {			result = L"";		}		if ( stringID > 0 ) {			if ( System::isUnicodeEnabled() ) {				wchar_t tmp[256];				int ret = ::LoadStringW( hinst, stringID, tmp, 255 );				if ( ret ) {					tmp[ret] = 0;					result = tmp;					failedToFindRes = false;				}							}			else {				char tmp[256];				int ret = ::LoadStringA( hinst, stringID, tmp, 255 );				if ( ret ) {					tmp[ret] = 0;					result = tmp;					failedToFindRes = false;				}			}					}		else {			failedToFindRes = true;		}	}	if ( failedToFindRes ) {		//look in the resource .strings file 				result = System::getCurrentThreadLocale()->translate( resourceName );			}	return result;}String Win32ResourceBundle::getVFF( const String& resourceName ){	String result;	bool failedToFindRes = true;	HRSRC resHandle = NULL;	if ( System::isUnicodeEnabled() ) {		resHandle = ::FindResourceW( getResourceInstance(), resourceName.c_str(), L"VFF" );	}	else {		resHandle = ::FindResourceA( getResourceInstance(), resourceName.ansi_c_str(), "VFF" );	}	if ( NULL != resHandle ){		HGLOBAL	data = ::LoadResource( NULL, resHandle );		if ( NULL != data ){			void* dataPtr = ::LockResource( data );			TCHAR* strData = (TCHAR*)dataPtr;			int size = SizeofResource( getResourceInstance(), resHandle );			result = strData;			int resSize = result.size();			void *tmp = dataPtr;			/**			*this is here to properly skip over '\0' characters in the stream			*/			while ( resSize < size ){				tmp = (void*)((char*)dataPtr + resSize + 1);				strData = (TCHAR*)tmp;				result += "\n";				result += strData;				resSize = result.size();				failedToFindRes = false;			}			::FreeResource( data );		}	}	else {		//throw exception- resource not found !!!!	}	return result;}Resource* Win32ResourceBundle::getResource( const String& resourceName ){	Resource* result = NULL;	bool failedToFindRes = true;	foundResName_ = false;	foundResType_ = "\0";	searchResName_ = resourceName;	/**	JC	we are using the ansi version as there isn't 	a wide string verion of the ENUMRESTYPEPROC	in the base version of the SDK headers that 	come with vc6.	The problem is that both the EnumResourceTypesA and 	EnumResourceTypeW functions are defined to take the 	same ENUMRESTYPEPROC, where in later version's of the 	SDK header the EnumResourceTypesA uses a ENUMRESTYPEPROCA	function pointer, and the EnumResourceTypesW uses a 	ENUMRESTYPEPROCW argument.	*/	::EnumResourceTypesA( getResourceInstance(),							 Win32ResourceBundle::EnumResTypeProcA,							 (LPARAM)this );		if ( true == foundResName_ ){		HRSRC resHandle = NULL;				resHandle = ::FindResourceA( getResourceInstance(),			                              resourceName.ansi_c_str(),										  foundResType_ );				if ( NULL != resHandle ){			HGLOBAL	dataHandle = ::LoadResource( NULL, resHandle );			if ( NULL != dataHandle ){				void* data = ::LockResource( dataHandle );				int size = ::SizeofResource( getResourceInstance(), resHandle );								return new Resource( data, size, resourceName );			}		}		else {			//throw exception- resource not found !!!!		}	}	//if we got this far then look for files!	String localeName = System::getCurrentThreadLocale()->getName();		bool fileExists = false;	String fileName = System::findResourceDirectory() +	resourceName;		if ( File::exists( fileName ) ) {		FileInputStream fs(fileName);		ulong32 size = fs.getSize();		char* buf = new char[size];		fs.read( (unsigned char*)buf, size );				result = new Resource( buf, size, resourceName );		delete [] buf;	}	return result;}BOOL CALLBACK Win32ResourceBundle::EnumResTypeProcA( HMODULE hModule, char* lpszType, LPARAM lParam ){#if !defined(VCF_CW) && !defined(UNICODE)	if ( (RT_CURSOR == lpszType) || (RT_ICON == lpszType) || (RT_BITMAP == lpszType) || (RT_STRING == lpszType) || (RT_VERSION == lpszType) || (RT_VXD == lpszType) ) {		return TRUE;	}	return ::EnumResourceNamesA( hModule,		                        lpszType,								Win32ResourceBundle::EnumResNameProcA,								lParam );#else	return FALSE;#endif}BOOL CALLBACK Win32ResourceBundle::EnumResNameProcA( HMODULE hModule, const char* lpszType, char* lpszName, LPARAM lParam ){	BOOL result = TRUE;	Win32ResourceBundle* thisPtr = (Win32ResourceBundle*)lParam;			if ( StringUtils::lowerCase(thisPtr->searchResName_) == StringUtils::lowerCase(lpszName) ) {		thisPtr->foundResName_ = true;		thisPtr->foundResType_ = lpszType;		result = FALSE;	}	return result;}HINSTANCE Win32ResourceBundle::getResourceInstance(){	HINSTANCE result = ::GetModuleHandle(NULL);			return result;}/* * Copyright (c) 2002 by Ted Peck <tpeck@roundwave.com> * Permission is given by the author to freely redistribute and include * this code in any program as long as this credit is given where due. * * THIS CODE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTY * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. IN PARTICULAR, NO WARRANTY IS MADE * THAT THE CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE * OR NON-INFRINGING. IN NO EVENT WILL THE AUTHOR BE LIABLE FOR ANY COSTS OR DAMAGES  * ARISING FROM ANY USE OF THIS CODE. NO USE OF THIS CODE IS AUTHORIZED EXCEPT UNDER * THIS DISCLAIMER. * * Use at your own risk! JC - Modified this for use in the VCF.*/template <typename CharType >struct VCF_VS_VERSIONINFO {   WORD  wLength;   WORD  wValueLength;   WORD  wType;   CharType szKey[1];   WORD  Padding1[1];   VS_FIXEDFILEINFO Value;   WORD  Padding2[1];   WORD  Children[1]; };typedef VCF_VS_VERSIONINFO<VCF::WideChar> VS_VERSIONINFO_W;typedef VCF_VS_VERSIONINFO<char> VS_VERSIONINFO_A;template <typename CharType >struct VCF_String {   WORD   wLength;   WORD   wValueLength;   WORD   wType;   CharType  szKey[1];   WORD   Padding[1];   WORD   Value[1]; }; typedef VCF_String<VCF::WideChar> String_W;typedef VCF_String<char> String_A;template <typename CharType >struct VCF_StringTable {   WORD   wLength;   WORD   wValueLength;   WORD   wType;   CharType  szKey[1];   WORD   Padding[1];   VCF_String<CharType> Children[1]; };typedef VCF_StringTable<VCF::WideChar> StringTable_W;typedef VCF_StringTable<char> StringTable_A;template <typename CharType >struct VCF_StringFileInfo {   WORD        wLength;   WORD        wValueLength;   WORD        wType;   CharType       szKey[1];   WORD        Padding[1];   VCF_StringTable<CharType> Children[1]; };typedef VCF_StringFileInfo<VCF::WideChar> StringFileInfo_W;typedef VCF_StringFileInfo<char> StringFileInfo_A;template <typename CharType >struct VCF_Var {   WORD  wLength;   WORD  wValueLength;   WORD  wType;   CharType szKey[1];   WORD  Padding[1];   DWORD Value[1]; }; typedef VCF_Var<VCF::WideChar> Var_W;typedef VCF_Var<char> Var_A;template <typename CharType >struct VCF_VarFileInfo {   WORD  wLength;   WORD  wValueLength;   WORD  wType;   CharType szKey[1];   WORD  Padding[1];   VCF_Var<CharType>  Children[1]; }; typedef VCF_VarFileInfo<VCF::WideChar> VarFileInfo_W;typedef VCF_VarFileInfo<char> VarFileInfo_A;typedef std::multimap<String,String> VersionMap;#define VS_ROUNDOFFS(a,b,r)	(((byte*)(b) - (byte*)(a) + ((r)-1)) & ~((r)-1))#define VS_ROUNDPOS(b, a, r)	(((byte*)(a))+VS_ROUNDOFFS(a,b,r))void getVersionInfoW( VersionMap& map, const String& fileName ){	VCF::WideChar fileNameW[MAX_PATH];	int sz = minVal<int>( MAX_PATH,fileName.size() );	fileName.copy( fileNameW,sz );	fileNameW[sz] = 0;	DWORD dummy;	DWORD size = GetFileVersionInfoSizeW( fileNameW, &dummy);	if ( 0 == size ) {		return;	}	unsigned char* buf = new unsigned char[size*sizeof(VCF::WideChar)];	memset(buf, 0, size*sizeof(VCF::WideChar));		if ( !GetFileVersionInfoW(fileNameW, 0, size, buf) ) {		delete [] buf;		return;	}	VS_VERSIONINFO_W* versionInfo = (VS_VERSIONINFO_W*)buf;	String key = versionInfo->szKey;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?