?
Solved

deleting key from windows registry

Posted on 2002-05-25
5
Medium Priority
?
202 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 300 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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