有的软件可能在不同的国家被用,那么就根据不同的国家选择不同的语言dll.(新建一个项目把资源信息编译成一个dll)。 在什么时候加载不同的dll呢?
实现方法:
从注册表中读出所用的语言类型(可以是在安装软件时用户设置的),然后验证版本相对应,如果对应那么就加载此语言dll作为项目资源句柄。
第一步:从注册表中读出语言种类.
bool GetLanguageFromRegistry(WCHAR* buffer, int SizeBytes)
{
HKEY key;
DWORD szType = REG_SZ;
DWORD szSize = SizeBytes;
bool ret = false;
if(RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\IDS\\uEye\\uEye camera demo\\Language", 0, KEY_READ, &key) == ERROR_SUCCESS)
{
if(RegQueryValueEx(key, L"Language", NULL, &szType, (LPBYTE)buffer, &szSize) == ERROR_SUCCESS)
{
ret = true;
}
RegCloseKey(key);
}
else
{
LANGID language = GetSystemDefaultLangID();
LANGID english = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
LANGID german = MAKELANGID(LANG_GERMAN, SUBLANG_DEFAULT);
CString Str;
if(language == english)
Str.LoadString(IDS_LANGUAGE_ENGLISH);
else if(language == german)
Str.LoadString(IDS_LANGUAGE_GERMAN);
else
Str.LoadString(IDS_LANGUAGE_ENGLISH);
wcscpy(buffer, Str);
if(RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\IDS\\uEye\\uEye camera demo\\Language", 0, 0, 0, KEY_ALL_ACCESS, NULL, &key, 0) == ERROR_SUCCESS)
{
RegSetValueEx(key, L"Language", NULL, REG_SZ, (LPBYTE)buffer, sizeof(buffer));
RegCloseKey(key);
ret = true;
}
}
return ret;
}
第二步:验证资源的dll版本号与应用程序的版本号是否对上.
bool SearchLanguageDll(WCHAR* file)
{
bool ret = false;
WCHAR DemoVersion[20];
WCHAR DllVersion[20];
GetDemoVersion(DemoVersion);
if(GetLanguageDllVersion(file, DllVersion) == true)
{
if(wcscmp(DemoVersion, DllVersion) == 0)
ret = true;
}
return ret;
}
bool GetDemoVersion(WCHAR* Result)
{
bool ret = false;
// Get the path to this program
WCHAR buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
CString Str;
DWORD dwSize = 0;
DWORD dwHandle;
dwSize = GetFileVersionInfoSize (buffer, &dwHandle);
if(dwSize != 0)
{
WCHAR* pc = new WCHAR[dwSize];
if(pc != NULL)
{
if(GetFileVersionInfo(buffer, 0, dwSize, pc))
{
VS_FIXEDFILEINFO* pfi;
int nLen;
if (VerQueryValue (pc, L"\\", (void**) &pfi, (PUINT) &nLen) )
{
swprintf(Result, L"%d.%d.%d", (pfi->dwFileVersionMS & 0xFFFF0000L)>>16,
(pfi->dwFileVersionMS & 0xFFFFL),
(pfi->dwFileVersionLS & 0xFFFF0000L)>>16);
/*
swprintf(Result, L"%d.%d.%d.%d", (pfi->dwFileVersionMS & 0xFFFF0000L)>>16,
(pfi->dwFileVersionMS & 0xFFFFL),
(pfi->dwFileVersionLS & 0xFFFF0000L)>>16,
(pfi->dwFileVersionLS & 0xFFFFL) );
*/
ret = true;
}
}
}
delete[] pc;
pc = NULL;
}
return ret;
}
bool GetLanguageDllVersion(const WCHAR* File, WCHAR* Result)
{
bool ret = false;
DWORD dwSize = 0;
DWORD dwHandle;
dwSize = GetFileVersionInfoSize ((WCHAR*)File, &dwHandle);
if(dwSize != 0)
{
WCHAR* pc = new WCHAR[dwSize];
if(pc != NULL)
{
if(GetFileVersionInfo((WCHAR*)File, 0, dwSize, pc))
{
VS_FIXEDFILEINFO* pfi;
int nLen;
if (VerQueryValue (pc, L"\\", (void**) &pfi, (PUINT) &nLen) )
{
swprintf(Result, L"%d.%d.%d", (pfi->dwFileVersionMS & 0xFFFF0000L)>>16,
(pfi->dwFileVersionMS & 0xFFFFL),
(pfi->dwFileVersionLS & 0xFFFF0000L)>>16);
/*
swprintf(Result, L"%d.%d.%d.%d", (pfi->dwFileVersionMS & 0xFFFF0000L)>>16,
(pfi->dwFileVersionMS & 0xFFFFL),
(pfi->dwFileVersionLS & 0xFFFF0000L)>>16,
(pfi->dwFileVersionLS & 0xFFFFL) );
*/
ret = true;
}
}
}
delete[] pc;
pc = NULL;
}
return ret;
}
第三步:加载dll作为资源句柄
HINSTANCE Lang = LoadLibrary(LanguageDll);
if(Lang != NULL)
AfxSetResourceHandle(Lang);