Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
HI!

I want to write a program in c/c++ that will find the installed location of OpenOffice.org and then set that location to the path environment variable in windows registry.

I did this in c# but cannot figure out how to do it in c/c++ since am new to it.

Any help will highly be appreciated.

thx
alfu

Hi John! Below is the c# code:

string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey, true);

//Go through the registry keys and get OpenOffice.org
foreach (string skName in rk.GetSubKeyNames())
{
   RegistryKey sk = rk.OpenSubKey(skName);

   try
   {
       if (Regex.IsMatch((sk.GetValue("DisplayName")).ToString(), "OpenOffice.org", RegexOptions.IgnoreCase))
       {

              path = "" + sk.GetValue("InstallLocation") + sk.GetValue("DisplayName");
       }
   }
   catch (Exception ex)
   {
                   
   }
string Pathvalue = "";
Pathvalue = Environment.GetEnvironmentVariable("Path");// get env. path
Pathvalue = Pathvalue + ";" + path; // append office path to env path

RegistryKey regkey;
regkey= Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", true);
try
{
   //Set path variable to value
   regkey.SetValue("Path", Pathvalue);
   regkey.Close();
}
catch (Exception ex)
{
}

Reason for doing this:
We are using a software that we build in house that needs open office to be set in the environment path and UNO path, so that it can work. What I do not want to do is to set this path for each client PC which will be painful.

Therefore, I need a way to do it automatically and this is why I want to write a program for it.

The problem with using the c# program is that all client machines must have .net framework 3 atleast for the c# .exe to execute.

I am using the installjammer installer to automatically run the c# .exe but I first check if the PC has .netFramework installed, and if not, installjammer will first install the framework and then run the c# .exe.

My boss told me to scrap it and use something simple. He said it doesnt make sense to install .netFramework just for achieving a simple task of setting a path.

Thanks

SA,

Many thanks. Your point is well noted. The only problem is I cannot avoid setting the path. Open Office needs UNO path to be set inorder for it to work and our application fully integrates with Open Office. It will a real pain and time consuming to modify the open office source code for it to be able to do that and second I dont want to be doing this manually. Your concern is understandable as a developer, but our application has an uninstaller which takes care of that once it is uninstalled
Posted
Updated 20-Apr-11 0:35am
v4
Comments
Henry Minute 18-Apr-11 16:00pm    
As John has said, if you show the code you used in C# you will get more help.
Sergey Alexandrovich Kryukov 19-Apr-11 0:46am    
Why would you touch environment variable? Dirty way.
--SA
Alfu Jallow 19-Apr-11 13:10pm    
Then suggest to me what is the clean way of doing it. I think that is why the forum exist in the first place.
alf
Sergey Alexandrovich Kryukov 19-Apr-11 13:57pm    
Sure, but you cannot blame me -- I did not have enough information at the moment I posted my comment, you probably added it later.
Please see my Answer, discuss it, ask a follow-up question...
--SA
Sandeep Mewara 19-Apr-11 4:38am    
what have you tried? I mean ... anything?
for now... "any help" is like... what to help with!

//for open office 2
OO2Path = "SOFTWARE\\OpenOffice.org\\UNO\\InstallPath";
OO2Key = "";
//for open office 3
OO3Path = "SOFTWARE\\OpenOffice.org\\Layers\\OpenOffice.org\\3";
OO3Key = "OFFICEINSTALLLOCATION";

You can use these strings and retrieve their values from registry and it will contain location of directory where open-office is installed...
if it returns nothing, that means OO is not installed.. :(

For reading registry values see : @ MSDN[]

Hope this may help you!!! :)
 
Share this answer
 
Comments
Alfu Jallow 19-Apr-11 10:12am    
Hi Bharat!
Thanks for your response. Any sample code to achieve this?

Am using RegGetValue() and RegOpenKeyEx() but it throws the exception "the proceedure entry point RegGetValueA could not be located in the dynamic link library", but I check in the MSDN site and I was made to understand that the functions are system specific.
This is one alternative to using Environment variable:

You can find out where OpenOffice is — you do it already as I understand. After this is done, your installation should run some code creating the application configuration file with a custom parameter showing where OpenOffice is, deploy it through installation. You can have this procedure as a utility, a part of your appliacation, in case the user wants to reinstall OpenOffice. The application should read this configuration file and run having the path to OpenOffice in place.

Please understand that adding Environment variable is discouraged. Who will remove it if your software is uninstalled? There are too many software products contaminated the Registry.

Of course, my advice will not work if you already have a legacy product which uses some Environment variable conventions and you don't have access to its source code. Should you have any chance to change is — do it as soon as you can, get rid of dirty practices.

—SA
 
Share this answer
 
Comments
Espen Harlinn 19-Apr-11 16:33pm    
My 5!
Sergey Alexandrovich Kryukov 19-Apr-11 16:49pm    
Thank you, Espen.
--SA
Show us the C# code you use (it shouldn't be a lot of code).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900