JAVA调用DLL的问题

orient_fc 2003-10-21 12:39:53
环境:TOMCAT 4.1 + JAVA
我要在JAVA(.CLASS)中调用DLL(存取注册表)
然后在JSP中调用JAVA类来存取注册表
现在在JAVA(.CLASS)中调用(MAIN函数)成功
但是在JSP中调用失败.为什么?
在JSP中调用JAVA类时DLL应该放在什么路径下.
...全文
54 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
betterjonah 2003-10-21
  • 打赏
  • 举报
回复
jre目录是java运行时环境目录。
至于第二种我没有多少把握,我是这么想的:我记得jni加载dll文件时,如果不是绝对路径,就在当前.class所在的目录下找。写文件就是看运行时这个.class在那里,来确定dll放在那里。不过在我影响中,运行时文件是写在tomcat快捷方式所在的目录。好像不能满足你的要求。你可以尝试使用配置文件来指定dll的位置,使用的时候使用绝对路径。

问一下,我的javah命令老失败,我的代码别人用都没有问题,可是我就是不行?需要配置么?还有你可以使用jniRegitry这个包,有源码的,看看他是怎么用的,可能对你有用。我记得他也是将一些文件复制到jre目录下的
etre 2003-10-21
  • 打赏
  • 举报
回复
加放系统目录就可以了
orient_fc 2003-10-21
  • 打赏
  • 举报
回复
TO betterjonah(jonah):
你的第一种方法我实验了---好用
第二种方法是怎么回事?请说明
(我感觉第一种方法不是很好---对于软件发布)
orient_fc 2003-10-21
  • 打赏
  • 举报
回复
DLL代码:
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "TRegister.h"
#include "jni.h"

/*
//////////////////////////////////////////////////////////////////////////////
// 名  前:Java_test_TRegister_AddKeyC
// 形  式:
// 戻 り 値:jint
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
JNIEXPORT void JNICALL Java_test_TRegister_AddKeyC
(JNIEnv *env, jobject obj, jstring strPath, jstring strKeyName, jstring strKeyValue)
{



}

//////////////////////////////////////////////////////////////////////////////
// 名  前:Java_test_TRegister_DelKeyC
// 形  式:
// 戻 り 値:jint
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
JNIEXPORT void JNICALL Java_test_TRegister_DelKeyC
(JNIEnv *env, jobject obj, jstring strPath, jstring strKeyName)
{



}

//////////////////////////////////////////////////////////////////////////////
// 名  前:Java_test_TRegister_EditValueC
// 形  式:
// 戻 り 値:jint
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
JNIEXPORT void JNICALL Java_test_TRegister_EditValueC
(JNIEnv *env, jobject obj, jstring strPath, jstring strKeyName, jstring strNewValue)
{



}

//////////////////////////////////////////////////////////////////////////////
// 名  前:Java_test_TRegister_GetValueC
// 形  式:
// 戻 り 値:jint
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
JNIEXPORT void JNICALL Java_test_TRegister_GetValueC
(JNIEnv *env, jobject obj, jstring strPath, jstring strKeyName)
{



}

//////////////////////////////////////////////////////////////////////////////
// 名  前:Java_test_TRegister_IsExistsC
// 形  式:
// 戻 り 値:jint
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
JNIEXPORT void JNICALL Java_test_TRegister_IsExistsC
(JNIEnv *env, jobject obj, jstring strPath, jstring strKeyName)
{



}
*/








