[ovs-dev] [PATCH 2/4] util: A generic function to convert error to string for windows.

Gurucharan Shetty shettyg at nicira.com
Fri Jan 17 20:26:21 UTC 2014


More users will be added in an upcoming commit.

Signed-off-by: Gurucharan Shetty <gshetty at nicira.com>
---
 lib/entropy.c |   11 +----------
 lib/util.c    |   13 +++++++++++++
 lib/util.h    |    5 +++++
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/lib/entropy.c b/lib/entropy.c
index fd73566..bdde607 100644
--- a/lib/entropy.c
+++ b/lib/entropy.c
@@ -62,16 +62,7 @@ get_entropy(void *buffer, size_t n)
                         PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
     if (!CryptGenRandom(crypt_prov, n, buffer)) {
         error = EINVAL;
-        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
-                      | FORMAT_MESSAGE_FROM_SYSTEM
-                      | FORMAT_MESSAGE_IGNORE_INSERTS,
-                      NULL,
-                      GetLastError(),
-                      0,
-                      (LPTSTR)&msg_buf,
-                      0,
-                      NULL
-            );
+        ovs_lasterror_to_string((LPTSTR)&msg_buf);
         VLOG_ERR("CryptGenRandom: read error (%s)", msg_buf);
         LocalFree(msg_buf);
     }
diff --git a/lib/util.c b/lib/util.c
index 0ebf085..da09286 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -1649,3 +1649,16 @@ exit:
     return ok;
 }
 
+#ifdef _WIN32
+
+/* Calls FormatMessage() with GetLastError() as an argument and stores the
+ * output in 'buffer'. The caller is responsible for freeing the memory
+ * pointed to by 'buffer' using LocalFree(). */
+void
+ovs_lasterror_to_string(LPSTR buffer)
+{
+    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
+                  | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0,
+                  buffer, 0, NULL);
+}
+#endif
diff --git a/lib/util.h b/lib/util.h
index 8886a54..0b2efce 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -490,6 +490,11 @@ void bitwise_put(uint64_t value,
 uint64_t bitwise_get(const void *src, unsigned int src_len,
                      unsigned int src_ofs, unsigned int n_bits);
 
+#ifdef _WIN32
+
+void ovs_lasterror_to_string(LPSTR buffer);
+#endif
+
 #ifdef  __cplusplus
 }
 #endif
-- 
1.7.9.5




More information about the dev mailing list