• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Termination or Exception in win 32 Dll

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?

Thanks
0
yoyumkj
Asked:
yoyumkj
  • 2
2 Solutions
 
SteHCommented:
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.
0
 
MiniDisc_2k2Commented:
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:

try
{
     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
0
 
MiniDisc_2k2Commented:
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.

Thanks,
-- Matt
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now