Navigation



Initializing

SWT calls the OleInitialize and OleUninitialize methods when the UI thread initializes the Display. This can be a problem if your COM code does not actually display anything or if you want to perform COM operations from a non-ui thread. When this occurs your code is responsible for calling the SWT COM initialize methods as shown here;

NOTE: See the discussion on threads for more details on how to perform COM operations from a non-ui thread.

OS.OleInitialize(0);
// Do Your COM stuff here
OS.OleUninitialize();

For example if you used the Shell automation library to obtain the list of recent documents and you did not want to actually display the list, the following code could be used;

public static void main(String[] args) {
  try {
    OS.OleInitialize(0);
    shell.Shell win32Shell = shell.Shell.create(
        COM.CLSCTX_INPROC_SERVER|COM.CLSCTX_LOCAL_SERVER);
    Folder recentFileItems = win32Shell.nameSpace(new Variant(
        ShellSpecialFolderConstants.ssfRECENT.getValue()));
    FolderItems folderItems = recentFileItems.items();
    int folderCount = folderItems.getCount();
    for (int i=0; i<folderCount; i++ ) {
      FolderItem folderItem = folderItems.item(new Variant(i));
      System.out.println("Name:" + folderItem.getName());
      FolderItemVerbs verbs = folderItem.verbs();
      int verbCount = verbs.getCount();
      for (int x=0; x<verbCount; x++ ) {
        FolderItemVerb verb = verbs.item(new Variant(x));
        System.out.println("  Verb:" + verb.getName());
      }
    }
    OS.OleUninitialize();
  } catch (Exception e) {
    e.printStackTrace();
  }
}