Some time ago, I used java to develop an installer for web environment built by tomcat MySQL. During the development process, I encountered problems such as writing registry and registering system services, etc., which were difficult to solve using java itself, so I thought of using JNI. C and delphi develop JNI. I moved from delphi to java, and I chose delphi.
To develop JNI with delphi, first download JNI.pas from http://delphi-jedi.org, add it to the project and you can develop JNI.
For example, create a desktop shortcut:
Code in Delphi:
library myDll
uses
JNI, windows, ComObj, ActiveX, ShlObj, SysUtils, Registry;
//Get the desktop directory
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;
//Create desktop shortcuts
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 //Check whether the extension is correct
Begin
raise Exception.Create('The extension of the shortcut must be .lnk!');
end;
try
OleInitialize(nil);//Initialize the OLE library, initialization must be called before using the OLE function.
AnObj := CreateComObject(CLSID_ShellLink);//Create a COM object based on the given ClassID, here is the shortcut
ShellLink := AnObj as IShellLink;//Capt to shortcut interface
AFile := AnObj as IPersistFile;//Captive conversion to file interface
//Set shortcut properties, only a few commonly used properties are set here
ShellLink.SetPath(PChar(ProgramPath)); // The target file of the shortcut, generally executable file
ShellLink.SetArguments(PChar(ProgramArg));// Target file parameters
ShellLink.SetWorkingDirectory(PChar(ExtractFilePath(ProgramPath)));//Working directory of the target file
ShellLink.SetDescription(PChar(Descr));// Description of the target file
ShellLink.SetIconLocation(PChar(IconPath),0);
FileName := LinkPath;//Convert file name to WideString type
AFile.Save(PWChar(FileName), False);//Save shortcut
Finally
OleUninitialize;// Close the OLE library, this function must be called in pairs with OleInitialize
end;
end;
//Create a desktop shortcut, this method is called in JNI
//The naming of this process is very particular. It starts with Java and uses underscore to connect the package name, class name and method name of the Java class. This naming method cannot be incorrect, otherwise, the Java class will not be able to correspond to the nativ method with it. At the same time, on Win32 platforms, the calling method of this process can only be declared as 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;
//Send a message to 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);
//************ If the message sent contains Chinese characters, it must first be transcoded through UTF8Encode, otherwise it will be garbled when obtained in Java
m:=UTF8Encode('Chinese');
msg:=JVM.StringToJString(PChar(m));
result:= msg;
end;
{$R *.res}
exports
Java_com_wpd_JavaWindows_createDesktopLink,
Java_com_wpd_JavaWindows_sendMessage;
end.
Put the above compilation to generate myDll.dll file and put it where Java can find it.
Code in 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","","test.LNK","","c:/test.ico");
System.out.println(new JavaWindows().sendMessage());
}
}