Solved

deleting key from windows registry

Posted on 2002-05-25
5
201 Views
Last Modified: 2012-05-04
Can any one help me out with JNI code snippet to delete the key from windows registry. Key is set in,
HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment

Thanks,
0
Comment
Question by:vibhav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7034607
Have you looked for com.ms classes or other MS java classes on your machine? You might well be able to find the classes that are for the registry.
0
 
LVL 92

Expert Comment

by:objects
ID: 7034880
0
 

Author Comment

by:vibhav
ID: 7035205
to objects:

I need simple delete function in jni...it has lot of feature and bit confusing in th link u provided.
0
 
LVL 92

Expert Comment

by:objects
ID: 7035988
Just extract the delete functionality if that's all you want. I doubt you'll find anything that *just* does delete.
0
 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 100 total points
ID: 7036540
Hi vibhav,

Here is a little sample demonstrates how to delete Windows registry values using JNI:

JNIRegDelSample.java
------------------------
class JNIRegDelSample
{
  private native int deleteRegValue( String key, String subkey, String value );
  public static void main( String[] args )
  {
    String key = "HKEY_LOCAL_MACHINE";
    String subkey =
      "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
    String value = "TEST";
    JNIRegDelSample sample = new JNIRegDelSample();
    System.out.println( sample.deleteRegValue( key, subkey, value ) );
  }
  static {
    System.loadLibrary( "delreg" );
  }
}

JNIRegDelSample.h
------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNIRegDelSample */

#ifndef _Included_JNIRegDelSample
#define _Included_JNIRegDelSample
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     JNIRegDelSample
 * Method:    deleteRegValue
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_JNIRegDelSample_deleteRegValue
  (JNIEnv *, jobject, jstring, jstring, jstring);

#ifdef __cplusplus
}
#endif
#endif

delreg.c
------------------------
#include <string.h>
#include <windows.h>

#include "JNIRegDelSample.h"

JNIEXPORT
jint JNICALL Java_JNIRegDelSample_deleteRegValue
( JNIEnv *env, jobject obj, jstring key, jstring subkey, jstring value )
{
  const char *pszKey = (*env)->GetStringUTFChars( env, key, 0 );
  const char *pszSubKey = (*env)->GetStringUTFChars( env, subkey, 0 );
  const char *pszValue = (*env)->GetStringUTFChars( env, value, 0 );
  HKEY hkey = NULL;
  HKEY hRoot = NULL;
  LONG lResult;

  if ( strcmp( pszKey, "HKEY_CLASSES_ROOT" ) == 0 ) {
    hRoot = HKEY_CLASSES_ROOT;
  } else if ( strcmp( pszKey, "HKEY_CURRENT_CONFIG" ) == 0 ) {
    hRoot = HKEY_CURRENT_CONFIG;
  } else if ( strcmp( pszKey, "HKEY_CURRENT_USER" ) == 0 ) {
    hRoot = HKEY_CURRENT_USER;
  } else if ( strcmp( pszKey, "HKEY_LOCAL_MACHINE" ) == 0 ) {
    hRoot = HKEY_LOCAL_MACHINE;
  } else if ( strcmp( pszKey, "HKEY_USERS" ) == 0 ) {
    hRoot = HKEY_USERS;
  } else {
    return -1;
  }
  lResult = RegOpenKeyEx( hRoot, pszSubKey, 0L, KEY_SET_VALUE, &hkey );
  if ( lResult == ERROR_SUCCESS ) {
    RegDeleteValue( hkey, pszValue );
    RegCloseKey( hkey );
  }
  return lResult;
}

Your steps:
1) javac JNIRegDelSample.java
2) javah JNIRegDelSample
3) cl -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 -LD advapi32.lib delreg.c -Fedelreg.dll

Note that for the step 3) you should setup VC vars (run something like "C:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT").

Ask me more if you need.
Regards, Andrew.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question