microsoft/Windows-classic-samples

DesktopToast--After the App exits and clicks the Toast message, local files cannot be loaded after the App is started.

1280103995 opened this issue · 1 comments

I added a new file config.ini in the DesktopToastSample. The contents are:

[base]
version=1.3.111

Then I modified the DesktopToastsSample.cpp file,

#include <fstream>
#include <iostream>
#include <string>

//Existing code

std::string GetConfigValue(const std::string& filename, const std::string& section, const std::string& key)
{
     std::string value;

     std::ifstream configFile(filename);
     if (configFile.is_open())
     {
         std::string line;
         std::string currentSection;
         bool sectionFound = false;
         while (std::getline(configFile, line))
         {
             line.erase(line.find_last_not_of(" \t\r\n") + 1);
             if (line.empty() || line[0] == ';' || line[0] == '#')
                 continue;

             if (line[0] == '[' && line.back() == ']')
             {
                 currentSection = line.substr(1, line.size() - 2);
                 if (currentSection == section)
                 {
                     sectionFound = true;
                 }
                 else
                 {
                     sectionFound = false;
                 }
                 continue;
             }

             if(sectionFound)
             {
                 size_t pos = line.find('=');
                 if (pos != std::string::npos)
                 {
                     std::string configKey = line.substr(0, pos);
                     if (configKey == key)
                     {
                         value = line.substr(pos + 1);
                         break;
                     }
                 }
             }
         }
         configFile.close();
     }
     else
     {
         OutputDebugStringA("Failed to open config file:");
         OutputDebugStringA(filename.c_str());
         OutputDebugStringA("\n");
     }

     return value;
}

std::string getVersion()
{
     std::string section = "base";
     std::string key = "version";
     std::string value = GetConfigValue("config.ini", section, key);
     MessageBoxA(nullptr, value.c_str(), "Configuration Value", MB_OK);
     return value;
}

// Prepare the main window
_Use_decl_annotations_
HRESULT DesktopToastsApp::Initialize(HINSTANCE hInstance)
{
     //Existing code
     getVersion();
     return hr;
}

Run the program and send a Toast, exit the App, and then click on the Toast message in the Notification Center to see a blank popup .
7127192838633
17127193062333

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    wchar_t path[MAX_PATH];
    if (GetModuleFileName(hInstance, path, ARRAYSIZE(path)))
    {
        wchar_t* lastSlash = wcsrchr(path, L'\\');
        if (lastSlash)
        {
            *lastSlash = L'\0'; // Truncate the path after the last slash
            SetCurrentDirectory(path);
        }
    }

    // ... the rest of your WinMain function ...
}