📄 res.cpp
字号:
// res.cpp
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "msc.h"
#include "res.h"
////////////////////////////////////////
// 儕僜乕僗偺僀儞僗僞儞僗 //
////////////////////////////////////////
static HINSTANCE hInstance ;
// 儕僜乕僗偺 INSTANCE 傪弶婜壔偡傞
static void InitResourceInstance ( void ) {
if ( IsNT () ) hInstance = GetModuleHandleW ( NULL ) ;
else hInstance = GetModuleHandleA ( NULL ) ;
#if defined _CONSOLE && ! defined NOCRT
if ( GetConsoleOutputCP () != GetOEMCP () ) SetResourceLanguage ( LANGID_ENGLISH ) ;
#endif
}
// 儕僜乕僗偺 INSTANCE 傪庢摼偡傞
HINSTANCE GetResourceInstance ( void ) {
if ( ! hInstance ) InitResourceInstance () ;
return hInstance ;
}
// 儕僜乕僗偺 INSTANCE 傪曄峏偡傞
// hNewInstance 偵 NULL 傪巜掕偡傞偲丄傕偲偵栠偡
HINSTANCE SetResourceInstance ( HINSTANCE hNewInstance ) {
if ( ! hInstance ) InitResourceInstance () ;
if ( ! hNewInstance ) {
if ( IsNT () ) hNewInstance = GetModuleHandleW ( NULL ) ;
else hNewInstance = GetModuleHandleA ( NULL ) ;
}
HINSTANCE hPrevInstance = hInstance ;
hInstance = hNewInstance ;
return hPrevInstance ;
}
#if ! defined NOCRT
////////////////////////////////////////
// 儕僜乕僗偺尵岅 //
////////////////////////////////////////
static int nResourceLangId = LANGID_NEUTRAL ; // = 0 ;
// 儕僜乕僗偺尵岅儘働乕儖傪庢摼偡傞
int GetResourceLanguage ( void ) {
return nResourceLangId ;
}
// 儕僜乕僗偺尵岅儘働乕儖傪曄峏偡傞
// 埲慜偺尵岅儘働乕儖傪曉偡
int SetResourceLanguage ( int nLangId ) {
int nPrevLangId = nResourceLangId ;
nResourceLangId = nLangId ;
return nPrevLangId ;
}
////////////////////////////////////////
// 僒僥儔僀僩儕僜乕僗偺愝掕 //
////////////////////////////////////////
static HINSTANCE hSatelliteInstance ;
// 懡尵岅梡儕僜乕僗偺 INSTANCE 傪庢摼偡傞
HINSTANCE GetSatelliteResourceInstance ( void ) {
return hSatelliteInstance ;
}
// 懡尵岅梡儕僜乕僗偺 INSTANCE 傪曄峏偡傞
// hNewInstance 偵 NULL 傪巜掕偡傞偲丄傕偲偵栠偡
HINSTANCE SetSatelliteResourceInstance ( HINSTANCE hNewInstance ) {
HINSTANCE hPrevInstance = hSatelliteInstance ;
hSatelliteInstance = hNewInstance ;
return hPrevInstance ;
}
////////////////////////////////////////
// 暥帤楍儕僜乕僗 //
////////////////////////////////////////
// GetStringTableData 偺壓惪偗娭悢
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 0 埲奜傪曉偡
static int GetStringTableDataSub ( int nId, const wchar_t **pVsString, int *pLength, HINSTANCE hCurrentInstance, int nLangId ) {
HRSRC hRsrc ;
HGLOBAL hGlobal = NULL ;
const wchar_t *pStringTable = NULL ;
if ( IsNT () ) hRsrc = FindResourceExW ( hCurrentInstance, MAKEINTRESOURCEW ( RT_STRING ), MAKEINTRESOURCEW ( ( nId / 0x10 + 1 ) ), nLangId ) ;
else hRsrc = FindResourceExA ( hCurrentInstance, MAKEINTRESOURCEA ( RT_STRING ), MAKEINTRESOURCEA ( ( nId / 0x10 + 1 ) ), nLangId ) ;
if ( hRsrc ) hGlobal = LoadResource ( hCurrentInstance, hRsrc ) ;
if ( hGlobal ) pStringTable = (const wchar_t*) LockResource ( hGlobal ) ;
if ( pStringTable ) {
for ( int nCount = nId % 0x10 ; nCount >= 0 ; nCount -- ) {
*pLength = *pStringTable ;
*pVsString = pStringTable + 1 ;
pStringTable = *pVsString + *pLength ;
}
if ( *pLength ) return 0 ;
else return 1 ;
}
return 1 ;
}
// LoadStringEx 偺壓惪偗娭悢
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 0 埲奜傪曉偡
static int GetStringTableData ( int nId, const wchar_t **pVsString, int *pLength ) {
if ( ! hInstance ) InitResourceInstance () ;
if ( hSatelliteInstance && ! GetStringTableDataSub ( nId, pVsString, pLength, hSatelliteInstance, nResourceLangId ) ) return 0 ;
if ( 1 && ! GetStringTableDataSub ( nId, pVsString, pLength, hInstance, nResourceLangId ) ) return 0 ;
if ( nResourceLangId != LANGID_ENGLISH && ! GetStringTableDataSub ( nId, pVsString, pLength, hInstance, LANGID_ENGLISH ) ) return 0 ;
if ( nResourceLangId != LANGID_NEUTRAL && ! GetStringTableDataSub ( nId, pVsString, pLength, hInstance, LANGID_NEUTRAL ) ) return 0 ;
return 1 ;
}
// 暥帤楍儕僜乕僗傪庢摼偡傞乮ANSI斉乯
// 僐僺乕偟偨暥帤偺挿偝傪曉偡
int LoadStringExA ( int nId, char *szBuffer, int nBufferSize ) {
if ( ! szBuffer ) return 0 ;
if ( ! nBufferSize ) return 0 ;
*szBuffer = 0 ;
const wchar_t *szString ;
int nLength ;
if ( GetStringTableData ( nId, & szString, & nLength ) ) return 0 ;
if ( nLength && ! *( szString + nLength - 1 ) ) nLength -- ;
int nResult = WideCharToMultiByte ( CP_ACP, 0, szString, nLength, NULL, 0, NULL, NULL ) ;
int nCopyLength = ( nResult < nBufferSize ) ? nResult : nBufferSize - 1 ;
memzero ( szBuffer, ( nCopyLength + 1 ) * sizeof(char) ) ;
WideCharToMultiByte ( CP_ACP, 0, szString, nLength, szBuffer, nCopyLength, NULL, NULL ) ;
return (int) strlen ( szBuffer ) ;
}
// 暥帤楍儕僜乕僗傪庢摼偡傞乮UNICODE斉乯
// 僐僺乕偟偨暥帤偺挿偝傪曉偡
// Win95/98/Me 偱傕巊梡壜
int LoadStringExW ( int nId, wchar_t *szBuffer, int nBufferSize ) {
if ( ! szBuffer ) return 0 ;
if ( ! nBufferSize ) return 0 ;
*szBuffer = 0 ;
const wchar_t *szString ;
int nLength ;
if ( GetStringTableData ( nId, & szString, & nLength ) ) return 0 ;
if ( nLength && ! *( szString + nLength - 1 ) ) nLength -- ;
int nCopyLength = ( nLength < nBufferSize ) ? nLength : nBufferSize - 1 ;
memmove ( szBuffer, szString, nCopyLength * sizeof(wchar_t) ) ;
szBuffer [ nCopyLength ] = 0 ;
return nCopyLength ;
}
static char *szGetStringBufferA ; // for Long Strings
static wchar_t*szGetStringBufferW ; // for Long Strings
static void __cdecl GetStringExitProc ( void ) ;
static volatile long IsGetStringExitProcRegistered ;
#define INIT_BUFSIZE 0x100
// 暥帤楍儕僜乕僗傪庢摼偡傞乮ANSI斉乯
// 儕僜乕僗偑尒偮偐傜側偗傟偽丄挿偝僛儘偺暥帤楍傪曉偡
// 彂偒崬傑傟偨暥帤楍偼師偵偙偺娭悢偑屇偽傟傞傑偱桳岠丄嵞擖晄壜
const char *GetStringA ( int nId ) {
free ( szGetStringBufferA ) ;
szGetStringBufferA = NULL ;
static char szInitBuffer [ INIT_BUFSIZE ] ; // for Short Strings
char *szBuffer = szInitBuffer ;
*szBuffer = 0 ;
const wchar_t *szString ;
int nLength ;
if ( GetStringTableData ( nId, & szString, & nLength ) ) return szBuffer ;
if ( nLength && ! *( szString + nLength - 1 ) ) nLength -- ;
int nTmpLength ;
if ( ! ( nTmpLength = WideCharToMultiByte ( CP_ACP, 0, szString, nLength, NULL, 0, NULL, NULL ) ) ) return szBuffer ;
if ( nTmpLength >= INIT_BUFSIZE ) {
if ( ! ( szGetStringBufferA = (char*) malloc ( ( nTmpLength + 1 ) * sizeof(char) ) ) ) return szBuffer ;
if ( ! InterlockedExchange ( & IsGetStringExitProcRegistered, TRUE ) ) atexit ( GetStringExitProc ) ;
szBuffer = szGetStringBufferA ;
}
if ( ! ( nTmpLength = WideCharToMultiByte ( CP_ACP, 0, szString, nLength, szBuffer, nTmpLength, NULL, NULL ) ) ) return szBuffer ;
szBuffer [ nTmpLength ] = 0 ;
return szBuffer ;
}
// 暥帤楍儕僜乕僗傪庢摼偡傞乮UNICODE斉乯
// 儕僜乕僗偑尒偮偐傜側偗傟偽丄挿偝僛儘偺暥帤楍傪曉偡
// 彂偒崬傑傟偨暥帤楍偼師偵偙偺娭悢偑屇偽傟傞傑偱桳岠丄嵞擖晄壜
// Win95/98/Me 偱傕巊梡壜
const wchar_t *GetStringW ( int nId ) {
free ( szGetStringBufferW ) ;
szGetStringBufferW = NULL ;
static wchar_t szInitBuffer [ INIT_BUFSIZE ] ; // for Short Strings
wchar_t *szBuffer = szInitBuffer ;
*szBuffer = 0 ;
const wchar_t *szString ;
int nLength ;
if ( GetStringTableData ( nId, & szString, & nLength ) ) return szBuffer ;
if ( nLength && ! *( szString + nLength - 1 ) ) nLength -- ;
if ( ! *( szString + nLength ) ) return szString ;
if ( nLength >= INIT_BUFSIZE ) {
if ( ! ( szGetStringBufferW = (wchar_t*) malloc ( ( nLength + 1 ) * sizeof(wchar_t) ) ) ) return szBuffer ;
if ( ! InterlockedExchange ( & IsGetStringExitProcRegistered, TRUE ) ) atexit ( GetStringExitProc ) ;
szBuffer = szGetStringBufferW ;
}
memmove ( szBuffer, szString, nLength * sizeof(wchar_t) ) ;
szBuffer [ nLength ] = 0 ;
return szBuffer ;
}
static void __cdecl GetStringExitProc ( void ) {
free ( szGetStringBufferA ) ;
free ( szGetStringBufferW ) ;
}
// 儊儌儕乕傪妋曐偟偰暥帤楍儕僜乕僗傪庢摼偡傞乮ANSI斉乯
// free() 傪巊偭偰夝曻偡傞偙偲
// 儕僜乕僗偑尒偮偐傜側偗傟偽丄NULL 傪曉偡
char *GetStringAllocA ( int nId ) {
char *szBuffer = NULL ;
const wchar_t *szString ;
int nLength ;
if ( GetStringTableData ( nId, & szString, & nLength ) ) return szBuffer ;
if ( nLength && ! *( szString + nLength - 1 ) ) nLength -- ;
int nTmpLength ;
if ( ! ( nTmpLength = WideCharToMultiByte ( CP_ACP, 0, szString, nLength, NULL, 0, NULL, NULL ) ) ) return szBuffer ;
if ( ! ( szBuffer = (char*) malloc ( ( nTmpLength + 1 ) * sizeof(char) ) ) ) return szBuffer ;
if ( ! ( nTmpLength = WideCharToMultiByte ( CP_ACP, 0, szString, nLength, szBuffer, nTmpLength, NULL, NULL ) ) ) return szBuffer ;
szBuffer [ nTmpLength ] = 0 ;
return szBuffer ;
}
// 儊儌儕乕傪妋曐偟偰暥帤楍儕僜乕僗傪庢摼偡傞乮UNICODE斉乯
// free() 傪巊偭偰夝曻偡傞偙偲
// 儕僜乕僗偑尒偮偐傜側偗傟偽丄NULL 傪曉偡
// Win95/98/Me 偱傕巊梡壜
wchar_t *GetStringAllocW ( int nId ) {
wchar_t *szBuffer = NULL ;
const wchar_t *szString ;
int nLength ;
if ( GetStringTableData ( nId, & szString, & nLength ) ) return szBuffer ;
if ( nLength && ! *( szString + nLength - 1 ) ) nLength -- ;
if ( ! ( szBuffer = (wchar_t*) malloc ( ( nLength + 1 ) * sizeof(wchar_t) ) ) ) return szBuffer ;
memmove ( szBuffer, szString, nLength * sizeof(wchar_t) ) ;
szBuffer [ nLength ] = 0 ;
return szBuffer ;
}
#endif // ! defined NOCRT
////////////////////////////////////////
// 僶乕僕儑儞儕僜乕僗 //
////////////////////////////////////////
// VS_VERSIONINFO, StringFileInfo, StringTable, String,...etc
#pragma pack(2)
typedef struct {
unsigned short wLength ;
unsigned short wValueLength ;
unsigned short wType ;
wchar_t szKey [ 1 ] ;
// 埲壓徣棯
} VS_TABLE ;
#pragma pack()
#define RoundUp(n,r) (((n)+(r)-1)&~((r)-1)) // 2 偺檖悢 r 偺攞悢偵愗傝忋偘
#define RoundUpLong(Size) RoundUp ( Size, sizeof(long) ) // 32價僢僩嫬奅偵愗傝忋偘
#define GetVsValue(pVs) ( (const void*) ( (pVs->wValueLength) ? ( (char*)pVs + RoundUpLong ( (char*)( wcsend ( pVs->szKey ) + 1 ) - (char*)pVs ) ) : NULL ) )
#define GetVsChildren(pVs) ( (const VS_TABLE*) ( (char*)pVs + RoundUpLong ( (char*)( wcsend ( pVs->szKey ) + 1 ) - (char*)pVs ) + RoundUpLong ( pVs->wValueLength << ( (pVs->wType)? 1 : 0 ) ) ) )
#define GetVsNext(pVs) ( (const VS_TABLE*) ( (char*)pVs + RoundUpLong ( pVs->wLength ) ) )
#define GetVsEnd(pVs) ( (const VS_TABLE*) ( (char*)pVs + pVs->wLength ) )
// 儕僜乕僗拞偺 VS_TABLE 傊偺億僀儞僞傪庢摼偡傞
// 僶乕僕儑儞儕僜乕僗偑尒偮偐傜側偗傟偽 NULL 傪曉偡
static const VS_TABLE *GetVersionInfo ( void ) {
HRSRC hRsrc ;
HGLOBAL hGlobal = NULL ;
const VS_TABLE *pVsVersionInfo = NULL ;
if ( ! hInstance ) InitResourceInstance () ;
if ( IsNT () ) hRsrc = FindResourceExW ( hInstance, MAKEINTRESOURCEW ( RT_VERSION ), MAKEINTRESOURCEW ( VS_VERSION_INFO ), MAKELANGID ( LANG_NEUTRAL, SUBLANG_NEUTRAL ) ) ;
else hRsrc = FindResourceExA ( hInstance, MAKEINTRESOURCEA ( RT_VERSION ), MAKEINTRESOURCEA ( VS_VERSION_INFO ), MAKELANGID ( LANG_NEUTRAL, SUBLANG_NEUTRAL ) ) ;
if ( hRsrc ) hGlobal = LoadResource ( hInstance, hRsrc ) ;
if ( hGlobal ) pVsVersionInfo = (const VS_TABLE*) LockResource ( hGlobal ) ;
return pVsVersionInfo ;
}
// 僶乕僕儑儞傪庢摼偡傞
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 0 埲奜傪曉偡
int GetFileVersion ( FILEVERSION *pFileVersion ) {
const VS_TABLE *pVsVersionInfo = GetVersionInfo () ;
if ( ! pVsVersionInfo ) return 1 ;
const VS_FIXEDFILEINFO *pFixedFileInfo = (const VS_FIXEDFILEINFO*) GetVsValue ( pVsVersionInfo ) ;
if ( ! pFixedFileInfo ) return 1 ;
pFileVersion->wMajor = HIWORD ( pFixedFileInfo->dwProductVersionMS ) ;
pFileVersion->wMinor = LOWORD ( pFixedFileInfo->dwProductVersionMS ) ;
pFileVersion->wBuild = HIWORD ( pFixedFileInfo->dwProductVersionLS ) ;
pFileVersion->wPrivate = LOWORD ( pFixedFileInfo->dwProductVersionLS ) ;
return 0 ;
}
// 僶乕僕儑儞傪斾妑偡傞
int CompareFileVersion ( const FILEVERSION *pFileVersion1, const FILEVERSION *pFileVersion2 ) {
if ( pFileVersion1->wMajor != pFileVersion2->wMajor ) return (short) ( pFileVersion1->wMajor - pFileVersion2->wMajor ) ;
if ( pFileVersion1->wMinor != pFileVersion2->wMinor ) return (short) ( pFileVersion1->wMinor - pFileVersion2->wMinor ) ;
if ( pFileVersion1->wBuild != pFileVersion2->wBuild ) return (short) ( pFileVersion1->wBuild - pFileVersion2->wBuild ) ;
if ( pFileVersion1->wPrivate != pFileVersion2->wPrivate ) return (short) ( pFileVersion1->wPrivate - pFileVersion2->wPrivate ) ;
return 0 ;
}
// 僶乕僕儑儞偺 wMajor 偲 wMinor 傪惍悢偵曄姺偡傞
// wMinor 偼 99 傪忋尷偲偡傞
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -