UC Browser

Tuesday, 28 May 2013

Read all values inside a Registry Key C++

Code:

#include "stdafx.h"
#include <windows.h>

#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383

void QueryKey(HKEY hKey) 
 { 
 TCHAR    achKey[MAX_KEY_LENGTH];   // buffer for subkey name
 DWORD    cbName;                   // size of name string 
 TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
 DWORD    cchClassName = MAX_PATH;  // size of class string 
 DWORD    cSubKeys=0;               // number of subkeys 
 DWORD    cbMaxSubKey;              // longest subkey size 
 DWORD    cchMaxClass;              // longest class string 
 DWORD    cValues;              // number of values for key 
 DWORD    cchMaxValue;          // longest value name 
 DWORD    cbMaxValueData;       // longest value data 
 DWORD    cbSecurityDescriptor; // size of security descriptor 
 FILETIME ftLastWriteTime;      // last write time 

 DWORD i, retCode; 

 TCHAR  achValue[MAX_VALUE_NAME]; 
 DWORD cchValue = MAX_VALUE_NAME; 

 // Get the class name and the value count. 
 retCode = RegQueryInfoKey(
  hKey,                    // key handle 
  achClass,                // buffer for class name 
  &cchClassName,           // size of class string 
  NULL,                    // reserved 
  &cSubKeys,               // number of subkeys 
  &cbMaxSubKey,            // longest subkey size 
  &cchMaxClass,            // longest class string 
  &cValues,                // number of values for this key 
  &cchMaxValue,            // longest value name 
  &cbMaxValueData,         // longest value data 
  &cbSecurityDescriptor,   // security descriptor 
  &ftLastWriteTime);       // last write time 

 // Enumerate the subkeys, until RegEnumKeyEx fails.

 if (cSubKeys)
  {
  printf( "\nNumber of subkeys: %d\n", cSubKeys);

  for (i=0; i<cSubKeys; i++) 
   { 
   cbName = MAX_KEY_LENGTH;
   retCode = RegEnumKeyEx(hKey, i,
    achKey, 
    &cbName, 
    NULL, 
    NULL, 
    NULL, 
    &ftLastWriteTime); 
   if (retCode == ERROR_SUCCESS) 
    {
    _tprintf(TEXT("(%d) %s\n"), i+1, achKey);
    }
   }
  } 

 // Enumerate the key values. 

 BYTE* buffer = new BYTE[cbMaxValueData];
 ZeroMemory(buffer, cbMaxValueData);

 if (cValues) 
  {
  printf( "\nNumber of values: %d\n", cValues);

  for (i=0, retCode = ERROR_SUCCESS; i<cValues; i++) 
   { 
   cchValue = MAX_VALUE_NAME; 
   achValue[0] = '\0'; 
   retCode = RegEnumValue(hKey, i, 
    achValue, 
    &cchValue, 
    NULL, 
    NULL,
    NULL,
    NULL);

   if (retCode == ERROR_SUCCESS ) 
    { 
    
    DWORD lpData = cbMaxValueData;
    buffer[0] = '\0';
    LONG dwRes = RegQueryValueEx(hKey, achValue, 0, NULL, buffer, &lpData);
    _tprintf(TEXT("(%d) %s : %s\n"), i+1, achValue, buffer); 
    } 
   }
  }
 delete [] buffer;
 }


int _tmain(int argc, _TCHAR* argv[])
{
HKEY hKey;
LONG dwRegOPenKey = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\"), 0, KEY_READ, &hKey);
if(dwRegOPenKey == ERROR_SUCCESS){
 printf("RegOpenKeyEx succeeded, error code %d\n", GetLastError());
 QueryKey(hKey);

 } else {
  printf("RegOpenKeyEx failed, error code %d\n", dwRegOPenKey);
 }
 RegCloseKey(hKey);
return 0;
}
If you have any issues with the information provided, feel free to mail me at manigandasuresh@gmail.com

Monday, 4 February 2013

Blocking an Application in Windows



          If you a computer administrator and you do not want user run a few program in the computer such as game, browser, etc. you can block the application from running using registry. You can block the program just add a few entry to your registry setting. I think this tips is useful trick to System Administrator or the for the people who have a PC and don't want other user (friend or family) running a few program in their PC.

          Usually the program can be blocked on the PC to prevent other user change setting in the computer such as regedit.exe, cmd.exe, etc. Other benefit, with this tricks you can block windows application from running without windows application blocker software. To do this tips is easy you can change some registry setting on your Windows PC. I have tested this tips in my computer and it work.

Here comes the way to Block Specific Program from Running in Windows:

  • Open your registry editor (Click Start-Run then type regedit), then find the following key:
    HKEY_CURRENT_USER / Software / Microsoft / Windows / CurrentVersion / Policies / Explorer
  • If you have found the key, add new DWORD Value and rename it to be DisallowRun.
  • Double click the value and set data value to be 1.
  • Create new key under Explorer key and rename it to be DisallowRun, so the complete key will be like this:
    HKEY_CURRENT_USER / Software / Microsoft / Windows / CurrentVersion / Policies / Explorer / DisallowRun
  • On DisallowRun key, create new String Value, and rename it as anything you like(example: application1). Double click the value and set the data value by application that you want to block. (Example winamp.exe, cmd.exe, or regedit.exe)
  • Close your registry editor.
  • Logout or restart PC for changes to take effect.
        This method will work in Windows starting from XP.


If you have any issues with the information provided, feel free to mail me at manigandasuresh@gmail.com

Thursday, 10 January 2013

Convert LPWSTR to string

C++ Code:

bool LPWSTR2String(std::string& outString, const LPWSTR inLPWSTR, UINT codepage = CP_ACP)
{
    bool retCode = false;
    char* temp = 0;
    int bytesRequired;
    bytesRequired = WideCharToMultiByte(codepage, 0, inLPWSTR, -1, 0, 0, 0, 0);
    if (bytesRequired > 0)
    {
        temp = new char[bytesRequired];
        int rc = WideCharToMultiByte(codepage, 0, inLPWSTR, -1, temp, bytesRequired, 0, 0);
        if (rc != 0)
        {
            temp[bytesRequired-1] = 0;
            outString = temp;
            retCode = true;
        }
    }
    delete [] temp;
    return retCode;
}