home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OCT_GUI.PAK / GUIDGEN.CPP next >
C/C++ Source or Header  |  1995-08-29  |  2KB  |  51 lines

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