Jump to content
Excelsior Forums
mlottman

FormatMessage:display last win errorcode as string

Recommended Posts

Anyone have an example of how to use xFunction to obtain the String message of the last windows error code?  I can obtain the error code fine, by using:

       xFunction f = new xFunction("kernel32","int GetLastError()");

       return ((Integer) f.invoke()).intValue();

but when it comes to using FormatMessage to gets its string representation I run into trouble defining the buffer that the message is returned thru (lpBuffer).  The function indicates that it wrote 24 characters to the buffer, but whenever I try to print the buffer, it is garbage characters.  The Microsoft signature of the method is:

DWORD FormatMessage(

   DWORD dwFlags,

   LPCVOID lpSource,

   DWORD dwMessageId,

   DWORD dwLanguageId,

   LPTSTR lpBuffer,

   DWORD nSize,

   va_list* Arguments)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp

Anyone have any examples of its proper use in xFunction?

Thanks,

Marc

 

Share this post


Link to post
Share on other sites

Please provide the code snapshot you use to invoke FormatMessage. Possible reasons of the problem are:

1) using Java type 'long' instead of 'int' for DWORD

2) using of CSTRING instead of UNICODESTRING for lpBuffer (or vice versa, depending of what version is called: FormatMessageA or FormatMessageW).

The proper signature for the lpBuffer is either 'CSTRING*' or 'UNICODESTRING*'.

If you do not specify flag FORMAT_MESSAGE_ALLOCATE_BUFFER, the possible code constructing lpBuffer of 1000 charcaters is:

 ?Argument str = new Argument("", Argument.UNICODESTRING, 1000);
?Pointer lpBuffer = str.createPointer();
...
?//after function invokation:
?String result = (String)str.getValue();

Of cause, the code snippet is not tested and is provided "as is", but it can give you some ideas how to solve your task.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×