Solved

deleting key from windows registry

Posted on 2002-05-25
5
198 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 85
check java version using powershell 13 93
spring jars download 1 28
Which non-HTML GUI front end to use with Java? 3 22
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 …
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

809 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