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

📄 genericstub.java

📁 java 调用windows的api
💻 JAVA
字号:
/*
 * GenericStub.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: GenericStub.java,v 1.5 2004/07/18 15:00:37 arosii_moa Exp $ */

package org.jawin.marshal;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.jawin.Bootstrap;
import org.jawin.COMException;
import org.jawin.GUID;
import org.jawin.ReturnFlags;
import org.jawin.constants.DispatchConstants;
import org.jawin.util.HexFormatter;
import org.jawin.win32.ITypeInfo;

/**
 * GenericStub methods for calling generic native DLL entry point (ie. entry points not matching 
 * signatures in {@link org.jawin.marshal.SharedStubs SharedStubs}), and COM vtable and IDispatch
 * based objects. These methods should usually NOT be called directly, instead use the
 * methods in {@link org.jawin.FuncPtr FuncPtr}, {@link org.jawin.COMPtr COMPtr} or
 * {@link org.jawin.DispatchPtr DispatchPtr}.
 * <br><br>
 * The native implementations of the methods in this class are in GenericStub.cpp.
 * 
 * @see org.jawin.FuncPtr FuncPtr
 * @see org.jawin.COMPtr COMPtr
 * @see org.jawin.DispatchPtr DispatchPtr
 *
 * @version     $Revision: 1.5 $
 * @author      Stuart Halloway, http://www.relevancellc.com/halloway/weblog/
 */
public class GenericStub {

	/**
	 * If the property <code>org.jawin.traceWin32</code> is set all calls
	 * to {@link #win32Invoke(int, String, int, int, byte[], Object[], ReturnFlags) win32Invoke()}
	 * will be traced on <code>System.out</code>.
	 */
	public static final boolean TRACE_WIN32 =
		(null != System.getProperty("org.jawin.traceWin32"));

	/**
	 * If the property <code>org.jawin.traceCom</code> is set all calls
	 * to {@link #comInvoke(String, int, int, byte[], Object[], int, int, int, int) to comInvoke()}
	 * will be traced on <code>System.out</code>.
	 */
	public static final boolean TRACE_COM =
		(null != System.getProperty("org.jawin.traceCom"));

	/**
	 * If the property <code>org.jawin.traceDispatch</code> is set all calls
	 * to {@link #dispatchInvoke(String, int, int, byte[], String, DispatchConstants, int, int) dispatchInvoke()}
	 * will be traced on <code>System.out</code>.
	 */
	public static final boolean TRACE_DISPATCH =
		(null != System.getProperty("org.jawin.traceDispatch"));

	private static final String SEPARATOR =
		"-----------------------------------------------------------";

	static {
		Bootstrap.init();
	}

	/**
	 * private constructor to avoid instantiation, as this class only contains
	 * static methods.
	 */
	private GenericStub() {
		// never called
	}

	/**
	 * FIXME - what is this used for?
	 */
	public static int findN(String str, String find, int n) {
		int found = 0;
		for (int loop = -1; loop < n; loop++) {
			found = str.indexOf(find, found + 1);
			if (found == -1) return -1;
		}
		return found;
	}
  
	/**
	 * Return the stacktrace information about the immediate calling method
	 * <b>outside</b> org.jawin.*.
	 */
	private static String immediateCaller() {
		Exception e = new Exception();
		StringWriter w = new StringWriter();
		PrintWriter pw = new PrintWriter(w);
		e.printStackTrace(pw);
		String s = w.toString();
		int start = s.lastIndexOf("org.jawin");
		start = s.indexOf("at ", start);
		if (start == -1) return "";
		int end = s.indexOf(")", start);
		if (end == -1) return "";
		return s.substring(start, end + 1);
	}

	/**
	 * for registering a custom marshal string used for struct and callback
	 * marshalling. FIXME - return and elaborate on this?
	 * 
	 * @param custom the marshal string to register
	 * @return the handle for the string - can be used in later marshall strings.
	 */
	public static int registerCustomString(String custom) {
		if (custom == null) {
			throw new NullPointerException("custom string can not be null");
		}
		return registerCustomString0(custom);
	}

	/**
	 * @see #registerCustomString(String)
	 */
	private static native int registerCustomString0(String custom);


	/**
	 * for calling a native method exposed in a DLL. Should usually NOT be called directly, instead use the 
	 * {@link org.jawin.FuncPtr#invoke(String, int, NakedByteStream, Object[], ReturnFlags) FuncPtr.invoke()}-method.
	 * 
	 * @see org.jawin.FuncPtr#invoke(String, int, NakedByteStream, Object[], ReturnFlags) FuncPtr.invoke()
	 */
	public static byte[] win32Invoke(int peer, String instructions, int stackSize, int argStreamSize,
			byte[] argStream, Object[] objectArgs, ReturnFlags flags) throws COMException {

		if (TRACE_WIN32) {
			System.out.println(SEPARATOR);
			System.out.println(immediateCaller());
			System.out.println("GenericStub.win32Invoke");
			System.out.println("\tinstructions: " + instructions);
			System.out.println("\tstackSize: " + stackSize);
			System.out.println("\tflags: " + flags);
			System.out.println("\targStream: " + ((argStream != null) ? Integer.toString(argStream.length) : "null") + ", argStreamSize: " + argStreamSize);
			System.out.println(HexFormatter.convertBytesToString(argStream, 8, true));
		}

		byte[] result = win32Invoke0(peer, instructions, stackSize, argStreamSize,
				argStream, objectArgs, flags.value);

		if (TRACE_WIN32) {
			System.out.println("GenericStub.comInvoke result");
			System.out.println(HexFormatter.convertBytesToString(result, 8, true));
		}
		return result;
	}

