C
C#9mo 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
arion9mo 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
kianyuenOP9mo 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
arion9mo ago
You could if necessary. It's what I've done in some of my wrapper libs
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
kianyuen
kianyuenOP9mo ago
Thanks yall.

Did you find this page helpful?