accessing the registry

S

soni29

hi,
i know that since the registry is only part of windows there probably
isn't a class in the sdk that will allow me to read from it. but does
anyone know of any free third party classes i can use? i've searched
and read about using jni, but don't know much about c or how to get
into the registry using c++. also on some forums someone mentioned
using the preferences class in the sdk, but i'm not 100% about that.
what i need to do is find out if the user has ms office installed on
their pc, if so where, i need to be able to launch office exe from my
app, we were building something we thought was going to run on linux
and windows, but all of a sudden my manager came and said forget it
now only windows, and he wants to try to add the launching ms office
suite into it. any ideas?

Thank you.
 
T

Tim Tyler

: i know that since the registry is only part of windows there probably
: isn't a class in the sdk that will allow me to read from it. but does
: anyone know of any free third party classes i can use?
: i've searched and read about using jni, but don't know much about c or
: how to get into the registry using c++. also on some forums someone
: mentioned using the preferences class in the sdk, but i'm not 100%
: about that.

You can read from the registry in Java using java.util.prefs.

However you can only read out things you've put in.

The location of the MS office suite won't fit in that category.

Have you tried "exec"ing an appropriate file type?
 
R

Roedy Green

you need a tiny bit of C++ and JNI glue to hook into the registry
manipulating api.

I would create it for you for $10 US.
 
G

Grant Wagner

soni29 said:
hi,
i know that since the registry is only part of windows there probably
isn't a class in the sdk that will allow me to read from it. but does
anyone know of any free third party classes i can use? i've searched
and read about using jni, but don't know much about c or how to get
into the registry using c++. also on some forums someone mentioned
using the preferences class in the sdk, but i'm not 100% about that.
what i need to do is find out if the user has ms office installed on
their pc, if so where, i need to be able to launch office exe from my
app, we were building something we thought was going to run on linux
and windows, but all of a sudden my manager came and said forget it
now only windows, and he wants to try to add the launching ms office
suite into it. any ideas?

Thank you.

Why do you need to read the Registry in order to launch MS Office
applications?

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd /c start MyWordDocument.doc");

If you want the path to a Microsoft Office program that handles a
particular document type, then you can retrieve that as well:

import java.io.*;

public class FileHandler {
public static void main(String[] s) {
String extension = "";
if (s.length == 1) {
extension = s[0];
try {
String output;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd.exe /c " +
"for /f \"tokens=2 delims==\" %a " +
"in ('assoc ." + extension + "') " +
"do @for /f \"tokens=2 delims==\" %b " +
"in ('ftype %a') do @echo %b");

InputStreamReader isr = new
InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
try {
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (IOException e) {
System.err.println(e);
}
} catch (IOException e1) {
System.err.println(e1);
}
} else {
System.out.println("Usage: FileHandler [extension]");
}
}
}

"java FileHandler doc" reports "C:\Program Files\Microsoft
Office\Office\Winword.exe" /n on my machine.

Note that the above will only work on Windows 2000/XP.
 
S

soni29

Got it, thank you.

Grant Wagner said:
soni29 said:
hi,
i know that since the registry is only part of windows there probably
isn't a class in the sdk that will allow me to read from it. but does
anyone know of any free third party classes i can use? i've searched
and read about using jni, but don't know much about c or how to get
into the registry using c++. also on some forums someone mentioned
using the preferences class in the sdk, but i'm not 100% about that.
what i need to do is find out if the user has ms office installed on
their pc, if so where, i need to be able to launch office exe from my
app, we were building something we thought was going to run on linux
and windows, but all of a sudden my manager came and said forget it
now only windows, and he wants to try to add the launching ms office
suite into it. any ideas?

Thank you.

Why do you need to read the Registry in order to launch MS Office
applications?

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd /c start MyWordDocument.doc");

If you want the path to a Microsoft Office program that handles a
particular document type, then you can retrieve that as well:

import java.io.*;

public class FileHandler {
public static void main(String[] s) {
String extension = "";
if (s.length == 1) {
extension = s[0];
try {
String output;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd.exe /c " +
"for /f \"tokens=2 delims==\" %a " +
"in ('assoc ." + extension + "') " +
"do @for /f \"tokens=2 delims==\" %b " +
"in ('ftype %a') do @echo %b");

InputStreamReader isr = new
InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
try {
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (IOException e) {
System.err.println(e);
}
} catch (IOException e1) {
System.err.println(e1);
}
} else {
System.out.println("Usage: FileHandler [extension]");
}
}
}

"java FileHandler doc" reports "C:\Program Files\Microsoft
Office\Office\Winword.exe" /n on my machine.

Note that the above will only work on Windows 2000/XP.
 
S

soni29

Thank you again for the response and solution, if possible could you
tell me what this means in your code:

Process p = rt.exec("cmd.exe /c " +
"for /f \"tokens=2 delims==\" %a " +
"in ('assoc ." + extension + "') " +
"do @for /f \"tokens=2 delims==\" %b " +
"in ('ftype %a') do @echo %b");

i know the Process class, and cmd.exe just trying to see the for
/f.... where did that come from?

Thank you.

Got it, thank you.

