home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / olemode / NewGuid.java < prev   
Encoding:
Java Source  |  2000-05-04  |  2.7 KB  |  96 lines

  1. /**
  2.  * (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3.  *
  4.  *  This application is similar to the GUIDGEN utility which
  5.  *  generates unique guids and puts them on the clipboard.
  6.  *  Unlike GUIDGEN, this version parses the GUID without
  7.  *  braces (useful for pasting into IDL files.)
  8.  *
  9.  *  In addition to showing how to use the "ole" keyword to
  10.  *  assist with Ole's special calling conventions, it shows how
  11.  *  even raw memory access, when necessary, can be made easy
  12.  *  using @dll.import.
  13.  */
  14.  
  15.  
  16. import com.ms.dll.*;
  17. import com.ms.com._Guid;
  18.  
  19.  
  20. // Use the "auto" linktype on all methods except where specifically
  21. // overridden.
  22. /** @dll.import(auto) */
  23. public class NewGuid
  24. {
  25.  
  26.     public static void main(String args[])
  27.     {
  28.         _Guid g = CoCreateGuid();
  29.         String text = StringFromIID(g);
  30.         text = text.substring(1);
  31.         text = text.substring(0, text.indexOf("}"));
  32.         System.out.println("Copying \"" + text + "\" to clipboard.");
  33.  
  34.         int hglobal = GlobalAlloc(GMEM_FIXED|GMEM_DDESHARE,
  35.                                   text.length() + 1);
  36.         if (0 == hglobal) {
  37.             System.err.println("GlobalAlloc failed.");
  38.         }
  39.         int hptr = GlobalLock(hglobal);
  40.         if (0 == hptr) {
  41.             System.err.println("GlobalLock failed.");
  42.         }
  43.         lstrcpyA(hptr, text);
  44.         GlobalUnlock(hglobal);
  45.  
  46.         if (!OpenClipboard(0)) {
  47.             System.err.println("OpenClipboard failed.");
  48.         }
  49.         try {
  50.             EmptyClipboard();
  51.             if (0 == SetClipboardData(CF_TEXT, hglobal)) {
  52.                 System.err.println("SetClipboardData failed.");
  53.             }
  54.         } finally {
  55.             CloseClipboard();
  56.         }
  57.  
  58.     }
  59.  
  60.  
  61.  
  62.     /** @dll.import("OLE32",ole) */
  63.     static native _Guid CoCreateGuid();
  64.  
  65.     /** @dll.import("OLE32",ole) */
  66.     static native String StringFromIID(_Guid g);
  67.  
  68.     /** @dll.import("KERNEL32") */
  69.     static native int GlobalAlloc(int flags, int cb);
  70.     /** @dll.import("KERNEL32") */
  71.     static native int GlobalLock(int hglob);
  72.     /** @dll.import("KERNEL32") */
  73.     static native int GlobalUnlock(int hglob);
  74.     /** @dll.import("KERNEL32", ansi) */
  75.     static native void lstrcpyA(int hptr, String text);
  76.  
  77.     static final int GMEM_FIXED    = 0;
  78.     static final int GMEM_DDESHARE = 0x2000;
  79.  
  80.  
  81.     /** @dll.import("USER32") */
  82.     static native boolean OpenClipboard(int hwnd);
  83.     /** @dll.import("USER32") */
  84.     static native boolean CloseClipboard();
  85.     /** @dll.import("USER32") */
  86.     static native int     SetClipboardData(int fmt, int hData);
  87.     /** @dll.import("USER32") */
  88.     static native boolean EmptyClipboard();
  89.  
  90.     static final int CF_TEXT = 1;
  91. }
  92.  
  93.  
  94.  
  95.  
  96.