Ajuda com DLL

5 respostas
S

Pessoal,

Criei uma DLL em Delphi e preciso usar uma procedure desta DLL em uma aplicação JAVA:

Estou tentando usar JNI… Abaixo segue o meu codigo implementado em JAVA:

package view;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class principal extends javax.swing.JFrame {

    /** Creates new form principal */
    public principal() {
        initComponents();
        System.load("EscritorRegistro.dll");
    }

    public native void WriterRegistry(String porta);

    private void jbtnAbrirActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String porta = "COM7";
        this.WriterRegistry(porta);
    }                                         

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnAbrir;
    // End of variables declaration                   
}

Pois bem, minha duvida é bastante básica. Quando executo a aplicação estoura o erro:
Exception in thread “AWT-EventQueue-0” java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: EscritorRegistro.dll

Mas adicionei a variavel EscritorRegistro.dll no path… o que pode estar ocorrendo… Se alguém puder me ajudar eu agradeço!

Abraço!

5 Respostas

Jackson_William

Boa tarde SergioJunior,

Tente ao declarar a dll, utilizar o path completo dela
exemplo: System.load(“C://dll//EscritorRegistro.dll”);

S

Perfeito!

Agora ele esta encontrando a DLL… Beleza!!!

Quando clico no botão que executa a procedure dentro da minha DLL estoura um erro:

Exception in thread “AWT-EventQueue-0” java.lang.UnsatisfiedLinkError: view.principal.WriterRegistry(Ljava/lang/String;)V
at view.principal.WriterRegistry(Native Method)

Abaixo segue meu codigo da DLL:

library EscritorRegistro;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils, Registry, Windows, Messages, Variants, Graphics, Controls,
  Dialogs, StdCtrls, Classes;

{$R *.res}

var Reg : TRegistry;

procedure WriterRegistry(porta : string); export;
begin

Reg := TRegistry.Create;
Reg.RootKey := HKEY_CURRENT_USER;
  if Reg.OpenKey('CONF_CONT_ERG', true) then
          begin
            Reg.WriteString('Porta', porta);
            Reg.LazyWrite := False; // força a gravação direta no registro
            Reg.CloseKey;
          end;

end;

exports
WriterRegistry;

begin
end.

Deve ser algum problema com meu método native… mas nao tenho a minima idéia do que… A minha procedure da DLL espera uma string como paramento de entrada e passo uma String em JAVA… será q pode ser incompatibilidade de tipos…

Abraço!

Alexandre_Saudate

Jackson William:
Boa tarde SergioJunior,

Tente ao declarar a dll, utilizar o path completo dela
exemplo: System.load(“C://dll//EscritorRegistro.dll”);

Uma dica…

se a dll já está no seu path, use System.loadLibrary só com o nome dela (no caso, EscritorRegistro).

Abraços

S

Então… Se eu colocar apenas o nome da DLL, da um erro de path…

Apenas colocando todo o endereço ele encontra a DLL, mas desta forma continua dando estourando o erro:

Exception in thread “AWT-EventQueue-0” java.lang.UnsatisfiedLinkError: view.principal.WriterRegistry(Ljava/lang/StringV
at view.principal.WriterRegistry(Native Method)

Obrigado pessoal!

Jackson_William

Boa tarde SergioJunior, veja este link talves, possa lhe ajudar, sei que se fosse em C, a estrutura da chama mudaria agora em Delphi não faço idéia:
http://koti.mbnet.fi/akini/java/jni/

Abraços

Criado 27 de dezembro de 2009
Ultima resposta 28 de dez. de 2009
Respostas 5
Participantes 3