Wrox Programmer Forums
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2003, 10:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating a GUID

I have been looking at ways to create a GUID. Below is the code I plan on implementing. Since, I am still cutting my teeth on C++. I wanted to get some feed back.

Thanks in advance for your help/input.

CString BuildGUID()
{
    long lRetVal = 0;
    GUID *oGuid;
    CString strPartOne;
    CString strPartTwo;
    CString strPartThree;
    CString strPartFour;
    CString strGUID;

    // Int variables
    strGUID = " ";
    oGuid = new GUID;
    // Call the API Function to get the GUID
    lRetVal = CoCreateGuid(oGuid);
    // Build the guid
    if (lRetVal == 0)
    {
        // Get the first 8 characters
        strPartOne.Format("%X_", oGuid->Data1);
        // Get the next 4 characters
        strPartTwo.Format("%X_", oGuid->Data2);
        // Get the next 4 characters
        strPartThree.Format("%X_", oGuid->Data3);
        // Get the next 6 characters
        strPartFour.Format("%X", oGuid->Data4);
        // Build the GUID
        strGUID = strPartOne + strPartTwo + strPartThree + strPartFour;
    }
    // Return the GUID
    return strGUID;
}

Larry Asher
__________________
Larry Asher
 
Old May 6th, 2004, 11:10 AM
Registered User
 
Join Date: May 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to hokukim
Default

Not sure if that'll work...try this one. Be sure to includ rpcrt4.lib as an additional dependency.

    // generate new guid
    GUID Guid;
    if(!(::CoCreateGuid(&Guid) == S_OK))
        return ERROR;

    // convert guid to CString
    CString GuidStr;
    BYTE* Str;
    UuidToString((UUID*)&Guid, &Str);
    GuidStr.Format("{%s}", (LPTSTR)Str);
    RpcStringFree(&Str);






Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating GUID in Javascript rbd Javascript 8 November 20th, 2007 12:14 PM
Ask IIS for a GUID? Raconteur ASP.NET 2.0 Professional 2 April 25th, 2007 03:25 PM
GUID cdhansen BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 October 21st, 2006 04:56 AM
generating two different GUID shaileshmark General .NET 5 August 1st, 2004 01:25 PM
Dynamic GUID? Klavs Pedersen Visual C++ 1 October 15th, 2003 12:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.