Il y a quelque temps, j'ai utilisé Java pour développer un installateur pour l'environnement Web construit par Tomcat MySQL. Au cours du processus de développement, j'ai rencontré des problèmes tels que la rédaction du registre et de l'enregistrement des services système, etc., qui étaient difficiles à résoudre en utilisant Java lui-même, j'ai donc pensé à utiliser JNI. C et Delphi développent JNI.
Pour développer JNI avec Delphi, téléchargez d'abord Jni.pas à partir de http://delphi-Jedi.org, ajoutez-le au projet et vous pouvez développer JNI.
Par exemple, créez un raccourci de bureau:
Code à Delphi:
bibliothèque mydll
usages
JNI, Windows, Comobj, ActiveX, Shlobj, Sysutils, Registry;
// Obtenez le répertoire de bureau
fonction getDeskTopPath (): String;
var
Reg: Tregistry;
Desktoppathe: String;
Commencer
Reg: = Tregistry.Create;
essayer
Reg.rootkey: = hkey_current_user;
Reg.openKey ('Software / Microsoft / Windows / CurrentVersion / Explorer / Shell Folders', false);
Si reg.valueExists ('Desktop') alors desktoppath: = reg.readstring ('Desktop');
Résultat: = Desktoppathe;
Enfin
Reg.free;
fin;
fin;
// Créer des raccourcis de bureau
Procédure CreatedesKTopLink (Programpath, Programarg, LinkName, DESCR, iconPath: String);
var
Anobj: iunknown;
Shellink: IshellLink;
AFILE: IPERSISTFILE;
Nom de fichier: Waidestring;
LinkPath: String;
Commencer
LinkPath: = getdesktoppath () + '/' + linkName;
Si majuscules (ExtractFileExt (linkpath)) <> '. lnk' alors // Vérifiez si l'extension est correcte
Commencer
Sauver exception.Create («L'extension du raccourci doit être .lnk!»);
fin;
essayer
Oléinitialialize (nil); // initialiser la bibliothèque OLE, l'initialisation doit être appelée avant d'utiliser la fonction OLE.
Anobj: = createComObject (clsid_shellLink); // Créer un objet com basé sur le classique donné, voici le raccourci
ShellLink: = ANOBJ comme IshellLink; // Capt à l'interface de raccourci
Afile: = anobj as ipersistfile; // conversion captive en interface de fichier
// Définit les propriétés de raccourci, seules quelques propriétés couramment utilisées sont définies ici
ShellLink.setPath (pChar (programme
ShellLink.setarguments (pChar (programarg)); // paramètres de fichier cible
ShellLink.setworkingDirectory (pChar (extractfilepath (programme
ShellLink.setDescription (PCHA (DESCR)); // Description du fichier cible
ShellLink.seticOnLocation (PCHA (iconPath), 0);
Nom de fichier: = linkPath; // convertir le nom du fichier en type plus large
Afile.save (pwchar (nom de fichier), false); // Enregistrer le raccourci
Enfin
Oléuninitialialize; // fermer la bibliothèque ole, cette fonction doit être appelée par paires avec oléinitialise
fin;
fin;
// Créer un raccourci de bureau, cette méthode est appelée dans JNI
// La dénomination de ce processus est très particulière. Cette méthode de dénomination ne peut pas être incorrecte, sinon, la classe Java ne pourra pas correspondre à la méthode Nativ avec. Dans le même temps, sur les plates-formes Win32, la méthode d'appel de ce processus ne peut être déclarée que STDCALL.
Procédure java_com_wpd_javawindows_createedesktoplink (Penv: pjnienv; obj: jobject; programmpath, programarg, linkName, descr, iconPath: jString); stdcall;
var
JVM: Tjnienv;
PPATH: String;
Parg: chaîne;
Lname: String;
Description: String;
Ipath: String;
Commencer
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;
fin;
// Envoyez un message à Java
Fonction java_com_wpd_javawindows_sendMessage (Penv: pjNienv; obj: jobject): jobject; stdcall;
var
JVM: Tjnienv;
MSG: Jobject;
m: String;
Commencer
JVM: = TJNIENV.CREATE (PENV);
// *********.
m: = utf8encode ('chinois');
msg: = jvm.stringtojstring (pChar (m));
Résultat: = msg;
fin;
{$ R * .res}
exportations
Java_com_wpd_javawindows_createdesktoplink,
Java_com_wpd_javawindows_sendMessage;
fin.
Mettez la compilation ci-dessus pour générer un fichier mydll.dll et mettez-le là où Java peut le trouver.
Code en Java:
package com.wpd;
classe publique Javawindows {
public native void CreatedesKTopLink (String Programpath, String Programarg, String linkPath, String Description, String iconPath);
String natif public SendMessage ();
statique{
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 ());
}
}