summaryrefslogtreecommitdiff
path: root/src/msg.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/msg.c')
-rw-r--r--src/msg.c48
1 files changed, 45 insertions, 3 deletions
diff --git a/src/msg.c b/src/msg.c
index ef218bf..dc7d2a4 100644
--- a/src/msg.c
+++ b/src/msg.c
@@ -1,12 +1,18 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define _WIN32_WINNT 0x400
+#include <windows.h>
+#else
+#include <errno.h>
+#endif
+
static const char *the_prefix = NULL;
const char*
@@ -107,10 +113,46 @@ p11t_msg_rv(CK_RV rv)
}
}
+#ifdef _WIN32
+
+static char last_error[1024];
+
+const char*
+p11t_msg_lasterr(void)
+{
+ LPVOID lpMsgBuf;
+ DWORD error = GetLastError();
+ DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, error,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ (LPTSTR) &lpMsgBuf, 0, NULL);
+ if(dwRet && lpMsgBuf)
+ {
+ strncpy(last_error, lpMsgBuf, 1024);
+ LocalFree(lpMsgBuf);
+ }
+ else
+ {
+ _snprintf(last_error, 1024, "unknown error: 0x%08x", (int)error);
+ }
+
+ return last_error;
+}
+
+#else /* _WIN32 */
+
+const char*
+p11t_msg_lasterr(void)
+{
+ return strerror(errno);
+}
+
+#endif /* _WIN32 */
+
void
p11t_msg_va(const char *message, va_list va)
{
- int len;
+ size_t len;
if(the_prefix)
fprintf(stdout, "%s: ", the_prefix);