SOL9 2.0 Class: Guid

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * Guid.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9 2.0
// 2010/12/16 Added a static 'toString' method

/*
    typedef struct _GUID {
        DWORD  Data1;      // 4
        WORD   Data2;      // 2
        WORD   Data3;      // 2
        BYTE   Data4[8];   // 1~8
    } GUID, UUID;
*/


#pragma once

#include <sol\Object.h>
#include <sol\StringT.h>
#include <sol\Bytes.h>
#include <sol\StringConverter.h>


#pragma comment(lib, "ole32.lib")

/**
 * Guid class
 */

namespace SOL {

class Guid :public Object {

public:
    /**
     */
    Guid() {
    }

public:
    /**
     * Create a new GUID of bytes of the form:
     *    43 09 61 9F 19 58 B1 40 95 F6 91 A2 13 79 D7 F0,
     * and return a string like: "{9F610943-5819-40B1-95F6-91A21379D7F0}"
     *
     * @param id    Reference to String. GUID of string format something like:
     *                "{9F610943-5819-40B1-95F6-91A21379D7F0}"
     */
    bool create(__out StringT<TCHAR>& id) { 
        bool rc = false;
        GUID guid;
        if (SUCCEEDED(CoCreateGuid(&guid))) {
            /*
            int len = sizeof(guid);
            unsigned char* data = new unsigned char[len];
            memcpy(data, &guid, len);
            ByteArray ba;
            ba.shallowCopy(data, len);
            ba.dump();
            */
            wchar_t buff[128];
            if(StringFromGUID2(guid, buff, SizeOf(buff))) {
        
                StringConverter converter;
                converter.convert(buff,id);
                //char* tmp = converter.toMultiByte(buff);
                //id.shallowCopy(tmp);

                rc = true;
            }
        }
        return rc;
    }

public:
    /**
     * Create a new GUID of bytes of the form:
     *    43 09 61 9F 19 58 B1 40 95 F6 91 A2 13 79 D7 F0,
     * and return it
     *
     * @param ba    Reference to ByteArray. GUID of byte format something like:
     *                43 09 61 9F 19 58 B1 40 95 F6 91 A2 13 79 D7 F0
     */
    bool create(Bytes& ba) { 
        bool rc = false;
        GUID guid;
        if (SUCCEEDED(CoCreateGuid(&guid))) {
            int len = sizeof(guid);
            unsigned char* data = new unsigned char[len];
            memcpy(data, &guid, len);
            ba.shallowCopy(data, len);
            rc = true;
        }
        return rc;
    }

public:
    /**
     * @param guid  GUID
     * @param id    Reference to String. GUID of string format something like:
     *                "{9F610943-5819-40B1-95F6-91A21379D7F0}"
     */
    //2010/12/16
    static const TCHAR* toString(__in GUID guid, __out StringT<TCHAR>& id) { 
        id = _T("");

        wchar_t buff[128];
        if(StringFromGUID2(guid, buff, SizeOf(buff))) {
        
            StringConverter converter;
            converter.convert(buff,id);
        }
        return (const TCHAR*)id;
    }
};

}


Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.