是否存在技术原因,为什么 COM DLL 在完成时删除传入的临时 JSON 会更好?

Is there a technical reason why it would be better for the COM DLL to delete the passed in temporary JSON when it is finished with it?

上下文

  1. 我的父 MFC 项目创建了一个 JSON 文件:

    CImportFromCLMExplorerDlg::CreateMSATranslationsJson(strTempJson);
    
  2. 此临时文件作为参数传递到 C# COM DLL 中供其使用:

    theApp.MSAToolsInterface().ImportHistoryFromCLMExplorer(
                         theApp.GetLanguageCode(dlgImportCLM.GetLanguageToImport()),
                         dlgImportCLM.GetCalendarDBPath(),
                         theApp.GetAssignHistoryXMLPath(),
                         strTempJson);
    
  3. 最后我把上面的COM DLL方法后的临时文件删除了returns:

    ::DeleteFile(strTempJson);
    

我的问题

从技术上讲,让 COM DLL 在使用完此文件后将其删除是一种更好的方法吗?或者在父项目中删除它是否完全没问题(看起来是)?

我不是征求意见,而是询问是否有技术原因,为什么 COM DLL 在完成时删除传入的临时 JSON 会更好。

JSON 文件

评论暗示了使用 ISteam 而不传递文字文件的可能性。目前我正在创建这样的 JSON 文件:

bool CImportFromCLMExplorerDlg::CreateMSATranslationsJson(const CString strPathJson)
{
    CkString strOut, strValue;
    CkJsonObject json;
    bool success;

    LanguageMSA eLang = theApp.GetProgramLanguage();

    // Note: The methods return false if "out of memory"

    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_BIBLE_READING));
    success = json.AddStringAt(-1, "BibleReading", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_BIBLE_READING_MAIN));
    success = json.AddStringAt(-1, "BibleReadingMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_BIBLE_READING_AUX));
    success = json.AddStringAt(-1, "BibleReadingAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_INITIAL_CALL));
    success = json.AddStringAt(-1, "InitialCall", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_INITIAL_CALL_MAIN));
    success = json.AddStringAt(-1, "InitialCallMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_INITIAL_CALL_AUX));
    success = json.AddStringAt(-1, "InitialCallAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_RETURN_VISIT));
    success = json.AddStringAt(-1, "ReturnVisit", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_RETURN_MAIN));
    success = json.AddStringAt(-1, "ReturnVisitMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_RETURN_AUX));
    success = json.AddStringAt(-1, "ReturnVisitAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_BIBLE_STUDY));
    success = json.AddStringAt(-1, "BibleStudy", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_STUDY_MAIN));
    success = json.AddStringAt(-1, "BibleStudyMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_STUDY_AUX));
    success = json.AddStringAt(-1, "BibleStudyAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_TALK));
    success = json.AddStringAt(-1, "Talk", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_TALK_MAIN));
    success = json.AddStringAt(-1, "TalkMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_TALK_AUX));
    success = json.AddStringAt(-1, "TalkAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_ASSISTANT));
    success = json.AddStringAt(-1, "Assistant", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_QA));
    success = json.AddStringAt(-1, "QuestionsAndAnswers", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_DISC_VIDEO));
    success = json.AddStringAt(-1, "DiscussionWithVideo", strValue);
    success = json.AddStringAt(-1, "SampleConversation", "Sample Conversation");
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_INITIAL_CALL_VIDEO));
    success = json.AddStringAt(-1, "InitialCallVideo", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_RETURN_VISIT_VIDEO));
    success = json.AddStringAt(-1, "ReturnVisitVideo", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_VIDEO));
    success = json.AddStringAt(-1, "Video", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_THEME_PRESENTATIONS));
    success = json.AddStringAt(-1, "Presentations", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_THEME_SPIRITUAL_GEMS));
    success = json.AddStringAt(-1, "SpiritualGems", strValue);


    json.put_EmitCompact(false);
    strOut.append(json.emit());
    strOut.append("\r\n");
    
    CkString strPathJsonEx;
    strPathJsonEx.setStringU(strPathJson);
    return strOut.saveToFile(strPathJsonEx, "utf-8");
}

它使用CkJsonObject and CkString 类。我不确定是否可以将其转换为可以传递给 DLL 的对象。作为记录,这就是我目前在 COM DLL 中读取 JSON 的方式:

private MSATranslations GetMSATranslations(string strPath)
{
    using (var reader = new StreamReader(strPath, Encoding.UTF8))
        return JsonConvert.DeserializeObject<MSATranslations>(reader.ReadToEnd());
}

