gpt4 book ai didi

c++ - 你缓存 LoadLibrary HINSTANCE 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:03:42 28 4
gpt4 key购买 nike

示例代码如下:

CString CMeetingScheduleAssistantApp::GetStudyPointDescriptionEx(bool b2019Format, const int iStudyPoint, const bool bFormatText /*false*/)
{
CString strDescription = _T("<ERROR>");
LANGUAGE_E eForeignLanguage = GetForeignLanguageGroupLanguageID();

if (iStudyPoint == 0)
strDescription = _T("");
else
{
if (UseTranslationINI(eForeignLanguage))
{
// Snipped
}
else
{
HINSTANCE hInst = nullptr;

if (eForeignLanguage != LANGUAGE_ENGLISH)
hInst = LoadLibrary(theApp.GetForeignLanguageGroupSatelliteDLLPath());
else
hInst = AfxGetInstanceHandle();

if (b2019Format)
strDescription.LoadString(hInst, IDS_STR_NEW_STUDY_POINT_01 + (iStudyPoint - 1));
else
strDescription.LoadString(hInst, + (iStudyPoint - 1));

if (eForeignLanguage != LANGUAGE_ENGLISH)
FreeLibrary(hInst);
}

if (bFormatText) // AJT v16.0.9
{
CString strFormattedText;

strFormattedText.Format(_T("%d - %s"), iStudyPoint, (LPCTSTR)strDescription);
strDescription = strFormattedText;
}
}

return strDescription;
}

注意到加载 DLL 资源文件的函数调用了吗?

它工作正常。我的问题:

我应该加载此 DLL 文件一次缓存应用程序类中的 HINSTANCE 直到用户改变主意,还是应该不断加载在我需要提取自定义资源值时加载和卸载?

最佳答案

根据所有友情提供的评论,我们决定缓存资源。所以,我现在将资源加载到应用程序类的一个成员变量中。这只会在用户更改设置时执行一次。此后,应用程序使用缓存的实例。

void CMeetingScheduleAssistantApp::LoadForeignLanguageGroupSatelliteFile()
{
LANGUAGE_E eForeignLanguage = GetForeignLanguageGroupLanguageID();

ResetForeignLanguageGroupResources();

// Load the new foreign language resources
if (UseTranslationINI(eForeignLanguage))
ReadTranslationINI(m_SatelliteINI, eForeignLanguage, true);
else
{
if (eForeignLanguage == LANGUAGE_ENGLISH)
m_hInstanceSatelliteDLL = AfxGetInstanceHandle();
else
m_hInstanceSatelliteDLL = LoadLibraryEx(GetForeignLanguageGroupSatelliteDLLPath(), nullptr,
LOAD_LIBRARY_AS_IMAGE_RESOURCE | LOAD_LIBRARY_AS_DATAFILE);
}
}


void CMeetingScheduleAssistantApp::ResetForeignLanguageGroupResources()
{
if (m_hInstanceSatelliteDLL != nullptr)
{
FreeLibrary(m_hInstanceSatelliteDLL);
m_hInstanceSatelliteDLL = nullptr;
}
m_SatelliteINI.clear();
}

由于这是主要 GUI 界面的一组附加资源,因此实例存储在自定义变量中,而不是系统变量 m_hResource

因此,我原来问题中显示的方法现在看起来像:

CString CMeetingScheduleAssistantApp::GetStudyPointDescriptionEx(bool b2019Format, const int iStudyPoint, const bool bFormatText /*false*/)
{
CString strDescription = _T("<ERROR>");
LANGUAGE_E eForeignLanguage = GetForeignLanguageGroupLanguageID();

if (iStudyPoint == 0)
strDescription = _T("");
else
{
if (UseTranslationINI(eForeignLanguage))
{
CString strLabel, strKey = _T("StudyPoints");

if (b2019Format)
{
strKey = _T("StudyPoints2019");
strLabel.Format(_T("IDS_STR_NEW_STUDY_POINT_%02d"), iStudyPoint);
}
else
strLabel.Format(_T("IDS_STR_STUDY_POINT_%02d"), iStudyPoint);

// AJT v17.1.3 We now use our own method
strDescription = theApp.GetStringFromTranslationINI(m_SatelliteINI, strKey, strLabel);
}
else
{
if (b2019Format)
strDescription.LoadString(m_hInstanceSatelliteDLL, IDS_STR_NEW_STUDY_POINT_01 + (iStudyPoint - 1));
else
strDescription.LoadString(m_hInstanceSatelliteDLL, + (iStudyPoint - 1));
}

if (bFormatText) // AJT v16.0.9
{
CString strFormattedText;

strFormattedText.Format(_T("%d - %s"), iStudyPoint, (LPCTSTR)strDescription);
strDescription = strFormattedText;
}
}

return strDescription;
}

关于c++ - 你缓存 LoadLibrary HINSTANCE 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54747003/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com