GUIDGEN Sample: Generates Globally Unique Identifiers (GUIDs)

The GUIDGEN sample is a simple dialog-based MFC application that can help you as you code ActiveX applications. GUIDGEN can be used to generate globally unique identifiers, or GUIDs, that identify classes, objects, and interfaces.

Aside from being a dialog-box based application, GUIDGEN also shows how MFC applications can paste text data to the Clipboard. The CGuidGenDlg::OnOK function in the GUIDGDLG.CPP file shows how OpenClipboard and SetClipboardData can be called to accomplish this task. The application also demonstrates calling the CoCreateGuid API to request a new GUID from the operating system. Look for this code in CGuidGenDlg::OnNewguid, which is also in the GUIDGDLG.CPP file.

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Running the Sample

If you have installed the Microsoft Visual C++ product, the GUIDGEN utility (GUIDGEN.EXE) is installed by default in \\Microsoft Visual Studio .NET 2003\Common7\Tools.

The radio buttons in the GUID Format group box determine the format of the GUID the program will generate.

  • The first format is designed to be used in invocations of MFC's IMPLEMENT_OLECREATE macro.

  • The second format generates a reference to the DEFINE_GUID macro.

The remaining formats are appropriate for statically allocated GUIDs and GUIDs included in registry entries or registry editor scripts.

Once you have selected the format for your GUID, you can copy it to the Clipboard with the Copy button. You can return to your editor and paste the GUID code into your application's source. When you need to generate a new GUID, click the NewGuid button.

Keywords

This sample demonstrates the following keywords:

AfxGetApp; AfxMessageBox; CDC::DrawIcon; CDC::GetSafeHdc; CDialog::DoModal; CDialog::EndDialog; CDialog::OnInitDialog; CDialog::OnOK; CMenu::AppendMenu; CRect::Height; CRect::Width; CString::Format; CString::GetLength; CString::IsEmpty; CString::LoadString; CWinApp::ExitInstance; CWinApp::InitInstance; CWinApp::SetRegistryKey; CWnd::CenterWindow; CWnd::DoDataExchange; CWnd::GetClientRect; CWnd::GetParent; CWnd::GetSystemMenu; CWnd::IsIconic; CWnd::OnPaint; CWnd::OnQueryDragIcon; CWnd::OnSysCommand; CWnd::OpenClipboard; CWnd::SendMessage; CWnd::SetDlgItemText; CWnd::UpdateData; CloseClipboard; CoInitialize; CoUninitialize; EmptyClipboard; GetForegroundWindow; GetProfileInt; GetSystemMetrics; GlobalAlloc; GlobalLock; GlobalUnlock; LoadIcon; SetClipboardData; memcpy

Note

Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.

See Also

Other Resources

MFC Samples