java調用windows API

來源:互聯網
上載者:User

使用jinvoke調用windowsAPI。jna使用比較麻煩,需要寫c代碼和參數轉換,jinvoke的使用就像jdk中的包一樣。

官網使用參考:http://www.jinvoke.com/calling-the-win32-api-from-java

 

一個彈出框的例子(這種代碼用於調用任何dll,不只是windows的,也可以是自己寫的dll)

import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;

public class HelloWindows
{
@NativeImport(library="User32")
public static native int MessageBox(int hwnd,
           String text,
           String caption,
           int type); //windows Api MessageBox函數原型

public static void main(String[]argc)
{
   JInvoke.initialize();
   MessageBox(0,"Thisi MessageBox is a native Win32 MessageBox",
      "Hellow World",0);
}
}

運行效果:

 

 

*
使用以上代碼方式可以任意調用本地的DLL檔案。
注意:1.import com.jinvoke.JInvoke;import com.jinvoke.NativeImport;//引用包
   2.@NativeImport(library="User32")//指定本地的DLL檔案
   3.public static native int MessageBox(int hwnd,String text,String caption,int type); //windows Api MessageBox函數原型 聲明一個可以在java當前類中使用的方法
   4.JInvoke.initialize();//執行個體化JInvoke
   5.MessageBox(0,"Thisi MessageBox is a native Win32 MessageBox","Hellow World",0);//然後就可以開始調用DLL中的函數了

 

*
如果只是為了使用User32.dll中的函數可以直接使用jinvoke.jar中提供的User32類,而不必使用@NativeImport(library="User32")去包含一個DLL,再去聲明一個做函數的類型轉換的方法。

 

 

*

編譯是需要帶上jinvoke.jar即:java -classpath .;jinvoke.jar HelloWindows

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.