Posts com Tag ‘Registro’

Fala Pessoal,

Essa semana novamente voltei a trabalhar com o registro do Windows mas dessa vez tive que trabalhar tanto com sistemas 32 e 64 bits.

Como a ferramenta que uso há somente a versão 32bits e caso o usuário instale em um computador 64 bits ele cria uma chave especial no registro  para o software (HKEY_LOCAL_MACHINE\Software\WOW6432Node), veja aqui

O Código abaixo foi usando o Visual C++ no Microsoft Visual Studio 2010 😦 , no exemplo abaixo estou pegando o caminho de instalação do 7Zip:

CString ReadProxyServer()
{
  CString cSvar = _T("");
  HKEY hKey;
  if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Automatos\\ASU\\Config"),0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
  {
    if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Automatos\\ASU\\Config"),0,KEY_WOW64_32KEY|KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS)
    {
      std::cout << "ERROR! Path not found! "<<"\n";
      std::exit;
    }
  }
  TCHAR szData[256];
  DWORD dwKeyDataType;
  DWORD dwDataBufSize = 256;
  if (::RegQueryValueEx(hKey, _T("ConfigDir"), NULL, &dwKeyDataType,(LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
  {
    switch ( dwKeyDataType )
    {
       case REG_SZ:
       cSvar = CString(szData);
       break;
    }
  }
  ::RegCloseKey( hKey );
  return cSvar;
 }

int main()

{
   //Convertendo o valor vindo do Registro
   CT2CA pszConvertedAnsiString (ReadProxyServer());
   std::string strStd (pszConvertedAnsiString);
   std::cout << strStd.c_str() << std::endl;
   return (0);
}

Abraços

Fala pessoal, blz?

Essa semana estive trabalhando com o Windows e aprendi a trabalhar com o registro do Sistema Operacional através de um programa em C++.

Segue abaixo um exemplo de como ler um registro em um Windows 64bits.


#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

int RegEdit()
{
 HKEY hKey = 0;
 char buf[MAX_PATH];
 std::string nome;
 DWORD dwType = 0;
 DWORD dwBufSize = MAX_PATH;
 int result;

result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",0,KEY_WOW64_64KEY|KEY_QUERY_VALUE,&hKey);

if(result == ERROR_SUCCESS)
 {
 dwType = REG_SZ;
 result = 0;
 result = RegQueryValueEx(hKey,L"DisplayName",NULL, &dwType, (BYTE*)buf, &dwBufSize);
 if(result == ERROR_SUCCESS)
 {
 int i = 0;
 do
 {
 nome = buf[i];
 i++;

 }
 while(i<dwBufSize);
 cout<<"\n";
 }
 else
 {
 cout << "can not query for key value\n";
 }

&nbsp;

}//end if
 RegCloseKey(hKey);
 return 0;
}//end function

int main()
{
 RegEdit();
 system("PAUSE");
}

Se fosse para escolher trabalharia com Linux, mas como nem tudo é como queremos, vou aproveitar para aprender o máximo que der.

Abraços