Question

I just modified my C++/CLI project to change the way it was parsing its config.ini text file. But it still accesses the file on the disk just as before:

StreamReader ^sr = gcnew StreamReader(CONFFILE_NAME);
String ^rdfl = sr->ReadToEnd();
sr->Close();

Now it won't run outside of the VS2008Express environment. It is a WinForm pretending to be a service and should stay in the notification tray but instead it just flashes its default icon there until I roll my mouse over it, a sure sign it has already terminated abnormally. The default icon should immediately be updated btw so I will check upto that point again. I've just tried a clean and rebuild.

Thanks.

_EDIT_

It runs fine if the config file is not there or is incompatible - a window appears and I enter the correct config and off she goes. But it reloads config data from a file from within VS2008 and it used to from without (albeit with a major logical error in the coding).

Here's the code immediately following that posted above:

    int fore = 0;
    int aft;
    while (statsRead < 4) {
        fore = rdfl->IndexOf(':', aft)+1;
        if (fore == -1) break;
        aft = rdfl->IndexOf('\n', fore);
        if (aft == -1) break;
        if (statsRead == 0) str1 = rdfl->Substring(fore, aft-fore)->Trim();
        else if (statsRead == 1) str2 = rdfl->Substring(fore, aft-fore)->Trim();
        else {
            String ^tmpIntStr = rdfl->Substring(fore, aft-fore)->Trim();
            int tmp_int;
            if (Int32::TryParse(tmpIntStr, tmp_int)) {
                switch (statsRead) {
                    case 2: int1 = tmp_int; break;
                    case 3: int2 = tmp_int; break;
                }
            } else break;
        }
        ++statsRead;
    }
}

afterwards any remaining stats are given default values which work in my case, apart from the first which triggers the window.

_EDIT_

I've updated the above with try and catch blocks as advocated by Hans and also the MSDN literatur for StreamReader but still it fails, the stack trace (thanks, Hans) indicates the fault is my while statement (no thanks, MS)

if (File::Exists(CONFFILE_NAME)) {
    String ^rdfl;
    try {
        StreamReader ^sr = gcnew StreamReader(CONFFILE_NAME);
        try{
            rdfl = sr->ReadToEnd();
        } finally {
            if ( sr )
                delete (IDisposable^)sr;
        }
    } catch (Exception ^ex) {
        MessageBox::Show(ex->Message);
    }
    int fore = 0;
    int aft;
    while (statsRead < 4) {
        fore = rdfl->IndexOf(':', aft)+1;
        if (fore == -1) break;
        aft = rdfl->IndexOf('\n', fore);
        if (aft == -1) break;
        if (statsRead == 0) str1 = rdfl->Substring(fore, aft-fore)->Trim();
        else if (statsRead == 1) str2 = rdfl->Substring(fore, aft-fore)->Trim();
        else {
            String ^tmpIntStr = rdfl->Substring(fore, aft-fore)->Trim();
            int tmp_int;
            if (Int32::TryParse(tmpIntStr, tmp_int)) {
                switch (statsRead) {
                    case 2: int1 = tmp_int; break;
                    case 3: int2 = tmp_int; break;
                }
            } else break;
        }
        ++statsRead;
    }
}
Was it helpful?

Solution

This is typically a simple mistake, like CONFFILE_NAME not being a full path name and the default working directory not set where you hope it is. Focus on getting a debugger attached. That's easy in .NET, use System::Diagnostics::Debugger::Launch() in your Main() method for example. And write an event handler for AppDomain::CurrentDomain->UnhandledException so that exceptions don't fall in the bit bucket without notice. Be sure to remove try/catch statements that shouldn't be there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top