步骤 1

Adjust the code that creates the JSON to simply build a `CString`:

bool CImportFromCLMExplorerDlg::CreateMSATranslationsJson(CString &strTranslationsJson)
{
    CkString strOut, strValue;
    CkJsonObject json;
    bool success;

    LanguageMSA eLang = theApp.GetProgramLanguage();

    // Note: The methods return false if "out of memory"

    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_BIBLE_READING));
    success = json.AddStringAt(-1, "BibleReading", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_BIBLE_READING_MAIN));
    success = json.AddStringAt(-1, "BibleReadingMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_BIBLE_READING_AUX));
    success = json.AddStringAt(-1, "BibleReadingAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_INITIAL_CALL));
    success = json.AddStringAt(-1, "InitialCall", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_INITIAL_CALL_MAIN));
    success = json.AddStringAt(-1, "InitialCallMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_INITIAL_CALL_AUX));
    success = json.AddStringAt(-1, "InitialCallAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_RETURN_VISIT));
    success = json.AddStringAt(-1, "ReturnVisit", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_RETURN_MAIN));
    success = json.AddStringAt(-1, "ReturnVisitMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_RETURN_AUX));
    success = json.AddStringAt(-1, "ReturnVisitAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_BIBLE_STUDY));
    success = json.AddStringAt(-1, "BibleStudy", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_STUDY_MAIN));
    success = json.AddStringAt(-1, "BibleStudyMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_STUDY_AUX));
    success = json.AddStringAt(-1, "BibleStudyAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_TALK));
    success = json.AddStringAt(-1, "Talk", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_TALK_MAIN));
    success = json.AddStringAt(-1, "TalkMain", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_TALK_AUX));
    success = json.AddStringAt(-1, "TalkAux", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_HISTORY_ASSISTANT));
    success = json.AddStringAt(-1, "Assistant", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_QA));
    success = json.AddStringAt(-1, "QuestionsAndAnswers", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_DISC_VIDEO));
    success = json.AddStringAt(-1, "DiscussionWithVideo", strValue);
    success = json.AddStringAt(-1, "SampleConversation", "Sample Conversation");
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_INITIAL_CALL_VIDEO));
    success = json.AddStringAt(-1, "InitialCallVideo", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_RETURN_VISIT_VIDEO));
    success = json.AddStringAt(-1, "ReturnVisitVideo", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_CMB_METHOD_VIDEO));
    success = json.AddStringAt(-1, "Video", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_THEME_PRESENTATIONS));
    success = json.AddStringAt(-1, "Presentations", strValue);
    strValue.setStringU(SMMETHOD3(eLang, IDS_STR_THEME_SPIRITUAL_GEMS));
    success = json.AddStringAt(-1, "SpiritualGems", strValue);

    json.put_EmitCompact(false);
    strOut.append(json.emit());
    strOut.append("\r\n");

    strTranslationsJson = strOut.getUnicode();

    return true;
}

第 2 步

那个 字符串传递给 API COM 包装器:

CString strTranslationsJson;
if (CImportFromCLMExplorerDlg::CreateMSATranslationsJson(strTranslationsJson))
{
    theApp.MSAToolsInterface().ImportHistoryFromCLMExplorer(
        theApp.GetLanguageCode(dlgImportCLM.GetLanguageToImport()),
        dlgImportCLM.GetCalendarDBPath(),
        theApp.GetAssignHistoryXMLPath(),
        strTranslationsJson);
}

步骤 3

包装器将其作为 CComBSTR 传递给 DLL:

void CMSATools::ImportHistoryFromCLMExplorer(const CString strLanguageCode, const CString strCalendarDBPath, const CString strHistoryDBPath, const CString strTranslationsJson)
{
    if (m_pInterface != nullptr)
    {
        CComBSTR bstrLanguageCode(strLanguageCode);
        CComBSTR bstrCalendarDBPath(strCalendarDBPath);
        CComBSTR bstrHistoryDBPath(strHistoryDBPath);
        CComBSTR bstrTranslationsJson(strTranslationsJson);
        m_pInterface->ImportHistoryFromCLMExplorer(bstrLanguageCode,
            bstrCalendarDBPath, bstrHistoryDBPath, bstrTranslationsJson);
    }
}

第 4 步

最后,在 DLL 中,我们将该字符串转换为我们所需的对象:

private MSATranslations GetMSATranslations(string strTranslationsJson)
{
    return JsonConvert.DeserializeObject<MSATranslations>(strTranslationsJson);
}

效果很好。不再有物理 JSON 文件!