mardi 1 avril 2014

Where to define error code constants in C++


Vote count:

0




I have exported a method from a C++ DLL and then call it from a VB.net forms application. The C++ method currently has no return value (void) but I want to improve it and return an int that represents a series of error codes. (I plan to return zero if all went well.)


Where and how is the best place to define these error codes?


Should I do the following at the top of my CPP file:



#define ERR_NEGATIVE_CELL_SIZE 1
#define ERR_INVALID_FILE_PATH 2
etc


The VB.net application will also define these same codes and then show UI messages to the user based on the code.


Obviously I would prefer to throw an exception in the DLL and catch it (along with the relevant exception message) in VB.net, but this doesn't seem to be possible using the extern "C" __declspec(dllexport) method.


Happy to hear about alternative design patterns. I also plan to expose the DLL methods via a C++ console executable, so storing the error messages once in the DLL and having them available to both the console and UI applications is ideal.



asked 6 mins ago


1 Answer



Vote count:

0




Please avoid the preprocessor wherever possible.


For your scenario, defining an enum would be reasonable. Define it next to the function prototype.



answered 58 secs ago





Aucun commentaire:

Enregistrer un commentaire