	/**
	 * @see #win32Invoke(int, String, int, int, byte[], Object[], ReturnFlags)
	 */
	private static native byte[] win32Invoke0(int peer, String instructions, int stackSize,
			int argStreamSize, byte[] argStream, Object[] objectArgs, int flags) throws COMException;


	/**
	 * for making a native IDispatch method-call. Should usually NOT be called directly, instead use one
	 * of the invoke, get or put methods in {@link org.jawin.DispatchPtr DispatchPtr}.
	 * <br><br>
	 * <b>Important:</b> the dispatch invoke marshal arguments rigth to left. This is the
	 * case for both the instructions string (both the in and out part) and for the
	 * argStream byte array. Please see the implementation in 
	 * {@link org.jawin.DispatchPtr#invokeN(String, Object[], int) DispatchPtr.invokeN()}
	 * for an example of how this should be done.
	 * <br><br>
	 * COM always uses {@link ReturnFlags#CHECK_HRESULT} for error-checking.
	 * 
	 * @see org.jawin.DispatchPtr#invokeN(String, Object[]) DispatchPtr.invoke() 
	 * @see org.jawin.DispatchPtr#get(String) DispatchPtr.get()
	 * @see org.jawin.DispatchPtr#put(String, Object) DispatchPtr.put()
	 */
	public static byte[] dispatchInvoke(String instructions, int stackSize, int argStreamSize,
			byte[] argStream, String meth, DispatchConstants flags, int peer, int unknown) throws COMException {

		if (TRACE_DISPATCH) {
			System.out.println(SEPARATOR);
			System.out.println(immediateCaller());
			System.out.println("GenericStub.dispatchInvoke");
			System.out.println("\tmethod: " + meth);
			System.out.println("\tpeer: " + peer + ", unknown: " + unknown);
			System.out.println("\tinstructions: " + instructions);
			System.out.println("\tstackSize: " + stackSize);
			System.out.println("\tflags: " + flags);
			System.out.println("\targStream: " + ((argStream != null) ? Integer.toString(argStream.length) : "null") + ", argStreamSize: " + argStreamSize);
			System.out.println(HexFormatter.convertBytesToString(argStream, 8, true));
		}

		byte[] result = dispatchInvoke0(instructions, stackSize, argStreamSize, argStream,
				meth, flags.value, peer, unknown);

		if (TRACE_DISPATCH) {
			System.out.println("GenericStub.dispatchInvoke result");
			System.out.println(HexFormatter.convertBytesToString(result, 8, true));
		}
		return result;
	}
	
	/**
	 * @see #dispatchInvoke(String, int, int, byte[], String, int, int, int)
	 */
	private static native byte[] dispatchInvoke0(String instructions, int stackSize,
			int argStreamSize, byte[] argStream, String meth, int flags, int peer, int unknown) throws COMException;


	/**
	 * for making a native vtable based COM-call. Should usually NOT be called directly, instead use the 
	 * {@link org.jawin.COMPtr#comInvoke(int, String, int, NakedByteStream, Object[]) COMPtr.comInvoke()}-method.
	 * <br><br>
	 * COM always uses {@link ReturnFlags#CHECK_HRESULT} for error-checking.
	 * 
	 * @see org.jawin.COMPtr#comInvoke(int, String, int, NakedByteStream, Object[]) COMPtr.comInvoke() 
	 */
	public static byte[] comInvoke(String instructions, int stackSize, int argStreamSize, byte[] argStream,
			Object[] objectArgs, int vtable, int guidToken, int peer, int unknown) throws COMException {

		if (TRACE_COM) {
			System.out.println(SEPARATOR);
			System.out.println(immediateCaller());
			System.out.println("GenericStub.comInvoke");
			System.out.println("\tvtable ix: " + vtable);
			System.out.println("\tpeer: " + peer + ", unknown: " + unknown);
			System.out.println("\tinstructions: " + instructions);
			System.out.println("\tstackSize: " + stackSize);
			System.out.println("\targStream: " + ((argStream != null) ? Integer.toString(argStream.length) : "null") + ", argStreamSize: " + argStreamSize);
			System.out.println(HexFormatter.convertBytesToString(argStream, 8, true));
		}

		byte[] result = comInvoke0(instructions, stackSize, argStreamSize, argStream, 
				objectArgs, vtable, guidToken, peer, unknown);
		
		if (TRACE_COM) {
			System.out.println("GenericStub.comInvoke result");
			System.out.println(HexFormatter.convertBytesToString(result, 8, true));
		}
		return result;
	}
	
	/**
	 * @see #comInvoke(String, int, int, byte[], Object[], int, int, int, int)
	 */
	private static native byte[] comInvoke0(String instructions, int stackSize, int argStreamSize,
			byte[] argStream, Object[] objectArgs, int vtable, int guidToken, int peer, int unknown) throws COMException;


	public static native int getTypeInfoCount(int peer, int unknown, int[] pctinfo) throws COMException;

	/**
	 * FIXME - native implementation not finished?
	 */
	public static native int getTypeInfo(int guidToken, int peer, int unknown, int iTInfo, GUID  lcid, ITypeInfo[]  ppTInfo) throws COMException;
  
	/**
	 * FIXME - no native implementation?
	 */
	public static native int getIDsOfNames(int peer, int unknown, GUID riid, String[] rgszNames, int cNames, GUID lcid, int[] rgDispId);
}

⌨️ 快捷键说明

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