0

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.

Vas
  • 75
  • 9
  • 1
    Break into the debugger when the app is frozen. What is the `BrowseSelectFile` thread doing? – Raymond Chen Apr 20 '18 at 14:03
  • @Raymond it doesn`t pop-up anything it just freezes i cant open the debug issue and i am not so experienced to open the dbg file and check it without knowing where to look for – Vas Apr 20 '18 at 14:09
  • 2
    @Vas run the debug version from within Visual Studio and when it freezes do Debug->Break All – Jabberwocky Apr 20 '18 at 14:10
  • 1
    @Vas - if you're using visual studio (and if not, why not!) run it in debug (press F5), If running externally, wait for it to freeze and then use the menu to "attach to process", then break. You can use WinDBG to do the same, but you'll have to learn the commands to use to view stack traces and the like (which is well worth the effort) – gbjbaanb Apr 20 '18 at 14:12
  • Okay thanks for the tip i will try it too – Vas Apr 20 '18 at 14:13
  • 3
    GetOpenFileName() can easily cause deadlock problems, it loads a bunch of shell extensions into your process. Programmer machines tend to have a lot of them, not always of the best quality. If you then also call CoInitialize() in a function then you certainly make it a lot worse, those shell extensions need you to call it in your program entrypoint and not call CoUninitialize() until your program terminates. – Hans Passant Apr 20 '18 at 14:24
  • @HansPassant is there a way to avoid such functions and replace them with better ones ? i can't understand where the conflict is and the debug info doesn`t help me much for the moment – Vas Apr 20 '18 at 14:35
  • You could replace your machine with a better one. But fix the CoInitialize() bug first. – Hans Passant Apr 20 '18 at 14:37
  • I should use CoInitialize() and CoUninitialize() only at my main function ? and just use the networklist manager in the above function ? – Vas Apr 20 '18 at 14:38
  • 1
    I noticed that your `NetworkConnectionStatus` function calls `CoUninitialize` even if `CoInitialize` failed. This seems bad. (Also, I don't understand why you can't connect a debugger to your program to see why it's stuck. How do you debug your program without a debugger? Just connect the debugger and see why the program is stuck. There's no point trying to speculate.) – Raymond Chen Apr 21 '18 at 11:40
  • Please dont add answer in your question. Add it as an answer – Suraj Rao Apr 21 '18 at 13:43

1 Answers1

2

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).

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • Thanks for the advice i am testing it right now, i was using the OFN_NONETWORKBUTTON before but i removed it cause i though it could cause problems, i ll tell you in a minute if the shareware acts good – Vas Apr 20 '18 at 14:11
  • :( Nope it does the same thing, it freezes after some seconds again. – Vas Apr 20 '18 at 14:13
  • After many tests and workarounds i think that it is not the CoInitialize() that produces the error but BrowseSelectFile() function. I ve tried every possible way of CoInitiaze() in the correct ways but anything tried cames up that after i use the File Open Dialog to select a file after some seconds or minutes that i have selected a file the application freezes, so i think @gbjbaanb is perfectly right about explorer bugs or something similar is going on, though i thought also that CoInitiliaze() would cause such behavior but its not, i can run 1 full day the CoInitialize without errors. – Vas Apr 20 '18 at 22:08
  • Does anyone knows other workable ways of Opening a file dialog in windows systems and choose a file without having such errors ? Any link or search to look for would be nice. By the way, i am also using the Open Folder Dialog that has a Callback function and it doesn't have issues or malfunctions but i cant find any relevant for the Open File Dialog with a Callback so it doesn't stuck and freezze the application maby – Vas Apr 20 '18 at 22:11
  • 1
    @Vas if you really think its not your explorer extensions, why not run wireshark and see what the computer is trying to do on the network when you call this function. And do what Raymond says, run it in the debugger and prove your assumptions. – gbjbaanb Apr 21 '18 at 11:52
  • I don`t have such experience on debuging so that option of Raymond will take more time and the question wont be that long open but sure i am on it to learn how to debug correctly, about wireshark or other program i will try to find something. – Vas Apr 21 '18 at 12:51
  • @Vas: If this answer doesn't answer your question, don't accept it. It's probably a good idea to take the [tour] and visit the [help]. It'll explain, how this place is supposed to work. – IInspectable Apr 21 '18 at 20:26