⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ole32.java

📁 用于Java 组件和通过Windows COM 对象或Windows DLL 来公开的组件之间的互操作
💻 JAVA
字号:
//******************************************************************
// Released under the DevelopMentor OpenSource Software License.
// Please consult the LICENSE file in the project root directory,
// or at http://www.develop.com for details before using this
// software.
//******************************************************************

package com.develop.jawin.win32;

import com.develop.jawin.*;
import com.develop.jawin.marshal.*;
import com.develop.io.*;
import com.develop.util.*;
import java.io.*;
import java.util.*;

public class Ole32 implements MarshalConstants {
  static final FuncPtr mfuncCLSIDFromProgID;
  static final int mstackCoGetObject = 16;
  static final FuncPtr mfuncCoGetObject;
  static final int mstackCoCreateInstance = 20;
  static final FuncPtr mfuncCoCreateInstance;

  static {
    try {
      mfuncCLSIDFromProgID = new FuncPtr("OLE32.DLL", "CLSIDFromProgID");
      mfuncCoGetObject = new FuncPtr("OLE32.DLL", "CoGetObject");
      mfuncCoCreateInstance = new FuncPtr("OLE32.DLL", "CoCreateInstance");
    } catch (COMException ce) {
      throw new COMError("Unable to load Ole32 entry points");
    }
    
  }
  public static void CoInitialize() 
    throws COMException
  {
    FuncPtr fp = new FuncPtr("OLE32.DLL", "CoInitialize");
    fp.invoke(0, ReturnFlags.CHECK_HRESULT);
  }

  public static void CoUninitialize() 
    throws COMException
  {
    FuncPtr fp = new FuncPtr("OLE32.DLL", "CoUninitialize");
    fp.invoke(ReturnFlags.CHECK_HRESULT);
  }

  public static COMPtr GetFromProgID(String ProgID, GUID iid) throws COMException {
    GUID clsid = CLSIDFromProgID(ProgID);
    //REMOTE_SERVER flag causes "no such interface supported" on local servers that 
    //would otherwise load, so it is omitted
    return CoCreateInstance(clsid, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, iid);
  }

  static GUID CLSIDFromProgID(String progID) throws COMException {
    try {
      NakedByteStream nbs = new NakedByteStream();
      LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
      leos.writeStringUnicode(progID);
      byte[] result = GenericStub.win32Invoke(mfuncCLSIDFromProgID.getPeer(), 
					      "GM64:H:ln64", 
					      8, 
					      leos.size(), 
					      nbs.getInternalBuffer(), 
					      null);
      LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
      return GUID.marshalOut(leis);
    } catch (IOException ioe) {
      throw new COMException(ioe);
    }
  }

  public static COMPtr CoGetObject(String name, GUID iid) 
    throws COMException
  {
    try {
      NakedByteStream nbs = new NakedByteStream();
      LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
      leos.writeStringUnicode(name);
      iid.marshal(leos, null);
      byte[] result = GenericStub.win32Invoke(mfuncCoGetObject.getPeer(), 
					      "GkP16A:H:L12N|4UU|", 
					      mstackCoGetObject, 
					      leos.size(), 
					      nbs.getInternalBuffer(), 
					      null);
      return IdentityManager.getCOMPtr(result, 0, iid);
    } catch (IOException ioe) {
      throw new COMException(ioe);
    }
  }

  public static final int CLSCTX_INPROC_SERVER	= 0x1;
  public static final int CLSCTX_INPROC_HANDLER	= 0x2;
  public static final int CLSCTX_LOCAL_SERVER	= 0x4;
  public static final int CLSCTX_REMOTE_SERVER	= 0x10;
  public static final int CLSCTX_ALL              = 0x17;

  public static COMPtr CoCreateInstance(GUID clsid, int clsContext, GUID iid) 
    throws COMException
  {
    try {
      NakedByteStream nbs = new NakedByteStream();
      LittleEndianOutputStream baos = new LittleEndianOutputStream(nbs);
      //first level of callstack
      clsid.marshal(baos, null);
      baos.writeInt(clsContext);
      iid.marshal(baos, null);
      byte[] result = GenericStub.win32Invoke(mfuncCoCreateInstance.getPeer(), 
					      "P16kIP16A:H:L16N|4UU|",
					      mstackCoCreateInstance, 
					      baos.size(), 
					      nbs.getInternalBuffer(), 
					      null);
                                                                                                        
      return IdentityManager.getCOMPtr(result, 0, iid);
    } catch (IOException ioe) {
      throw new COMException(ioe);
    }
  }

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -