zoukankan      html  css  js  c++  java
  • JNI 在命令行窗口输入字符,不显所输入字符,显指定的掩饰符

    //JNI-命令行窗口输入字符,显掩饰符.txt

    /*
      目标:在命令行窗口输入字符,不显所输入字符,显指定的掩饰符
      作者:tangshancheng@21cn.com
    */

    1、KeyBoard.java源代码
    //: KeyBoard.java
    import java.io.*;

    public class KeyBoard {
    static { 
        System.loadLibrary("inputdll"); 
      } 
      public native static char get(); 

      public static void main(String[] args) {
       StringBuffer stfDir = new StringBuffer();
        KeyBoard test = new KeyBoard(); 
        char c;
        c=test.get();
        while(c!=' ' && c!=' '){
         System.out.print('*');//'a'-'A' = 32
         stfDir.append(c);
         c=test.get();
        }
        System.out.println(" Here is what you input: "+ new String(stfDir));
      }
    } ///:~

    2、编译、生成头文件(KeyBoard.h)
    F:java>javac KeyBoard.java
    F:java>javah KeyBoard

    3、inputdll.cpp文件具体实现这两个函数: 
    a、在vc中新建...-工程-Win32 Dynamic-Link Library,输入相关信息(工程名为inputdll)
    b、新建...-文件-C++Source Files,输入相关信息(文件名为inputdll.c)
    c、输入c内容: 
    #include "KeyBoard.h" 
    #include "conio.h"
    JNIEXPORT jchar JNICALL Java_KeyBoard_get (JNIEnv *env, jobject obj){
      char c; 
      c=getch();     
      return c;
    }

    d、cl -I"%JAVA_HOME%include" -I"%JAVA_HOME%includewin32" -LD HelloWorldImp.c -Feinputdll.dll


    4、编译连接成库文件,本例是在WINDOWS下做的,生成的是inputdll.dll文件。
    并且名称要与JAVA中需要调用的一致,这里就是inputdll.dll 

    5、把inputdll.dll拷贝到KeyBoard.class所在的目录下,java KeyBoard运行它,就可以观察到结果了。

    6、运行结果实例:
    F:java>java  KeyBoard
    *******
    Here is what you input:
    1234567

  • 相关阅读:
    数组的typedef 和函数的typedef
    函数返回值当左值的问题
    C++中虚析构函数的作用
    word2013密钥
    子类父类步长问题
    函数重定义——重写———重载
    C++的成员初始化列表和构造函数体(以前未知)
    常引用
    项目开发中的字符串模型
    指针函数的++(极易犯错误)
  • 原文地址:https://www.cnblogs.com/flying607/p/3442104.html
Copyright © 2011-2022 走看看