简体   繁体   中英

How to use assert in c dll

I am using a legacy C Dll (I have the source code) that has numerous asserts scattered through the program. The dll is being used by a C# windows app.

The problem is that the "assertion failure" never shows up when there is an error in the DLL. The Dll is a console app (not sure if that's relevant). There are dozens of asserts, and AFAIK there is no easy way to get the error mesg (or flag) back to the C# app without adding a lot of extra code.

Is there a way to force the output of the assert to the screen?

Check the definition of the assert() macro in your C library. It usually has a 'pluggable' output mechanism. Worst case you have to rewrite assert() yourself.

The underlying problem here would be that a Console program has 2 output streams: normal and error. The System.Diagnostics.Process class has a StandardError property that can be used to intercept message written to the stderror stream.

Note that the defining NDEBUG turns off the assert mechanism. That identifier is defined per default in release-builds (/D-option). Check if the asserts work in the debug build of your dll and if so, edit your release-project settings to remove the /D option or any NDEBUG-definitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM