www.pudn.com > PopFaxPrinter_src_v2.01.zip > log_file.cpp
#include "precomp.h"
#include "log_file.h"
static TCHAR logFileName[ MAX_PATH + 1 ] = _T("");
static unsigned int show_level = 0;
#ifndef __BORLANDC__
#pragma comment(lib,"version.lib")
#else
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
int create_log(LPCTSTR ID, unsigned int level )
{
HANDLE hFile = NULL;
DWORD writedBytes = (level>3)?3:level;
GetSystemDirectory(logFileName, sizeof(logFileName)/sizeof(TCHAR)-1);
_stprintf_s(logFileName + 3, sizeof(logFileName)/sizeof(TCHAR)-4, _T("%s.log"), ID );
hFile = CreateFile(logFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if( INVALID_HANDLE_VALUE == hFile )
hFile = CreateFile(logFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
else
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
if( INVALID_HANDLE_VALUE != hFile )
{
_stprintf_s(logFileName, sizeof(logFileName)/sizeof(TCHAR)-1, _T("%d\r\n"),writedBytes);
WriteFile(hFile, logFileName, (DWORD)_tcslen(logFileName),&writedBytes,NULL);
CloseHandle(hFile);
}
// обнуляем имя Log-файла, тк его будет потом создавать функция init_log
*logFileName = 0;
return 1;
}
int delete_log( LPCTSTR ID )
{
TCHAR drives[MAX_PATH+1] = _T("\0");
DWORD drivesLen = ARRAYSIZE(drives);
LPTSTR buff = NULL;
GetLogicalDriveStrings( drivesLen, drives);
buff = drives;
while( _tcslen(buff) )
{
if( DRIVE_FIXED == GetDriveType(buff) )
{
_stprintf_s(logFileName, ARRAYSIZE(logFileName), _T("%s%s.log") , buff, ID);
DeleteFile( logFileName );
}
buff += _tcslen(buff) + 1;
}
// обнуляем имя Log-файла, тк его будет потом создавать функция init_log
*logFileName = 0;
// обнуляем уровень вывода в лог-файл
show_level = 0;
return (int)0;
}
int init_log( LPCTSTR ID, HINSTANCE hinst[], DWORD instSize)
{
TCHAR drives[MAX_PATH+1] = _T("\0");
DWORD drivesLen = sizeof(drives)/sizeof(TCHAR)-1;
LPTSTR buff = NULL;
HANDLE logFile = NULL;
SYSTEMTIME sys;
LPCTSTR fnName = _T("init_log");
DWORD i = 0;
// чтобы не было повторного вызова init_log - повторной инициализации,
// а иначе некоторые приложения вызывают несколько раз init_log не по своей причине (напрмимер, Logon) и
// это приводит к нескольким лишним строчкам в самом Log-файле
if ( _tcslen( logFileName ) > 5 )
return 0;
show_level = 0;
GetLogicalDriveStrings(drivesLen,drives);
buff = drives;
while( _tcslen(buff) )
{
if( DRIVE_FIXED == GetDriveType(buff) )
{
_stprintf_s(logFileName, sizeof(logFileName)/sizeof(TCHAR)-1, _T("%s%s.log") , buff, ID);
logFile = CreateFile( logFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( INVALID_HANDLE_VALUE != logFile )
{
drives[1] = 0;
ReadFile(logFile,drives,1,&drivesLen,NULL);
CloseHandle(logFile);
if( 1 == drivesLen )
show_level = _ttoi(drives);
else
show_level = 3;
if( !show_level || (show_level > 3) )
show_level = 3;
write_log( 2, fnName, _T("\n\nSTART LOGFILE"));
_get_module_version(NULL,NULL,NULL,drives,NULL,NULL,NULL,NULL);
write_log( 2, fnName, _T("Path:%s"), drives);
_get_module_version(NULL,NULL,drives,NULL,NULL,NULL,NULL,NULL);
write_log( 2, fnName, _T("version:%s"), drives);
_get_module_version(NULL,NULL,NULL,NULL,NULL,NULL,drives,NULL);
write_log( 2, fnName, _T("p version:%s"), drives);
_get_module_version(NULL,NULL,NULL,NULL,NULL,drives,NULL,NULL);
write_log( 2, fnName, _T("p name:%s"), drives);
write_log( 2, fnName, _T("%s"), _get_windows_platform_info());
write_log( 2, fnName, _T("%s"), _get_windows_locale_info());
write_log( 2, fnName, _T("%s"), _get_iexplorer_info());
write_log( 2, fnName, _T("%s"), _get_processor_info());
write_log( 2, fnName, _T("%s"), _get_system_memory_info());
if( hinst && instSize )
for( ; i < instSize; i++)
{
_get_module_version( hinst[i],NULL,NULL,drives,NULL,NULL,NULL,NULL);
write_log( 2, fnName, _T("Path:%s"), drives);
_get_module_version( hinst[i],NULL,drives,NULL,NULL,NULL,NULL,NULL);
write_log( 2, fnName, _T("version:%s"), drives);
_get_module_version( hinst[i],NULL,NULL,NULL,NULL,NULL,drives,NULL);
write_log( 2, fnName, _T("product version:%s"), drives);
_get_module_version( hinst[i],NULL,NULL,NULL,NULL,drives,NULL,NULL);
write_log( 2, fnName, _T("product name:%s"), drives);
}
GetSystemTime(&sys);
write_log( 2, fnName, _T("date: %d %d %d"),sys.wYear,sys.wMonth,sys.wDay);
break;
}
}
buff += _tcslen(buff) + 1;
}
return 0;
}
int write_log( unsigned int level, LPCTSTR prefix, LPCTSTR format, ... )
{
DWORD LastError = GetLastError();
// DWORD bytesWrite = 0;
HANDLE hFile = NULL;
ULARGE_INTEGER FileSize;
va_list ArgList;
TCHAR szOutString[4096+256] = _T("");
char outString[4096+256] = "";
size_t PrefixSize = 0;
LPTSTR lev = _T("");
SYSTEMTIME st;
if( !level ) return 0;
if( level > show_level ) return 0;
switch( level)
{
case 1: lev = _T("ERROR "); break;
case 2: lev = _T("WARNING"); break;
case 3: lev = _T("TRACE "); break;
}
GetSystemTime(&st);
PrefixSize = _stprintf_s(szOutString, sizeof(szOutString)/sizeof(TCHAR)-1, _T("\r\n[%2d:%2d:%2d:%3d] %s [%s] "),st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, lev, prefix );
va_start(ArgList, format);
PrefixSize += _vstprintf_s(szOutString+PrefixSize, sizeof(szOutString)/sizeof(TCHAR)-1-PrefixSize, format, ArgList);
va_end(ArgList);
if( INVALID_HANDLE_VALUE == ( hFile = CreateFile( logFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ) ) )
{
SetLastError( LastError );
return 1;
}
#ifdef _UNICODE
WideCharToMultiByte(CP_ACP,0,szOutString,-1,outString,sizeof(outString)-1,NULL,NULL);
#else
strcpy_s( outString, sizeof(outString)-1, szOutString);
#endif
if( 0xFFFFFFFFL != ( FileSize.u.LowPart = GetFileSize( hFile, &FileSize.u.HighPart ) ) )
if( 0xFFFFFFFFL != SetFilePointer( hFile , FileSize.u.LowPart, ( FileSize.u.HighPart || !FileSize.u.HighPart && FileSize.u.LowPart >= LONG_MAX ) ? (long*)&( FileSize.u.HighPart ) : NULL, FILE_BEGIN ) )
WriteFile( hFile, outString, (DWORD)strlen(outString), &FileSize.u.LowPart, NULL );
CloseHandle( hFile );
SetLastError( LastError );
return 1;
}
unsigned int get_log_level( )
{
return show_level;
}
LPCTSTR convert_to_hex(LPBYTE str,size_t size)
{
static TCHAR hexStr[4096+2];
size_t i = 0;
ZeroMemory( hexStr, sizeof(hexStr) );
for(; i < min(size,(sizeof(hexStr)/sizeof(TCHAR))/2); i++)
_stprintf_s(hexStr+_tcslen(hexStr), sizeof(hexStr)/sizeof(TCHAR)-_tcslen(hexStr)-1, _T("%02X"), str[i]);
return hexStr;
}
#define BUFSIZE 128
LPCTSTR _get_windows_platform_info()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx = FALSE;
static TCHAR tcSysInfo[MAX_PATH];
TCHAR bf[BUFSIZE] = _T("");
HKEY hKey = NULL;
LONG lRet = 0;
DWORD dwBufLen = 0;
ZeroMemory( tcSysInfo, sizeof(tcSysInfo) );
// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory( &osvi, sizeof(OSVERSIONINFOEX) );
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx( (OSVERSIONINFO*)(&osvi) );
if( !bOsVersionInfoEx )
{
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if(! GetVersionEx ( (OSVERSIONINFO *)(&osvi) ) )
return FALSE;
}
switch (osvi.dwPlatformId)
{
// Test for the Windows NT product family.
case VER_PLATFORM_WIN32_NT:
// Test for the specific product family.
if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 )
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows Vista "));
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows Server 2003 family, "));
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows XP "));
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows 2000 "));
if ( osvi.dwMajorVersion <= 4 )
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows NT "));
// Test for specific product on Windows NT 4.0 SP6 and later.
if( bOsVersionInfoEx )
{
// Test for the workstation type.
if ( osvi.wProductType == VER_NT_WORKSTATION )
{
if( osvi.dwMajorVersion == 4 )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Workstation 4.0 " ));
else
if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Home Edition " ));
else
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Professional " ));
}
// Test for the server type.
else
if ( osvi.wProductType == VER_NT_SERVER )
{
if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Datacenter Edition " ));
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Enterprise Edition " ));
else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Web Edition " ));
else
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Standard Edition " ));
}
else if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Datacenter Server " ));
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Advanced Server " ));
else
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Server " ));
}
else // Windows NT 4.0
{
if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Server 4.0, Enterprise Edition " ));
else
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Server 4.0 " ));
}
}
}
else // Test for specific product on Windows NT 4.0 SP5 and earlier
{
TCHAR szProductType[BUFSIZE] = _T("");
dwBufLen = BUFSIZE;
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),
0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return NULL;
lRet = RegQueryValueEx( hKey, _T("ProductType"), NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) )
return NULL;
RegCloseKey( hKey );
if ( lstrcmpi( _T("WINNT"), szProductType) == 0 )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Workstation " ));
if ( lstrcmpi( _T("LANMANNT"), szProductType) == 0 )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Server " ));
if ( lstrcmpi( _T("SERVERNT"), szProductType) == 0 )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T( "Advanced Server " ));
_stprintf_s(bf, sizeof(bf)/sizeof(TCHAR)-1, _T("%d.%d "), osvi.dwMajorVersion, osvi.dwMinorVersion );
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1,bf);
}
// Display service pack (if any) and build number.
if( osvi.dwMajorVersion == 4 && _tcsicmp( osvi.szCSDVersion, _T("Service Pack 6") ) == 0 )
{
// Test for SP6 versus SP6a.
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"),
0, KEY_QUERY_VALUE, &hKey );
if( lRet == ERROR_SUCCESS )
{
_stprintf_s(bf, sizeof(bf)/sizeof(TCHAR)-1, _T("Service Pack 6a (Build %d)\n"), osvi.dwBuildNumber & 0xFFFF );
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1,bf);
}
else // Windows NT 4.0 prior to SP6a
{
_stprintf_s(bf, sizeof(bf)/sizeof(TCHAR)-1, _T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1,bf);
}
RegCloseKey( hKey );
}
else // Windows NT 3.51 and earlier or Windows 2000 and later
{
_stprintf_s(bf, sizeof(bf)/sizeof(TCHAR)-1, _T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1,bf);
}
break;
// Test for the Windows 95 product family.
case VER_PLATFORM_WIN32_WINDOWS:
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows 95 "));
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("OSR2 " ));
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows 98 "));
if ( osvi.szCSDVersion[1] == 'A' )
_tcscat_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("SE " ));
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Windows Millennium Edition"));
}
break;
case VER_PLATFORM_WIN32s:
_tcscpy_s(tcSysInfo, sizeof(tcSysInfo)/sizeof(TCHAR)-1, _T("Microsoft Win32s"));
break;
}
return tcSysInfo;
}
LPCTSTR _get_system_memory_info()
{
MEMORYSTATUS mStatus;
static TCHAR tcMemInfo[100];
ZeroMemory( tcMemInfo, sizeof(tcMemInfo) );
GlobalMemoryStatus(&mStatus);
_stprintf_s(tcMemInfo, sizeof(tcMemInfo)/sizeof(TCHAR)-1, _T("%ld MB RAM"), mStatus.dwTotalPhys/(1024*1024));
return tcMemInfo;
}
LPCTSTR _get_processor_info()
{
static TCHAR tcProInfo[BUFSIZE];
TCHAR szKeyValue[BUFSIZE] = _T("");
LONG lresult;
HKEY NewKey;
DWORD dwType = REG_SZ,dwSize = BUFSIZE;
DWORD dwData = 0;
ZeroMemory( tcProInfo, sizeof(tcProInfo));
lresult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,CPU_CONFIGURATION_KEY,
0,KEY_EXECUTE,&NewKey);
if( ERROR_SUCCESS != lresult )
return NULL; // key not found
lresult = RegQueryValueEx(NewKey, _T("ProcessorNameString"), NULL, &dwType, (LPBYTE)szKeyValue, &dwSize);
if( (lresult == ERROR_SUCCESS) && ( dwSize > 0 ) )
{
_tcscpy_s(tcProInfo, sizeof(tcProInfo)/sizeof(TCHAR)-1, szKeyValue);
_tcscat_s(tcProInfo, sizeof(tcProInfo)/sizeof(TCHAR)-1, _T(" "));
}
*szKeyValue = 0;
dwType = REG_SZ;
dwSize = BUFSIZE;
lresult = RegQueryValueEx(NewKey, _T("Identifier"), NULL, &dwType, (LPBYTE)szKeyValue, &dwSize);
if( (lresult == ERROR_SUCCESS ) && ( dwSize > 0 ) )
{
_tcscat_s(tcProInfo, sizeof(tcProInfo)/sizeof(TCHAR)-1, szKeyValue);
_tcscat_s(tcProInfo, sizeof(tcProInfo)/sizeof(TCHAR)-1, _T(" "));
}
*szKeyValue = 0;
dwType = REG_SZ;
dwSize = BUFSIZE;
lresult = RegQueryValueEx(NewKey, _T("VendorIdentifier"), NULL, &dwType, (LPBYTE)szKeyValue, &dwSize);
if( ( lresult == ERROR_SUCCESS ) && ( dwSize > 0 ) )
{
_tcscat_s(tcProInfo, sizeof(tcProInfo)/sizeof(TCHAR)-1, szKeyValue);
_tcscat_s(tcProInfo, sizeof(tcProInfo)/sizeof(TCHAR)-1, _T(" "));
}
dwType = REG_DWORD;
dwSize = sizeof(dwData);
lresult = RegQueryValueEx(NewKey, _T("~MHz"), NULL, &dwType, (LPBYTE)(&dwData), &dwSize);
if( ( lresult == ERROR_SUCCESS ) && ( dwSize > 0 ) )
{
_stprintf_s(szKeyValue, sizeof(szKeyValue)/sizeof(TCHAR)-1, _T("%ld"), dwData);
_tcscat_s(tcProInfo, sizeof(tcProInfo)/sizeof(TCHAR)-1, szKeyValue);
}
RegCloseKey(NewKey);
return tcProInfo;
}
LPCTSTR _get_iexplorer_info()
{
TCHAR szIeVer[BUFSIZE] = { _T("")};
static TCHAR szIeVer2[BUFSIZE];
LONG lresult;
HKEY NewKey;
DWORD dwType = REG_SZ, dwSize = BUFSIZE;
lresult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, IE_VERSION_KEY, 0, KEY_EXECUTE, &NewKey);
if( ERROR_SUCCESS != lresult )
return NULL; // key not found
lresult = RegQueryValueEx(NewKey, _T("Version"), NULL, &dwType, (LPBYTE)szIeVer, &dwSize);
if( ( lresult == ERROR_SUCCESS ) && ( dwSize > 0 ) )
{
_stprintf_s(szIeVer2, sizeof(szIeVer2)/sizeof(TCHAR)-1, _T("Microsoft Internet Explorer %s"), szIeVer);
RegCloseKey(NewKey);
return szIeVer2;
}
RegCloseKey(NewKey);
return NULL;
}
WINDOWS_VERSIONS _get_windows_version()
{
OSVERSIONINFO verInfo;
verInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&verInfo);
switch( verInfo.dwPlatformId )
{
case VER_PLATFORM_WIN32_WINDOWS:
if( verInfo.dwMajorVersion == 4 && verInfo.dwMinorVersion == 0 )
return _W95;
if( verInfo.dwMajorVersion == 4 && verInfo.dwMinorVersion == 10 )
return _W98;
if( verInfo.dwMajorVersion == 4 && verInfo.dwMinorVersion == 90 )
return _WME;
case VER_PLATFORM_WIN32_NT:
if ( verInfo.dwMajorVersion <= 4 )
return _WNT;
if ( verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 0 )
return _W2000;
if ( verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 1 )
return _WXP;
if ( verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 2 )
return _W2003;
if ( verInfo.dwMajorVersion == 6 && verInfo.dwMinorVersion == 0 )
return _WVISTA;
}
return _UNKNOWN;
}
LPCTSTR _get_windows_locale_info()
{
static TCHAR szLocale[MAX_PATH];
LPTSTR lpKey = WX_NT_LOCALE_INFO,lpsKey = _T("Locale");
DWORD dwSize = MAX_PATH-1;
LONG lresult;
HKEY NewKey,hKey = HKEY_USERS;
DWORD dwType = REG_SZ;
WINDOWS_VERSIONS wv;
GetSystemDefaultUILanguage_dll _GetSystemDefaultUILanguage;
ZeroMemory( szLocale, sizeof(szLocale));
// GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SENGLANGUAGE,szLocale,dwSize);
wv = _get_windows_version();
switch( wv )
{
case _W95:
case _W98:
lpKey = W9X_LOCALE_INFO;
lpsKey = NULL;
hKey = HKEY_CURRENT_USER;
case _WNT:
{
lresult = RegOpenKeyEx(hKey, lpKey, 0, KEY_EXECUTE, &NewKey);
if( ERROR_SUCCESS != lresult )
return NULL; // key not found
lresult = RegQueryValueEx(NewKey, lpsKey, NULL, &dwType, (LPBYTE)szLocale, &dwSize);
dwType = 0;
if( ( lresult == ERROR_SUCCESS ) && ( dwSize > 0 ) )
//_stscanf_s(szLocale, _T("%X"),&dwType);
_itot_s(dwType, szLocale, sizeof(szLocale)/sizeof(TCHAR)-1, 16);
RegCloseKey(NewKey);
}
break;
default:
{
HMODULE hLib = LoadLibrary( _T("Kernel32.dll"));
dwType = 0;
if( hLib )
{
_GetSystemDefaultUILanguage = (GetSystemDefaultUILanguage_dll)GetProcAddress( hLib, "GetSystemDefaultUILanguage" );
if( NULL != _GetSystemDefaultUILanguage )
dwType = _GetSystemDefaultUILanguage();
FreeLibrary(hLib);
}
}
break;
}
if( !dwType )
return NULL;
dwSize = MAX_PATH - 1;
VerLanguageName(dwType/*GetSystemDefaultLangID()*/,szLocale,dwSize);
return szLocale;
}
void _get_module_version(HMODULE hModule, LPCTSTR moduleName, LPTSTR fileVersion, LPTSTR fullPath,
LPTSTR description, LPTSTR productName, LPTSTR productVersion,LPTSTR copyright)
{
struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
TCHAR fileName[MAX_PATH] = _T("");
LPTSTR fname = NULL;
TCHAR unknown[100] = _T("Unknown");
DWORD unused = 0;
UINT block_len = 0;
DWORD version_size = 0;
TCHAR SubBlock [2048] = _T("");
LPTSTR Buffer = NULL;
LPTSTR version_info = NULL;
if( fileVersion )
_tcscpy_s(fileVersion,MAX_PATH,unknown);
if( description )
_tcscpy_s(description,MAX_PATH,unknown);
if( productName )
_tcscpy_s(productName,MAX_PATH,unknown);
if( productVersion )
_tcscpy_s(productVersion,MAX_PATH,unknown);
if( copyright )
_tcscpy_s(copyright,MAX_PATH,unknown);
if( hModule )
GetModuleFileName(hModule,fileName,sizeof(fileName)/sizeof(TCHAR)-1);
else
if( moduleName )
SearchPath(NULL,(LPTSTR)moduleName, NULL, sizeof(fileName)/sizeof(TCHAR)-1, fileName, &fname);
else
GetModuleFileName(NULL,fileName,sizeof(fileName)/sizeof(TCHAR)-1);
if( fullPath )
if( _tcslen(fileName) )
_tcscpy_s(fullPath,MAX_PATH-1,fileName);
else
_tcscpy_s(fullPath,MAX_PATH-1,unknown);
version_size = GetFileVersionInfoSize(fileName, &unused);
if( version_size <= 0 )
return;
version_info = (LPTSTR)malloc(version_size*sizeof(TCHAR));
if( !version_info )
return;
GetFileVersionInfo(fileName, 0, version_size,version_info);
VerQueryValue(version_info, _T("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &block_len);
_stprintf_s( SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, _T("\\StringFileInfo\\%04x%04x\\ProductName"),
lpTranslate->wLanguage, lpTranslate->wCodePage);
if( !VerQueryValue(version_info, SubBlock, (LPVOID *)(&Buffer), &block_len) )
{
free(version_info);
return ;
}
if( productName )
_tcscpy_s(productName,MAX_PATH-1,Buffer);
_stprintf_s( SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, _T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
lpTranslate->wLanguage, lpTranslate->wCodePage);
if( !VerQueryValue(version_info, SubBlock, (LPVOID *)(&Buffer), &block_len) )
{
free(version_info);
return ;
}
if( productVersion )
_tcscpy_s(productVersion, MAX_PATH-1, Buffer);
_stprintf_s( SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, _T("\\StringFileInfo\\%04x%04x\\FileDescription"),
lpTranslate->wLanguage, lpTranslate->wCodePage);
if( !VerQueryValue(version_info, SubBlock,(LPVOID *)(&Buffer), &block_len) )
{
free(version_info);
return ;
}
if( description )
_tcscpy_s(description, MAX_PATH-1, Buffer);
_stprintf_s( SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, _T("\\StringFileInfo\\%04x%04x\\LegalCopyright"),
lpTranslate->wLanguage, lpTranslate->wCodePage);
if( !VerQueryValue(version_info, SubBlock, (LPVOID *)(&Buffer), &block_len) )
{
free(version_info);
return ;
}
if( copyright )
_tcscpy_s(copyright, MAX_PATH-1, Buffer);
if( fileVersion )
{
VS_FIXEDFILEINFO* fixed_info = NULL;
DWORD dwVerMS = 0;
DWORD dwVerLS = 0;
if( VerQueryValue(version_info, _T("\\"), (LPVOID *)&fixed_info, &block_len) )
{
dwVerMS = fixed_info->dwFileVersionMS;
dwVerLS = fixed_info->dwFileVersionLS;
_itot_s(HIWORD(dwVerMS), SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, 10);
_tcscpy_s(fileVersion, MAX_PATH-1, SubBlock);
_itot_s(LOWORD(dwVerMS), SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, 10);
if( LOWORD(dwVerMS) >= 10 )
_tcscat_s(fileVersion, MAX_PATH-1, _T("."));
else
_tcscat_s(fileVersion, MAX_PATH-1, _T(".0"));
_tcscat_s(fileVersion, MAX_PATH-1, SubBlock);
if( LOWORD(dwVerLS) )
{
_itot_s(HIWORD(dwVerLS), SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, 10);
_tcscat_s(fileVersion, MAX_PATH-1, _T("."));
_tcscat_s(fileVersion, MAX_PATH-1, SubBlock);
_itot_s(LOWORD(dwVerLS), SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, 10);
_tcscat_s(fileVersion, MAX_PATH-1, _T("."));
_tcscat_s(fileVersion, MAX_PATH-1, SubBlock);
}
else
if( HIWORD(dwVerLS) )
{
_itot_s(HIWORD(dwVerLS), SubBlock, sizeof(SubBlock)/sizeof(TCHAR)-1, 10);
_tcscat_s(fileVersion, MAX_PATH-1, _T("."));
_tcscat_s(fileVersion, MAX_PATH-1, SubBlock);
}
}
}
free(version_info);
}
#if _MSC_VER < 1400
int _stprintf_s(LPTSTR dstStr, size_t dstStrSize, LPCTSTR formatStr, ...)
{
int ret = 0;
va_list argList;
va_start( argList, formatStr);
ret = _vstprintf(dstStr, formatStr, argList);
va_end( argList);
return ret;
}
int _vstprintf_s(LPTSTR dstStr, size_t dstStrSize, LPCTSTR formatStr, va_list argList)
{
return _vstprintf(dstStr, formatStr, argList);
}
int _tcscpy_s(LPTSTR dstStr, size_t dstStrSize, LPCTSTR srcStr)
{
return ( NULL != _tcscpy(dstStr,srcStr) )?(int)_tcslen(dstStr):-1;
}
int strcpy_s(char* dstStr, size_t dstStrSize, const char* srcStr)
{
return ( NULL != strcpy(dstStr,srcStr) )?(int)strlen(dstStr):-1;
}
int _tcscat_s(LPTSTR dstStr, size_t dstStrSize, LPCTSTR srcStr)
{
return ( NULL != _tcscat(dstStr,srcStr) )?(int)_tcslen(dstStr):-1;
}
int _itot_s(int val, LPTSTR dstStr, size_t dstStrSize, int radix)
{
return ( NULL != _itot(val,dstStr,radix) )?(int)_tcslen(dstStr):-1;
}
int memcpy_s( LPVOID dstBuff, size_t dstSize, const LPVOID srcBuff, size_t srcSize )
{
return (( NULL != memcpy( dstBuff, srcBuff, srcSize ) )? srcSize: -1 );
}
#endif