开发者

Combining the value of GetLastError and a custom error message

I have a function that returns a different DWORD value for each case there is an error. So I have the following defines:

#define ERR_NO_DB_CONNECTION    0x90000
#define ERR_DB_NOT_OPEN         0x90001
#define ERR_DB_LOCKED           0x90002
#define ERR_DB_CONN_LOST        0x90003

Now, I return those values when an error occurs. I need to also return the value of GetLastError in the same return.

No, I can't read it later.

I tried combining it different ways, eg:

return ERR_DB_NOT_OPEN + GetLastError();

and then extract the error by subtracting the value of ERR_DB_NOT_OPEN but since I need to use this in functions where ther开发者_高级运维e can be several return values it can get quite complex to do that.

Is there any way to achieve this? I mean, combine the value + GetLastError and extract them later? Code is appreciated.

Thanks

Jess.


I know this post is old, but just in case... Complementing Mark's answer. Following code region is available for you to define your own errors.

Error codes are 32-bit values (bit 31 is the most significant bit). Bit 29 is reserved for application-defined error codes; no system error code has this bit set. If you are defining an error code for your application, set this bit to indicate that the error code has been defined by your application and to ensure that your error code does not conflict with any system-defined error codes.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms680627%28v=vs.85%29.aspx


According to Microsoft's documentation, the system error codes max out at 15999 (0x3E7F). This means you have the entire upper word to play with. You'll need to shorten your error codes to fit into 4 hex digits, then you can use some Windows macros to combine and split them:

return MAKELPARAM(GetLastError(), ERR_DB_NOT_OPEN);

int lasterror = LOWORD(result);
int code = HIWORD(result);


You can combine them into a string (a char array) and then split them from the caller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