前一段時間
用delphi開發JNI
例如創建桌面快捷方式:
Delphi中的代碼:
library myDll
uses
JNI
//取得桌面目錄
function getDesktopPath():String;
var
Reg:TRegistry;
DesktopPath:String;
begin
Reg:=TRegistry
try
Reg
Reg
if Reg
Result:= DesktopPath;
finally
Reg
end;
end;
//創建桌面快捷方式
procedure CreateDesktopLink(ProgramPath
var
AnObj: IUnknown;
ShellLink: IShellLink;
AFile: IPersistFile;
FileName: WideString;
LinkPath:String;
begin
LinkPath:=getDesktopPath()+
if UpperCase(ExtractFileExt(LinkPath))<>
begin
raise Exception
end;
try
OleInitialize(nil);//初始化OLE庫
AnObj := CreateComObject(CLSID_ShellLink);//根據給定的ClassID生成一個COM對象
ShellLink := AnObj as IShellLink;//強制轉換為快捷方式接口
AFile := AnObj as IPersistFile;//強制轉換為文件接口
//設置快捷方式屬性
ShellLink
ShellLink
ShellLink
ShellLink
ShellLink
FileName := LinkPath;//把文件名轉換為WideString類型
AFile
finally
OleUninitialize;//關閉OLE庫
end;
end;
//創建桌面快捷方式
//這個過程的命名很有講究
procedure Java_com_wpd_JavaWindows_createDesktopLink(PEnv: PJNIEnv; Obj: JObject;ProgramPath
var
JVM:TJNIEnv;
PPath:String;
PArg:String;
LName:String;
Description:String;
IPath:String;
begin
JVM := TJNIEnv
PPath:=JVM
PArg:=JVM
LName:=JVM
Description:=JVM
IPath:=JVM
CreateDesktopLink(PPath
JVM
end;
//向java發送一個信息
function Java_com_wpd_JavaWindows_sendMessage(PEnv: PJNIEnv; Obj: JObject):JObject;stdcall;
var
JVM:TJNIEnv;
msg:JObject;
m:String;
begin
JVM := TJNIEnv
//*********如果發送的信息中包含中文字符
m:=UTF
msg:=JVM
result:= msg;
end;
{$R *
exports
Java_com_wpd_JavaWindows_createDesktopLink
Java_com_wpd_JavaWindows_sendMessage;
end
把上面的編譯生成myDll
java中的代碼:
package com
public class JavaWindows {
public native void createDesktopLink(String programPath
public native String sendMessage();
static{
System
}
public static void main(String s[]){
new JavaWindows()
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27225.html