//////////////////////////////////////////////////////////////////////////////
// 名  前:RegDBGetKeyValueEx
// 形  式:
// 戻 り 値:int
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
int WINAPI RegDBGetKeyValueEx(LPCTSTR lpMachineName, HKEY hRegKey, LPCTSTR lpKeyName, LPCTSTR lpValueName, LPTSTR lpszKeyValue)
{
// Local variables
HKEY hRemoteKey = HKEY_LOCAL_MACHINE ;
HKEY hKey = NULL ;
DWORD dwKeySize; // Size of key value
DWORD dwKeyDataType; // Type of data stored in key
LONG lRC; // Return code
char cMachine[128] = {0} ;

// Initialize variables
dwKeyDataType = 0;
dwKeySize = 0;
/*
if (lpMachineName)
if (lpMachineName[0] != 0)
wsprintf (cMachine, "\\\\%s", lpMachineName);
*/
//Open the key
lRC = RegOpenKeyEx (hRemoteKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
if (lRC == ERROR_SUCCESS)
{
// Got key, get value. First, get the size of the key.
lRC = RegQueryValueEx (hKey, lpValueName, NULL, &dwKeyDataType, NULL, &dwKeySize);

if (dwKeyDataType == REG_DWORD) //DWORD
{
char cData[128] = {0} ;
lRC = RegQueryValueEx (hKey, lpValueName, NULL, &dwKeyDataType, (LPBYTE)cData, &dwKeySize);
ltoa ((LONG)cData[0], lpszKeyValue, 10);
}
else
// Now get the actual key value
lRC = RegQueryValueEx (hKey, lpValueName, NULL, &dwKeyDataType, (LPBYTE)lpszKeyValue, &dwKeySize);
}

RegCloseKey(hKey) ;
RegCloseKey(hRemoteKey) ;
return lRC ;
}

//////////////////////////////////////////////////////////////////////////////
// 名  前:RegDBSetKeyValueEx
// 形  式:
// 戻 り 値:int
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
int WINAPI RegDBSetKeyValueEx(LPCTSTR lpMachineName, HKEY hRegKey, LPCTSTR lpKeyName, LPCTSTR lpValueName, LPCTSTR lpszKeyValue)
{
// Local variables
HKEY hRemoteKey = HKEY_LOCAL_MACHINE ;
HKEY hKey = NULL ;
LONG lRC; // Return code
char cMachine[128] = {0} ;
DWORD dwDisp;

if(lpMachineName)
if(lpMachineName[0] != 0)
wsprintf(cMachine,"\\\\%s",lpMachineName) ;

// Initialize variables
lRC = RegCreateKeyEx (hRemoteKey, lpKeyName, 0, REG_NONE,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp) ;

if (lRC == ERROR_SUCCESS)
{
// Got key, set value.
lRC = RegSetValueEx (hKey, lpValueName, 0, REG_SZ, (LPBYTE)lpszKeyValue, strlen(lpszKeyValue) + 1);
RegCloseKey(hKey);
}

RegCloseKey(hRemoteKey);
return lRC;
}

//////////////////////////////////////////////////////////////////////////////
// 名  前:Java_GetKey_RegDBGetKeyValueEx
// 形  式:
// 戻 り 値:jobject
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////
JNIEXPORT jobject JNICALL Java_test_TOrtRegister_RegDBGetKeyValueEx (JNIEnv* env, jobject this_obj,
jstring jMachineName, jint hRegKey, jstring jKeyName, jstring jValueName)
{
LPCTSTR lpMachineName ;
LPCTSTR lpKeyName ;
LPCTSTR lpValueName ;
char cKeyValue[255] ;
jobject ret ;

lpMachineName = (*env)->GetStringUTFChars (env, jMachineName, 0) ;
lpKeyName = (*env) -> GetStringUTFChars (env, jKeyName, NULL) ;
lpValueName = (*env) -> GetStringUTFChars (env, jValueName, NULL) ;

RegDBGetKeyValueEx (lpMachineName, (HKEY)hRegKey, lpKeyName, lpValueName, cKeyValue) ;

(*env) -> ReleaseStringUTFChars (env, jMachineName, NULL);
(*env) -> ReleaseStringUTFChars (env, jKeyName, NULL);
(*env) -> ReleaseStringUTFChars (env, jValueName, NULL);
ret = (*env) -> NewStringUTF (env, cKeyValue);

return ret;
}

//////////////////////////////////////////////////////////////////////////////
// 名  前:Java_SetKey_RegDBSetKeyValueEx
// 形  式:
// 戻 り 値:jint
// 機能説明:
//
// 改版履歴:
//
//////////////////////////////////////////////////////////////////////////////RegDBSetKeyValueEx
JNIEXPORT jint JNICALL Java_test_TOrtRegister_RegDBSetKeyValueEx (JNIEnv* env, jobject this_obj,
jstring jMachineName, jint hRegKey, jstring jKeyName, jstring jValueName, jstring jKeyValue)
{
LPCTSTR lpMachineName ;
LPCTSTR lpKeyName ;
LPCTSTR lpValueName ;
LPCTSTR lpszKeyValue ;
int iRet ;

lpMachineName = (*env)->GetStringUTFChars(env, jMachineName, NULL) ;
lpKeyName = (*env)->GetStringUTFChars(env, jKeyName, NULL) ;
lpValueName = (*env)->GetStringUTFChars(env, jValueName, NULL) ;
lpszKeyValue = (*env)->GetStringUTFChars(env, jKeyValue, NULL) ;

iRet = RegDBSetKeyValueEx(lpMachineName, (HKEY)hRegKey, lpKeyName, lpValueName, lpszKeyValue) ;

(*env)->ReleaseStringUTFChars(env, jMachineName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jKeyName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jValueName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jKeyValue, NULL) ;

return iRet ;
}
orient_fc 2003-10-21
  • 打赏
  • 举报
回复
JAVA代码:
//**********************************************************//
//クラス概要: DLLを呼び出して レジストリにアクセスのクラス
//クラス名前:TOrtRegister
//作成者:房超
//日付:
//修正描述:
//修正日付:
//修正者:
//クラス属性:
//関数リスト:
// public Register (String strFilePath)
// public String GetKeyValue (String MachineName, int nRegKey,
// String KeyName, String ValueName)
// public int SetKeyValue (String MachineName, int nRegKey,
// String KeyName, String ValueName, String KeyValue)
//**********************************************************//
package test;

public class TOrtRegister extends TObject
{
public static String m_strDPath = "";


{
System.out.println("hello");
System.loadLibrary("register" );
}

public static final int HKEY_CLASSES_ROOT = 0x80000000 ;
public static final int HKEY_CURRENT_USER = 0x80000001 ;
public static final int HKEY_LOCAL_MACHINE = 0x80000002 ;
public static final int HKEY_USERS = 0x80000003 ;
public static final int HKEY_CURRENT_CONFIG = 0x80000005 ;


private int nRegKey = 0 ; //Key value i.e. HKEY_CLASSES_ROOT
public String MachineName = "" ; //Machine name we want to interrogate
public String KeyName = ""; //Name of key we want to set
public String ValueName = ""; //Value of key we want to set
public String KeyValue = ""; //Value we want to set the key to
public int iResult = 1;

public native String RegDBGetKeyValueEx(String MachineName, int nRegKey,
String KeyName, String ValueName) ;
public native int RegDBSetKeyValueEx(String MachineName, int nRegKey,
String KeyName, String ValueName, String KeyValue) ;

//***********************************************************//
//関数描述:クラスの構築
//名前:Register
//入力引数:
//出力引数:
//戻り値:
//**********************************************************//
public TOrtRegister (String strPath)
{
super ();

m_strDPath = strPath;
}

public void SetNRegKey (int nLRegKey)
{
nRegKey = nLRegKey;
}

public int GetNRegKey ()
{
return nRegKey;
}

public void funaa ()
{
int n = 0;
}


//***********************************************************//
//関数描述:
//名前: GetKeyValue
//入力引数:
//出力引数:
//戻り値:
//**********************************************************//

public String GetKeyValue (String MachineName, int nRegKey, String KeyName, String ValueName)
{
return RegDBGetKeyValueEx (MachineName, nRegKey, KeyName, ValueName) ;
}

//***********************************************************
//関数描述:
//名前: SetKeyValue
//入力引数:
//出力引数:
//戻り値:
//**********************************************************//

public int funSetKeyValue (String MachineName, int nRegKey,
String KeyName, String ValueName, String KeyValue)
{
return RegDBSetKeyValueEx (MachineName, nRegKey, KeyName, ValueName, KeyValue) ;
}


iamwls 2003-10-21
  • 打赏
  • 举报
回复
公布代码可好?谢谢
orient_fc 2003-10-21
  • 打赏
  • 举报
回复
TO betterjonah(jonah):
能详细的说明一下吗?谢谢,在线等待
betterjonah 2003-10-21
  • 打赏
  • 举报
回复
试试JAVA_HOME/JRE/BIN/.
如果还不行,这样做。
用.CLASS写一个文件,看这个文件在那个目录,将.DLL放在这个文件所在的目录.

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