Skip to content

Instantly share code, notes, and snippets.

@tjfontaine
Created November 19, 2013 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjfontaine/7539324 to your computer and use it in GitHub Desktop.
Save tjfontaine/7539324 to your computer and use it in GitHub Desktop.
diff --git a/src/uuid.cc b/src/uuid.cc
index ff0d6ec..9ab9647 100644
--- a/src/uuid.cc
+++ b/src/uuid.cc
@@ -2,14 +2,29 @@
#include <node.h>
#include <node_object_wrap.h>
#include <assert.h>
+
+#ifdef _WIN32
+#include <Objbase.h>
+typedef GUID uuid_t;
+#else
#include <uuid/uuid.h>
+#endif
+
#include <ctype.h>
#undef UUID_STR_LEN
#define UUID_STR_LEN 36
+
static inline void my_uuid_unparse_lower(uuid_t in, char *out) {
int i;
+#ifdef _WIN32
+ int j;
+ wchar_t ostr[UUID_STR_LEN + 3];
+ StringFromGUID2(in, (LPOLESTR)ostr, UUID_STR_LEN + 3);
+ for(i=1, j=0; i < UUID_STR_LEN + 1; i++, j++) out[j] = tolower(ostr[i]);
+#else
uuid_unparse(in, out);
- for(i=0;i<36;i++) out[i] = tolower(out[i]);
+ for(i=0;i<UUID_STR_LEN;i++) out[i] = tolower(out[i]);
+#endif
}
using namespace v8;
@@ -28,14 +43,21 @@ class libuuid {
protected:
static Handle<Value> Create(const Arguments& args) {
- uuid_t id;
HandleScope scope;
- char uuid_str[37];
+ uuid_t id;
+ char uuid_str[UUID_STR_LEN + 1];
+
+#ifdef _WIN32
+ if (CoCreateGuid(&id) != S_OK) {
+ v8::ThrowException(v8::Exception::Error(String::New("Failed to create GUID")));
+ return scope.Close(Undefined());
+ }
+#else
uuid_generate(id);
+#endif
my_uuid_unparse_lower(id, uuid_str);
String::New(uuid_str);
-
return scope.Close(String::New(uuid_str));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment