Solved

deleting key from windows registry

Posted on 2002-05-25
5
194 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
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
0
 

Author Comment

by:vibhav
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now