Solved

deleting key from windows registry

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 47
maven disable workspace resolution 1 23
Selenium docs api java index 3 48
American Express @Work site and Java 4 30
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

828 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