Discussion:
function to turn an error number into an error message?
(too old to reply)
T
2020-01-06 04:59:26 UTC
Permalink
Hi All,

https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified.

When I already have the error number (2), what is the
proper function to use to get the error string ("The
system cannot find the file specified")?

Is it this one?

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew

The "va_list" seems to be a stopper. I just want back the string.

Many thanks,
-T
R.Wieser
2020-01-06 07:41:16 UTC
Permalink
T,
what is the proper function to use to get the error string ("The system
cannot find the file specified")?
Is it this one?
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew
Yes.
The "va_list" seems to be a stopper. I just want back the string.
What I'm using:

DWORD dwFlags, // FORMAT_MESSAGE_FROM_SYSTEM or
FORMAT_MESSAGE_IGNORE_INSERTS
LPCVOID lpSource, // 0
DWORD dwMessageId, // The error code
DWORD dwLanguageId, // 0
LPWSTR lpBuffer, // RW ptr to the string buffer
DWORD nSize, // RW ptr to variable holding available
string buffer size
va_list *Arguments // null

Hope that helps.

Regards,
Rudy Wieser
T
2020-01-06 20:36:27 UTC
Permalink
Post by R.Wieser
DWORD dwFlags, // FORMAT_MESSAGE_FROM_SYSTEM or
FORMAT_MESSAGE_IGNORE_INSERTS
LPCVOID lpSource, // 0
DWORD dwMessageId, // The error code
DWORD dwLanguageId, // 0
LPWSTR lpBuffer, // RW ptr to the string buffer
DWORD nSize, // RW ptr to variable holding available
string buffer size
va_list *Arguments // null
I am copying this down into my comments on the
calling function!

Thank you!
T
2020-01-06 21:23:18 UTC
Permalink
Post by R.Wieser
T,
what is the proper function to use to get the error string ("The system
cannot find the file specified")?
Is it this one?
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew
Yes.
The "va_list" seems to be a stopper. I just want back the string.
DWORD dwFlags, // FORMAT_MESSAGE_FROM_SYSTEM or
FORMAT_MESSAGE_IGNORE_INSERTS
LPCVOID lpSource, // 0
DWORD dwMessageId, // The error code
DWORD dwLanguageId, // 0
LPWSTR lpBuffer, // RW ptr to the string buffer
DWORD nSize, // RW ptr to variable holding available
string buffer size
va_list *Arguments // null
Hope that helps.
Regards,
Rudy Wieser
Hi R.!

Based on your code, I dropped

FORMAT_MESSAGE_ALLOCATE_BUFFER

from $dwFlags and instead of getting back the pointer,
I got back


lpBuffer 84 0 104 0 101 0 32 0 115 0 121 0 115 0 116 0 101 0 109 0 32 0
99 0 97 0 110 0 110 0 111 0 116 0 32 0 102 0 105 0 110 0 100 0 32 0 116
0 104 0 101 0 32 0 102 0 105 0 108 0 101 0 32 0 115 0 112 0 101 0 99 0
105 0 102 0 105 0 101 0 100 0 46 0 13 0 10 0 0

Which is little endian UTF16. YIPPEE!!!

Do you realize that I must have looked at over fifty
examples of this writen in C and C++ adn ALL of them
used FORMAT_MESSAGE_ALLOCATE_BUFFER

DDDDUUUUUUDDDDDEEEE!!!!!! You are my hero!!!!

-T
R.Wieser
2020-01-07 08:02:23 UTC
Permalink
T,
Post by T
Based on your code, I dropped
FORMAT_MESSAGE_ALLOCATE_BUFFER
from $dwFlags and instead of getting back the pointer,
I got back
[snip]

I'm glad it worked for you. :-)

But ... It should work as well when using FORMAT_MESSAGE_ALLOCATE_BUFFER.
The created buffer is than returned in the variable "lpBuffer" points to.
Just don't forget to "LocalFree( )" the buffer after you're done with it
(otherwise you have a memory leak)

Regards,
Rudy Wieser
T
2020-01-07 14:41:02 UTC
Permalink
Post by R.Wieser
"LocalFree( )"
I am checking with Raku's NativeCall folks to see if
they do that automatically or if I have to do it myself.

Thank you!
R.Wieser
2020-01-07 15:14:52 UTC
Permalink
T,
Post by T
I am checking with Raku's NativeCall folks to see if
they do that automatically or if I have to do it myself.
You'll have to do it yourself I'm afraid. Thats why I normally use a
(local) buffer instead.

Regards,
Rudy Wieser
T
2020-01-07 15:24:05 UTC
Permalink
Post by R.Wieser
T,
Post by T
I am checking with Raku's NativeCall folks to see if
they do that automatically or if I have to do it myself.
You'll have to do it yourself I'm afraid. Thats why I normally use a
(local) buffer instead.
Regards,
Rudy Wieser
It is like pulling teeth to get information out of those
folks at times. I may make the call "just because"

T
2020-01-06 21:43:00 UTC
Permalink
Post by R.Wieser
DWORD nSize, // RW ptr to variable holding available
Hi R.,

Curious. I always get a 0 back in $nSize.

I can count the words in the string myself, stopping
at the double nul, but then again I really do not need to.

And sending it a bogus error numbers gives me all nuls
back in the string, so I will throw an error on that.

Do you get back a byte or word count in $nSize?

-T
T
2020-01-06 22:54:05 UTC
Permalink
Do you get back a byte or word count in $nSize?
I declared the return wrong

:'(
Loading...