c++winapigetopenfilename

C++ win32 App is Freezing after a while i select a file from file dialog?


I am running my Win32 application on Windows 7 32-bit.

I have the following two functions that create a freeze in my application but only when the Internet is available:

HWND mainHwnd;

// When i want to add a file somewhere in the app 
_bstr_t BrowseSelectFile(){

    _bstr_t FileSelected;
    OPENFILENAME ofn;
    TCHAR FileName[MAX_PATH];
    HWND hwn = mainHwnd // setted in global variable from main events 
    //INT_PTR CALLBACK WindowRes(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) mainHwnd = hwnd;

    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwn;
    ofn.lpstrFile = FileName;
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(FileName);
    ofn.nMaxFileTitle = 0;
    ofn.lpstrFilter = L"All Files\0*.*\0\0";
    ofn.nFilterIndex = 1;

    ofn.Flags = OFN_EXPLORER | OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_FORCESHOWHIDDEN | OFN_PATHMUSTEXIST;

    if (!GetOpenFileName(&ofn)){
        FileSelected  = "false";
    }else{
        FileSelected = FileName;
    }

    CloseHandle(&ofn);
    return FileSelected;
}

// A function that runs each second to check if the internet is available or not
_bstr_t NetworkConnectionStatus(){

    _bstr_t IntStatus;
    string Check = "false";

    DWORD dwResult;
    HRESULT fr = 0;
    fr = CoInitialize(NULL);
    if (SUCCEEDED(fr)){
        INetworkListManager *networkListManager = NULL;
        fr = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (LPVOID*)&networkListManager);
        if (SUCCEEDED(fr)){
            VARIANT_BOOL vb = 0;
            fr = networkListManager->get_IsConnectedToInternet(&vb);
            if (SUCCEEDED(fr)){
                if (vb){
                    Check = "true";
                }
            }
        }
        networkListManager->Release();
    }
    CoUninitialize();

    if (Check == "true"){
        IntStatus = "true";
    }else{
        IntStatus = "false";
    }

    return IntStatus;

}

When I don't use the BrowseSelectFile() function at all, the application works fine and never has issues or errors, whether the Internet is available or not.

When I use the BrowseSelectFile() function IF the Internet is not available, the application runs perfect without issues, BUT when the application changes status by NetworkConnectionStatus() and the Internet is available, my application freezes and nothing works, not even the application's tray. I have to close it manually.

This application freeze happens also if the application is running when the Internet is available and I use the BrowseSelectFile() function, or when the Internet becomes available after I have used the BrowseSelectFile() function. It freezes after awhile, it is not an instant freeze, but after some seconds or minutes, the application totally freezes and I can't do anything other than close it manually.

What could cause such problem?

I tried many workarounds in the BrowseSelectFile() function. Maybe it holds something open or closes the main HWND after the CloseHandle(&ofn);?

It doesn`t even pop-up any error or debug messages, it just freezes.

Do I have any error in these functions that may cause such freezing?

----- UPDATE - ANSWER ----

I am terrible sorry :) these two functions are having no errors. I hope someone will find them usefull cause they work perfect at all windows versions.

Thanks to wireshark and the suggestion of @gbjbaanb i found that i was using a query to check at the server database for some values and that was triggering after the application was available to internet so it can enable some functions for internet, that query was causing this malfunction of freezing.

Thank you all for your help and time i really learned at least that debuging is a good way to go but since i am not so experienced, wireshark helped me indeed to find the error.


Solution

  • Obviously the function is calling the explorer functionality to browse network shares (and webDAV shares). If there's no internet, these calls will return immediately as "not available", with the internet, they'll be trying to browse and or discover the network contents or at least its availability.

    I'd search the internet for explorer related problems with internet shares. If you don't have any obviously available in your "my network" or "network" folders, then you may have to look at the registry and delete old entries that are still stuck there, or remove any old webdav packages that might have been installed.

    the other thing that might help is to add the OFN_NONETWORKBUTTON to your parameters (and possibly the OFN_SHAREAWARE one too).