Subversion Repositories userdetect2

Rev

Blame | Last modification | View Log | RSS feed

  1. #ifndef _UD2_GUID_H_
  2. #define _UD2_GUID_H_
  3.  
  4. #include <assert.h>
  5.  
  6. // #define USE_OLE32
  7.  
  8. #ifdef USE_OLE32
  9. #pragma comment(linker, "-lOle32")
  10. #define __GUID(x) _StringToGUID(L ## x)
  11. const GUID _StringToGUID(LPCWSTR lpcstrGUID) {
  12.         GUID guid;
  13.         assert(SUCCEEDED(CLSIDFromString(lpcstrGUID, &guid)));
  14.         return guid;
  15. }
  16. #else
  17. #define __GUID(x) _StringToGUID(x)
  18. const bool StringToGUID(const char* szGUID, GUID* g) {
  19.         // Check if string is a valid GUID
  20.         if (strlen(szGUID) != 38) return false;
  21.         for (int i=0; i<strlen(szGUID); ++i) {
  22.                 char g = szGUID[i];
  23.                
  24.                 if (i == 0) {
  25.                         if (g != '{') return false;
  26.                 } else if (i == 37) {
  27.                         if (g != '}') return false;
  28.                 } else if ((i == 9) || (i == 14) || (i == 19) || (i == 24)) {
  29.                         if (g != '-') return false;
  30.                 } else {
  31.                         if (!((g >= '0') && (g <= '9')) && !((g >= 'A') && (g <= 'F')) && !((g >= 'a') && (g <= 'f'))) {
  32.                                 return false;
  33.                         }
  34.                 }
  35.         }
  36.        
  37.         char* pEnd;
  38.     g->Data1 = strtoul(szGUID+1,&pEnd,16);
  39.     g->Data2 = strtoul(szGUID+10,&pEnd,16);
  40.     g->Data3 = strtoul(szGUID+15,&pEnd,16);
  41.         char b[3]; b[2] = 0;   
  42.         memcpy(&b[0], szGUID+20, 2*sizeof(b[0])); g->Data4[0] = strtoul(&b[0], &pEnd, 16);
  43.         memcpy(&b[0], szGUID+22, 2*sizeof(b[0])); g->Data4[1] = strtoul(&b[0], &pEnd, 16);
  44.         for (int i=0; i<6; ++i) {
  45.                 memcpy(&b[0], szGUID+25+i*2, 2*sizeof(b[0])); g->Data4[2+i] = strtoul(&b[0], &pEnd, 16);
  46.         }
  47.         return true;
  48. }
  49. const GUID _StringToGUID(const char* szGUID) {
  50.         GUID g;
  51.         assert(StringToGUID(szGUID, &g));
  52.         return g;
  53. }
  54. #endif
  55.  
  56. #endif
  57.  
  58.