is there any way to catch a C++ DLL exception in C#?

Recommended Answers

All 10 Replies

Yes. In your AssemblyInfo.cs add this:

[assembly:RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)]

Then to catch the exception:

private void button3_Click(object sender, EventArgs e)
    {
      try
      {
        //c++ call
      }
      catch (System.Runtime.CompilerServices.RuntimeWrappedException ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

im sorry but it is sill unable to catch the C++ exception despite the given solution.

private void button3_Click(object sender, EventArgs e)
        {
            try     
            
            {
                ConnectUSB("", 1, 1, 0);    // C++ call, i did DLLimport.
            }
            
            catch (System.Runtime.CompilerServices.RuntimeWrappedExceptionex)   

            {      
                MessageBox.Show(ex.Message);    
            }
}

and i have already added the code in assemblyinfo.cs

What is happening? Does the debugger give you any information, or are you seeing an error message dialog box open with the error details? Post a screenshot and output of when the operation fails.

okay. Im using a DLL written in C++, so i have to use dllimport.
i run a command. the command is to connect to a device. So when the command is run successfully, it does not have any exception from the dll. But when the command is unsuccessful, it will prompt a exception saying there's error (example USB not connected)
So what i want to do is when the it is successfully connect to the device it will prompt a messagebox saying it is successful.

private void button3_Click(object sender, EventArgs e)        
{            
try                  
{                
ConnectUSB("", 1, 1, 0);    // C++ call, i did DLLimport.            
}             
catch (System.Runtime.CompilerServices.RuntimeWrappedExceptionex)                {                      
MessageBox.Show(ex.Message);                
}

Messagebox.Show("successful","successful",MessageBoxButtom.OK,MessageBoxIcon.Information);

}

The catch is not able to catch the exception. So when is it not successful connected the exception error from the dll will prompt, after that it will just jump to the messagebox below. the exception is not being catched.

anbody can tell me that whether is it possible to catch or trap the exception that i have encounter.

thank you.

I don't think you can do anything about that. From what you're saying the DLL is invoking the message box -- so an exception happens in the C++ DLL and they show a messagebox from the DLL. So really the exception is never making it to your application. You need to contact the vendor and have them remove that prompt.

so there's no way i can trap the exception by coding??

There is no exception. It is some internal error on in the DLL that causes it to display a message box (at least that is what I have gathered from your posts). It is a design flaw on the part of the person who wrote that DLL which inhibits you from developing complete automation. You can probably hexedit the DLL and supress the message box but how you would go about doing that is beyond me. I would contact the DLL vendor. You might be able to watch for a dialog being displayed when you're running the problematic section of code and automate some kind of SendKeys task to make the dialog close itself but that isn't a "good solution" in my opinion and since a dialog can be shown you can't rely on that library in a non-gui service application.

what is try and catch in c++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.