C
C#2mo ago
kianyuen

How should Error Status Code returned from a DLLImport function be handled?

//Output: None
//return: 0 - success, <0 - failure(refer to error code definition)
//Note: None
[DllImport("./libraries/something.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int OpenDevice(int nPort, int nBaud);
//Output: None
//return: 0 - success, <0 - failure(refer to error code definition)
//Note: None
[DllImport("./libraries/something.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int OpenDevice(int nPort, int nBaud);
I am thinking defining a different function that wrap it and throw exception. Or should I just use the function as is?
5 Replies
arion
arion2mo ago
From what I've seen, ppl use enums for the error codes, with 0 being success eg. https://github.com/CorsairOfficial/cue-sdk/blob/master/src%2Finclude%2FiCUESDK.h#L22
kianyuen
kianyuen2mo ago
Is there any specific practice regarding using the function when imported it in your app? Like should I make a function to wrap it and throw exception with the error code, if it isnt zero?
arion
arion2mo ago
You could if necessary. It's what I've done in some of my wrapper libs
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
kianyuen
kianyuen2mo ago
Thanks yall.
Want results from more Discord servers?
Add your server