win32.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file win32.cpp
00024  * \version $Id: string.h 700 2006-04-15 17:11:09Z edmanm $ 
00025  * \brief Win32-specific functions
00026  */
00027 
00028 #include "win32.h"
00029 #include <tlhelp32.h>
00030 #include <shlobj.h>
00031 #include <QDir>
00032 
00033 
00034 /** Finds the location of the "special" Windows folder using the given CSIDL
00035  * value. If the folder cannot be found, the given default path is used. */
00036 QString
00037 win32_get_folder_location(int folder, QString defaultPath)
00038 {
00039   TCHAR path[MAX_PATH+1];
00040   LPITEMIDLIST idl;
00041   IMalloc *m;
00042   HRESULT result;
00043 
00044   /* Find the location of %PROGRAMFILES% */
00045   if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, folder, &idl))) {
00046     /* Get the path from the IDL */
00047     result = SHGetPathFromIDList(idl, path);
00048     SHGetMalloc(&m);
00049     if (m) {
00050       m->Release();
00051     }
00052     if (SUCCEEDED(result)) {
00053       QT_WA(return QString::fromUtf16((const ushort *)path);,
00054             return QString::fromLocal8Bit((char *)path);)
00055     }
00056   }
00057   return defaultPath;
00058 }
00059 
00060 /** Gets the location of the user's %PROGRAMFILES% folder. */
00061 QString
00062 win32_program_files_folder()
00063 {
00064   return win32_get_folder_location(
00065      CSIDL_PROGRAM_FILES, QDir::rootPath() + "\\Program Files");
00066 }
00067 
00068 /** Gets the location of the user's %APPDATA% folder. */
00069 QString
00070 win32_app_data_folder()
00071 {
00072   return win32_get_folder_location(
00073       CSIDL_APPDATA, QDir::homePath() + "\\Application Data");
00074 }
00075 
00076 /** Returns the value in keyName at keyLocation. 
00077  *  Returns an empty QString if the keyName doesn't exist */
00078 QString
00079 win32_registry_get_key_value(QString keyLocation, QString keyName)
00080 {
00081   HKEY key;
00082   char data[255] = {0};
00083   DWORD size = sizeof(data);
00084 
00085   /* Open the key for reading (opens new key if it doesn't exist) */
00086   if (RegOpenKeyExA(HKEY_CURRENT_USER,
00087                     qPrintable(keyLocation), 
00088                     0L, KEY_READ, &key) == ERROR_SUCCESS) {
00089     
00090     /* Key exists, so read the value into data */
00091     RegQueryValueExA(key, qPrintable(keyName), 
00092                     NULL, NULL, (LPBYTE)data, &size);
00093   }
00094 
00095   /* Close anything that was opened */
00096   RegCloseKey(key);
00097 
00098   return QString(data);
00099 }
00100 
00101 /** Creates and/or sets the key to the specified value */
00102 void
00103 win32_registry_set_key_value(QString keyLocation, QString keyName, QString keyValue)
00104 {
00105   HKEY key;
00106   
00107   /* Open the key for writing (opens new key if it doesn't exist */
00108   if (RegOpenKeyExA(HKEY_CURRENT_USER,
00109                    qPrintable(keyLocation),
00110                    0, KEY_WRITE, &key) != ERROR_SUCCESS) {
00111 
00112     /* Key didn't exist, so write the newly opened key */
00113     RegCreateKeyExA(HKEY_CURRENT_USER,
00114                    qPrintable(keyLocation),
00115                    0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
00116                    &key, NULL);
00117   }
00118 
00119   /* Save the value in the key */
00120   RegSetValueExA(key, qPrintable(keyName), 0, REG_SZ, 
00121                 (BYTE *)qPrintable(keyValue),
00122                 (DWORD)keyValue.length() + 1); // include null terminator
00123 
00124   /* Close the key */
00125   RegCloseKey(key);
00126 }
00127 
00128 /** Removes the key from the registry if it exists */
00129 void
00130 win32_registry_remove_key(QString keyLocation, QString keyName)
00131 {
00132   HKEY key;
00133   
00134   /* Open the key for writing (opens new key if it doesn't exist */
00135   if (RegOpenKeyExA(HKEY_CURRENT_USER,
00136                    qPrintable(keyLocation),
00137                    0, KEY_SET_VALUE, &key) == ERROR_SUCCESS) {
00138   
00139     /* Key exists so delete it */
00140     RegDeleteValueA(key, qPrintable(keyName));
00141   }
00142 
00143   /* Close anything that was opened */
00144   RegCloseKey(key);
00145 }
00146 
00147 /** Returns a list of all currently active processes, including their pid
00148  * and exe filename. */
00149 QHash<qint64, QString>
00150 win32_process_list()
00151 {
00152   QHash<qint64, QString> procList;
00153   HANDLE hSnapshot;
00154   PROCESSENTRY32 proc;
00155   QString exeFile;
00156   qint64 pid;
00157  
00158   /* Create a snapshot of all active processes */
00159   hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
00160   if (hSnapshot != INVALID_HANDLE_VALUE) {
00161     proc.dwSize = sizeof(PROCESSENTRY32);
00162     
00163     /* Iterate through all the processes in the snapshot */
00164     if (Process32First(hSnapshot, &proc)) {
00165       do {
00166         /* Extract the PID and exe filename from the process record */
00167         pid = (qint64)proc.th32ProcessID;
00168         QT_WA(
00169           exeFile = QString::fromUtf16((const ushort *)proc.szExeFile);,
00170           exeFile = QString::fromAscii((const char *)proc.szExeFile);
00171         )
00172         /* Add this process to our list */
00173         procList.insert(pid, exeFile);
00174       } while (Process32Next(hSnapshot, &proc));
00175     }
00176     CloseHandle(hSnapshot);
00177   }
00178   return procList;
00179 }
00180 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0