📄 ole32.java
字号:
/*
* Ole32.java -
*
* This file is part of the Jawin Project: http://jawinproject.sourceforge.net/
*
* Please consult the LICENSE file in the project root directory,
* or at the project site before using this software.
*/
/* $Id: Ole32.java,v 1.4 2004/07/18 15:07:02 arosii_moa Exp $ */
package org.jawin.win32;
import org.jawin.*;
import org.jawin.io.*;
import java.io.*;
/**
*
*
* @version $Revision: 1.4 $
* @author Stuart Halloway, http://www.relevancellc.com/halloway/weblog/
*/
public class Ole32 {
private static final FuncPtr funcCoInitialize;
private static final FuncPtr funcCoUninitialize;
private static final FuncPtr funcCLSIDFromProgID;
private static final String instrCLSIDFromProgID = "GM64:H:ln64";
private static final int stackSizeCLSIDFromProgID = 8;
private static final FuncPtr funcCoGetObject;
private static final String instrCoGetObject = "GkP16A:H:L12N|4UU|";
private static final int stackSizeCoGetObject = 16;
private static final FuncPtr funcCoCreateInstance;
private static final String instrCoCreateInstance = "P16kIP16A:H:L16N|4UU|";
private static final int stackSizeCoCreateInstance = 20;
static {
try {
String ole32Dll = "OLE32.DLL";
funcCoInitialize = new FuncPtr(ole32Dll, "CoInitializeEx");
funcCoUninitialize = new FuncPtr(ole32Dll, "CoUninitialize");
funcCLSIDFromProgID = new FuncPtr(ole32Dll, "CLSIDFromProgID");
funcCoGetObject = new FuncPtr(ole32Dll, "CoGetObject");
funcCoCreateInstance = new FuncPtr(ole32Dll, "CoCreateInstance");
} catch (COMException ce) {
throw new COMError("Unable to load Ole32 entry points (" + ce.getMessage() + ")");
}
}
/**
* private constructor to avoid instantiation, as this class only contains
* static methods.
*/
private Ole32() {
// never called
}
/**
* Initializes the COM library for use by the calling thread and
* set the thread's concurrency model to a {@link COINIT#APARTMENTTHREADED} thread.
* <br><br>
* Multithreaded applications should call {@link Ole32#CoInitialize(COINIT)} instead.
*
* @throws COMException if unable to initialize the COM library successfully.
*/
public static void CoInitialize() throws COMException {
CoInitialize(COINIT.APARTMENTTHREADED);
}
/**
* Initializes the COM library for use by the calling thread, sets
* the thread's concurrency model to coinit, and creates a new apartment
* for the thread if one is required.
* <br><br>
* This method should be used instead of {@link Ole32#CoInitialize()} in
* multithreaded applications, with a {@link COINIT#MULTITHREADED} parameter.
*
* @param coinit the threads concurrency model.
* @throws COMException if unable to initialize the COM library successfully.
* @throws NullPointerException if coinit is null.
*/
public static void CoInitialize(COINIT coinit) throws COMException {
funcCoInitialize.invoke_I(0, coinit.getValue(), ReturnFlags.CHECK_HRESULT);
}
public static void CoUninitialize() throws COMException {
funcCoUninitialize.invoke_I(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);
}
/**
* @throws NullPointerException if prodID is null
*/
private static GUID CLSIDFromProgID(String progID) throws COMException {
try {
/*
* bytes required:
* - each char in the string -> 2 bytes.
* - the string length as int -> 4 bytes.
* - a NULL/NONNULL byte mark -> 1 byte.
*/
NakedByteStream nbs = new NakedByteStream(progID.length() * 2 + 5);
LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
leos.writeStringUnicode(progID);
byte[] result = funcCLSIDFromProgID.invoke(instrCLSIDFromProgID,
stackSizeCLSIDFromProgID, nbs, null,
ReturnFlags.CHECK_HRESULT);
LittleEndianInputStream leis = new LittleEndianInputStream(new ByteArrayInputStream(result));
return GUID.marshalOut(leis);
} catch (IOException ioe) {
throw new COMException(ioe);
}
}
/**
* @throws NullPointerException if name is null
*/
public static COMPtr CoGetObject(String name, GUID iid) throws COMException {
try {
/*
* bytes required:
* - each char in the string -> 2 bytes.
* - the string length as int -> 4 bytes.
* - a NULL/NONNULL byte mark -> 1 byte.
* - iid -> 16 bytes.
*/
NakedByteStream nbs = new NakedByteStream(name.length() * 2 + 21);
LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
leos.writeStringUnicode(name);
iid.marshal(leos, null);
byte[] result = funcCoGetObject.invoke(instrCoGetObject,
stackSizeCoGetObject, nbs, null,
ReturnFlags.CHECK_HRESULT);
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 {
/*
* bytes required:
* - 2 * GUID = 2 * 16 bytes -> 32 bytes.
* - the clsContext as int -> 4 bytes
*/
NakedByteStream nbs = new NakedByteStream(36);
LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
//first level of callstack
clsid.marshal(leos, null);
leos.writeInt(clsContext);
iid.marshal(leos, null);
byte[] result = funcCoCreateInstance.invoke(instrCoCreateInstance,
stackSizeCoCreateInstance, nbs, null,
ReturnFlags.CHECK_HRESULT);
return IdentityManager.getCOMPtr(result, 0, iid);
} catch (IOException ioe) {
throw new COMException(ioe);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -