前一段时间,我用java开发一个安装程序,适用于tomcat mysql构建的web环境。在开发的过程中遇到与写注册表,注册系统服务等问题用java本身很难解决,于是我想到用jni。c与delphi两者开发jni,我是从delphi转到java上来的,我选择了delphi。
用delphi开发jni,首先从http://delphi-jedi.org 下载jni.pas,把它加入到工程中就可以开发jni了。
例如创建桌面快捷方式:
delphi中的代码:
library mydll
uses
jni,windows,comobj,activex,shlobj,sysutils,registry;
//取得桌面目录
function getdesktoppath():string;
var
reg:tregistry;
desktoppath:string;
begin
reg:=tregistry.create;
try
reg.rootkey:=hkey_current_user;
reg.openkey('software\microsoft\windows\currentversion\explorer\shell folders',false);
if reg.valueexists('desktop') then desktoppath:=reg.readstring('desktop');
result:= desktoppath;
finally
reg.free;
end;
end;
//创建桌面快捷方式
procedure createdesktoplink(programpath, programarg, linkname, descr,iconpath: string);
var
anobj: iunknown;
shelllink: ishelllink;
afile: ipersistfile;
filename: widestring;
linkpath:string;
begin
linkpath:=getdesktoppath()+'\'+linkname;
if uppercase(extractfileext(linkpath))<>'.lnk' then //检查扩展名是否正确
begin
raise exception.create('快捷方式的扩展名必须是.lnk!');
end;
try
oleinitialize(nil);//初始化ole库,在使用ole函数前必须调用初始化
anobj := createcomobject(clsid_shelllink);//根据给定的classid生成一个com对象,此处是快捷方式
shelllink := anobj as ishelllink;//强制转换为快捷方式接口
afile := anobj as ipersistfile;//强制转换为文件接口
//设置快捷方式属性,此处只设置了几个常用的属性
shelllink.setpath(pchar(programpath)); // 快捷方式的目标文件,一般为可执行文件
shelllink.setarguments(pchar(programarg));// 目标文件参数
shelllink.setworkingdirectory(pchar(extractfilepath(programpath)));//目标文件的工作目录
shelllink.setdescription(pchar(descr));// 对目标文件的描述
shelllink.seticonlocation(pchar(iconpath),0);
filename := linkpath;//把文件名转换为widestring类型
afile.save(pwchar(filename), false);//保存快捷方式
finally
oleuninitialize;//关闭ole库,此函数必须与oleinitialize成对调用
end;
end;
//创建桌面快捷方式,在jni中调用的就是这个方法
//这个过程的命名很有讲究,它以 java 开头,用下划线将 java 类的包名、类名和方法名连起来。这个命名方法不能有误,否则, java 类将无法将 nativ 方法与它对应起来。同时,在 win32 平台上,此过程的调用方式只能声明为 stdcall 。
procedure java_com_wpd_javawindows_createdesktoplink(penv: pjnienv; obj: jobject;programpath,programarg,linkname,descr,iconpath:jstring);stdcall;
var
jvm:tjnienv;
ppath:string;
parg:string;
lname:string;
description:string;
ipath:string;
begin
jvm := tjnienv.create(penv);
ppath:=jvm.unicodejstringtostring(programpath);
parg:=jvm.unicodejstringtostring(programarg);
lname:=jvm.unicodejstringtostring(linkname);
description:=jvm.unicodejstringtostring(descr);
ipath:=jvm.unicodejstringtostring(iconpath);
createdesktoplink(ppath,parg,lname,description,ipath);
jvm.free;
end;
//向java发送一个信息
function java_com_wpd_javawindows_sendmessage(penv: pjnienv; obj: jobject):jobject;stdcall;
var
jvm:tjnienv;
msg:jobject;
m:string;
begin
jvm := tjnienv.create(penv);
//*********如果发送的信息中包含中文字符,则要先要经过utf8encode转码,否则在java中取得时会是乱码
m:=utf8encode('中国人');
msg:=jvm.stringtojstring(pchar(m));
result:= msg;
end;
{$r *.res}
exports
java_com_wpd_javawindows_createdesktoplink,
java_com_wpd_javawindows_sendmessage;
end.
把上面的编译生成mydll.dll文件,放到java能够找到的地方。
java中的代码:
package com.wpd;
public class javawindows {
public native void createdesktoplink(string programpath,string programarg,string linkpath,string description,string iconpath);
public native string sendmessage();
static{
system.loadlibrary("mydll");
}
public static void main(string s[]){
new javawindows().createdesktoplink("c:\text.exe","","测试.lnk","","c:\test.ico");
system.out.println(new javawindows().sendmessage());
}
}
闽公网安备 35060202000074号