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

📄 comsharedstubdriver.java

📁 java 调用windows的api
💻 JAVA
字号:
package org.jawin.tools;

import org.jawin.*;
import org.jawin.constants.*;
import java.io.*;
import java.util.*;


/**
 * FIXME - used for generating java and native source for the org.jawin.marshal.COMMarshal class
 * is this still used? (at least it generates native implementations in the com.develop branch?)
 *
 * @version     $Revision: 1.4 $
 * @author      Stuart Halloway, http://www.relevancellc.com/halloway/weblog/
 */
public class COMSharedStubDriver {
  public static final int depth = 2;

  public static void main(String [] args) {
    try {
      PrintStream psNative = System.out;
      PrintStream psJava = System.out;
      if (args.length < 2) {
	System.out.println("dumping to stdout...");
      } else {
        psNative = new PrintStream(new FileOutputStream(args[0]));
	System.out.println("building native stubs to " + args[0]);
	psJava = new PrintStream(new FileOutputStream(args[1]));
	System.out.println("building java stubs to " + args[1]);
      }    

      Vector comtypes = new Vector();
      COMType type = null;

      type = new COMType();
      type.vt = VarTypes.VT_I4;
      type.code = "I";
      type.name = "int";
      type.nativeName = "jint";
      type.preInvoke = "        int inv% = arg%;\n"; 
      comtypes.add(type);

      type = new COMType();
      type.vt = VarTypes.VT_R4;
      type.code = "F";
      type.name = "float";
      type.nativeName = "jfloat";
      type.preInvoke = "        float inv% = arg%;\n"; 
      comtypes.add(type);

      type = new COMType();
      type.vt = VarTypes.VT_BSTR;
      type.code = "G";
      type.name = "String";
      type.nativeName = "jstring";
      type.preInvoke = "        CComBSTR temp%;\n        temp%.Attach(pEnv->jstobs(arg%));\n        int inv% = (int)temp%.m_str;\n"; 
      comtypes.add(type);

      //difficult to add types like IUnknown with current design
      //because they would require marshalling on the Java side too

      COMType[] intypes = new COMType[comtypes.size()];
      intypes = (COMType[]) comtypes.toArray(intypes);

      type = new COMType();
      type.vt = VarTypes.VT_BSTR;
      type.code = "oG";
      type.name = "String";
      type.nativeName = "jstring";
      type.preInvoke = "        CComBSTR result%;\n        int inv% = (int)&result%.m_str;\n"; 
      type.postInvoke = "        return pEnv->bstrtojsl(result%);\n";
      type.isOut = true;
      comtypes.add(type);

      type = new COMType();
      type.vt = VarTypes.VT_I4;
      type.code = "oI";
      type.name = "int";
      type.nativeName = "jint";
      type.isOut = true;
      type.preInvoke = "        int result%;\n        int inv% = (int) &result%;\n"; 
      type.postInvoke = "        return result%;\n";
      comtypes.add(type);

      type = new COMType();
      type.vt = VarTypes.VT_R4;
      type.code = "oF";
      type.name = "float";
      type.nativeName = "jfloat";
      type.isOut = true;
      type.preInvoke = "        float result%;\n        int inv% = (int) &result%;\n"; 
      type.postInvoke = "        return result%;\n";
      comtypes.add(type);

      COMType[] alltypes = new COMType[comtypes.size()];
      alltypes = (COMType[]) comtypes.toArray(alltypes);


      COMType[][] typeArrays = new COMType[depth][];
      for (int n=0; n<(depth-1); n++) {
	typeArrays[n] = intypes;
      }
      typeArrays[depth-1] = alltypes;

      COMSharedStubBuilder build1 = new COMSharedStubBuilder();
      build1.decl = "JNIEXPORT ";
      build1.baseName = " JNICALL Java_com_develop_jawin_marshal_COMMarshal_invoke";
      build1.baseSig = "(JNIComUtil * pEnv, jclass, jint vtableIndex, jint guidToken, jint peer, jint unknown";
      build1.methodIntro = ")\n"+
	"{\n"+
	"    CComPtr<IUnknown> cpUnk;\n"+
	"    try {\n"+
	"        getUnknown(guidToken, peer, unknown, &cpUnk);\n"+
	"        FTYPE%* vtable = (FTYPE%*) (*(int*)(cpUnk.p));\n";
      build1.methodInvoke="        JNI_HR(vtable[vtableIndex]((int)cpUnk.p";
      build1.invokeClose = "));\n";
      build1.methodExit = "    }\n"+
	"    HANDLE_JNI_EXCEPTIONS()\n";
      build1.methodClose = "}\n";

      build1.buildStubs(typeArrays, depth, psNative, true);

      COMSharedStubBuilder build2 = new COMSharedStubBuilder();
      build2.decl = "    public static native ";
      build2.baseName = " invoke";
      build2.baseSig = "(int vtableIndex, int guidToken, int peer, int unknown";
      build2.methodClose = ");\n";

      build2.buildStubs(typeArrays, depth, psJava, false) ;

    }
  catch (Exception e) {
    e.printStackTrace();
  }
  }
}


  

⌨️ 快捷键说明

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