Termination or Exception in win 32 Dll

Posted on 2004-11-17
Last Modified: 2008-02-01
I need some help to figure out the termination problem.
I used "throw A" to termination the program when some constaints do not match.
For example,

double __stdcall PP(const char* A)
   if ( *A != 'r')
   throw "A";

After I tried this way and created dll to call from excel and when I call the dll from excel, it shows error message and terminated the excel for some reasons.
How can I use the termination in this case?

Question by:yoyumkj
    LVL 13

    Accepted Solution

    Are you handling the exception you are throwing? If the OS gets the exception it won't be able to handle it and thus terminate.
    Or post the code where you use the function PP.
    LVL 1

    Assisted Solution

    Are you actually catching that throw? Let's say excel calls it... eventually you reach

    PP("some stuff");  // function throws here
    // Some more code that doesn't get executed because of the unhandled throw

    You need to catch that throw yourself. Something like a simple message box should do. Assuming Win32 API:

         PP("some stuff"); // function throws
    catch(const char *err)
        char* errmsg = new char[200];
        strcpy(errmsg, "An error occurred attempting to cal PP(). The error message was ");
        strcat(errmsg, err);
        strcat(errmsg, ". Continue execution?");
        if (AfxMessageBox(errmsg, MB_YESNO|MB_ICONEXCLAMATION) == IDNO)
              delete errmsg;
              throw; // or for a more graceful exit, abort().
        delete errmsg;
    // Some more code -- this time it executes because it continued (if requested)

    One more thing. I think I see an error in your code in that function. Did you really mean

    > if ( *A != 'r')

    A is a string. As such, you cannot compare it with an =. It's more unlikely that you intended to compare it with a character, and not a string representation of a character. Did you mean this?

    if (strcmp(A, "r"))

    Remember that strcmp returns 0 if the strings match. If you want to see if they match, put a ! before the strcmp(), but from your code, I think you're checking to see if they don't match.

    Good luck!
    -- MiniDisc
    LVL 1

    Expert Comment

    I think that both SteH and I were correct in assuming that the throw was not caught and thus caused the problem, so perhaps a split of points between us two would be warranted?

    I could have also been right with the problem using == instead of strcmp()... either way, I think the question has been answered correctly and the points should somehow be distributed if the asker doesn't respond.

    -- Matt

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now