home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OCT_GUI.PAK / GUIDGEN.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  1.8 KB  |  52 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
  4. //
  5. // Tool to generate a GUID from the network card ID, using the OLE 2 API func.
  6. //----------------------------------------------------------------------------
  7. #define STRICT
  8. #include <windows.h>
  9. #include <string.h>
  10. #include <compobj.h>
  11.  
  12. int CALLBACK __export
  13. GuidProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM /*lParam*/)
  14. {
  15.   if (msg == WM_COMMAND && wParam != 'G')   // ignore msgs from edit control
  16.     ::EndDialog(hDlg, TRUE);                // quit on OK or ESC or Alt-F4
  17.   else if (msg != WM_INITDIALOG)
  18.     return 0;
  19.   else {                                    // do all the work at INITDIALOG
  20.     GUID id;
  21.     IMalloc* alloc;
  22.     char far* guidStr = 0;
  23.     ::CoInitialize(0);                      // init OLE to get memory for GUID
  24.     ::CoGetMalloc(MEMCTX_TASK, &alloc);
  25.     ::CoCreateGuid(&id);
  26.     ::StringFromCLSID(id, &guidStr);
  27.     if (guidStr) {
  28.       ::OpenClipboard(hDlg);                // copy GUID string to clipboard
  29.       ::EmptyClipboard();
  30.       HANDLE cbhdl = ::GlobalAlloc(GHND,lstrlen(guidStr)+1);
  31.       char far* cbdata = (char far*)::GlobalLock(cbhdl);
  32.       lstrcpy(cbdata, guidStr);
  33.       ::GlobalUnlock(cbhdl);
  34.       ::SetClipboardData(CF_TEXT, cbhdl);
  35.       ::CloseClipboard();
  36.       ::SetDlgItemText(hDlg, 'G', guidStr); // show GUID, overwrites error msg
  37.     }
  38.     if (alloc) {
  39.       alloc->Free(guidStr);
  40.       alloc->Release();
  41.     }
  42.     ::CoUninitialize();
  43.   }
  44.   return 1;
  45. }
  46.  
  47. int far PASCAL
  48. WinMain(HINSTANCE hInst, HINSTANCE/*hPrev*/, char far*/*cmdLine*/, int/*show*/)
  49. {
  50.   return ::DialogBox(hInst, "GuidDlg", 0,(DLGPROC)GuidProc);
  51. }
  52.