Grant Wagner said:
soni29 said:
hi,
i know that since the registry is only part of windows there probably
isn't a class in the sdk that will allow me to read from it. but does
anyone know of any free third party classes i can use? i've searched
and read about using jni, but don't know much about c or how to get
into the registry using c++. also on some forums someone mentioned
using the preferences class in the sdk, but i'm not 100% about that.
what i need to do is find out if the user has ms office installed on
their pc, if so where, i need to be able to launch office exe from my
app, we were building something we thought was going to run on linux
and windows, but all of a sudden my manager came and said forget it
now only windows, and he wants to try to add the launching ms office
suite into it. any ideas?

Thank you.

Why do you need to read the Registry in order to launch MS Office
applications?

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd /c start MyWordDocument.doc");

If you want the path to a Microsoft Office program that handles a
particular document type, then you can retrieve that as well:

import java.io.*;

public class FileHandler {
public static void main(String[] s) {
String extension = "";
if (s.length == 1) {
extension = s[0];
try {
String output;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd.exe /c " +
"for /f \"tokens=2 delims==\" %a " +
"in ('assoc ." + extension + "') " +
"do @for /f \"tokens=2 delims==\" %b " +
"in ('ftype %a') do @echo %b");

InputStreamReader isr = new
InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
try {
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (IOException e) {
System.err.println(e);
}
} catch (IOException e1) {
System.err.println(e1);
}
} else {
System.out.println("Usage: FileHandler [extension]");
}
}
}

"java FileHandler doc" reports "C:\Program Files\Microsoft
Office\Office\Winword.exe" /n on my machine.

Note that the above will only work on Windows 2000/XP.
 
D

Daniel Hagen

hi,
i know that since the registry is only part of windows there probably
isn't a class in the sdk that will allow me to read from it. but does
anyone know of any free third party classes i can use? i've searched

Depends on the VM you are using. With the Microsoft VM, you can use
the Registry Classes from com.ms.wfc.app for reading from and writing
to the registry.

Regards

Daniel Hagen
 
G

Grant Wagner

cmd.exe /c just executes the command that follows it in a Windows NT command prompt, the rest is:

for /f "tokens=2 delims==" %a in ('assoc .doc') do @for /f "tokens=2 delims==" %b in ('ftype %a') do @echo %b

- for /f executes the "for" command with the ability to retrieve formatting
- "tokens=2 delims==" says, split the retrieved result at "=" and return the 2nd token
- %a is the variable to place the result in
- 'assoc .doc' is executed and in the case of .doc, returns ".doc=Word.Document.8"
- "Word.Document.8" (token 2) is placed in %a
- the "do" is preformed
- @for /f executes "for" with formatting options again, the @ says to suppress echoing the command
- 'ftype %a' (%a now contains Word.Document.8) is executed, and the stuff after the = is placed in %b
- %b is then @echoed (again, @ suppresses the actual command), so Java can retrieve the result

All the above basically executes:

assoc .doc
returns ".doc=Word.Document.8"
ftype Word.Document.8
returns "Word.Document.8="C:\Program Files\Microsoft Office\Office\Winword.exe" /n"

I came up with this solution when I needed access to the "help application" for a particular file type in some
batch files, the nice thing is that if you have a language that can call system commands (such as Java), it
works equally well there, all without requiring any actual Registry access.

I also wanted to demonstrate the power of the "for" command in Windows NT.
Thank you again for the response and solution, if possible could you
tell me what this means in your code:

Process p = rt.exec("cmd.exe /c " +
"for /f \"tokens=2 delims==\" %a " +
"in ('assoc ." + extension + "') " +
"do @for /f \"tokens=2 delims==\" %b " +
"in ('ftype %a') do @echo %b");

i know the Process class, and cmd.exe just trying to see the for
/f.... where did that come from?

Thank you.

Got it, thank you.

Grant Wagner said:
soni29 wrote:

hi,
i know that since the registry is only part of windows there probably
isn't a class in the sdk that will allow me to read from it. but does
anyone know of any free third party classes i can use? i've searched
and read about using jni, but don't know much about c or how to get
into the registry using c++. also on some forums someone mentioned
using the preferences class in the sdk, but i'm not 100% about that.
what i need to do is find out if the user has ms office installed on
their pc, if so where, i need to be able to launch office exe from my
app, we were building something we thought was going to run on linux
and windows, but all of a sudden my manager came and said forget it
now only windows, and he wants to try to add the launching ms office
suite into it. any ideas?

Thank you.

Why do you need to read the Registry in order to launch MS Office
applications?

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd /c start MyWordDocument.doc");

If you want the path to a Microsoft Office program that handles a
particular document type, then you can retrieve that as well:

import java.io.*;

public class FileHandler {
public static void main(String[] s) {
String extension = "";
if (s.length == 1) {
extension = s[0];
try {
String output;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd.exe /c " +
"for /f \"tokens=2 delims==\" %a " +
"in ('assoc ." + extension + "') " +
"do @for /f \"tokens=2 delims==\" %b " +
"in ('ftype %a') do @echo %b");

InputStreamReader isr = new
InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
try {
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (IOException e) {
System.err.println(e);
}
} catch (IOException e1) {
System.err.println(e1);
}
} else {
System.out.println("Usage: FileHandler [extension]");
}
}
}

"java FileHandler doc" reports "C:\Program Files\Microsoft
Office\Office\Winword.exe" /n on my machine.

Note that the above will only work on Windows 2000/XP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top