示例#1
0
bool setProgramToAutostart(bool value) {
  HKEY key;
  LONG error = RegCreateKeyExW (
    HKEY_CURRENT_USER,
    L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
    0, NULL, REG_OPTION_NON_VOLATILE,
    KEY_ALL_ACCESS, NULL, &key, 0);
  if (error != ERROR_SUCCESS) {
    WIN_WARNING(L"cannot open autostart registry key: %s", getLastWindowsError().c_str());
    return false;
  }

  if (value) {
    error = RegSetValueExW(key, Application::getName().data(), 0, REG_SZ,
      (LPBYTE)(Application::getExecutablePath().data()), (DWORD)((Application::getExecutablePath().size() + 1) * sizeof(wchar_t)));
  } else {
    if (RegQueryValueExW(key, Application::getName().data(),0,0,0,0) != ERROR_FILE_NOT_FOUND)
      error = RegDeleteValueW(key, Application::getName().data());
  }

  RegCloseKey(key);
  if (error != ERROR_SUCCESS) {
    WIN_WARNING(L"cannot set autostart registry value: %s", getLastWindowsError().c_str());
    return false;
  }
  return true;
}
示例#2
0
bool isProgramInAutostart() {
  HKEY key;
  LONG error;

  error = RegCreateKeyExW(
    HKEY_CURRENT_USER,
    L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
    0, NULL, REG_OPTION_NON_VOLATILE,
    KEY_ALL_ACCESS, NULL, &key, 0);
  if (error != ERROR_SUCCESS) {
    WIN_WARNING(L"cannot open autostart registry key: %s", getLastWindowsError().c_str());
    return false;
  }

  wchar_t *buffer;
  DWORD buffersize = 0;

#if (WINVER < _WIN32_WINNT_VISTA)
  DWORD type;
  error = RegQueryValueEx(key, Application::getName().data(), NULL,  &type, NULL, &buffersize);
  if (type != REG_SZ) {
    WIN_WARNING(L"%s", L"autostart registry value has wrong type.");
    RegCloseKey(key);
    return false;
  }
#else
  error = RegGetValue(key, NULL, Application::getName().data(), RRF_RT_REG_SZ, NULL, NULL, &buffersize);
#endif
  if (error == ERROR_SUCCESS) {
    buffer = (wchar_t*)malloc(sizeof(wchar_t) * (buffersize+1));
#if (WINVER < _WIN32_WINNT_VISTA)
    error = RegQueryValueEx(key, Application::getName().data(), NULL,  &type, (BYTE*)buffer, &buffersize);
#else
    error = RegGetValue(key, NULL, Application::getName().data(), RRF_RT_REG_SZ, NULL, buffer, &buffersize);
#endif
  }
  bool result;
  if (error != ERROR_SUCCESS) {
    if (error != ERROR_FILE_NOT_FOUND) {
      WIN_WARNING(L"cannot read autostart registry value: %s", getLastWindowsError().c_str());
    }
    result = false;
  } else {
    result = (Application::getExecutablePath().compare(buffer) == 0);
  }

  RegCloseKey(key);

  return result;
}
示例#3
0
bool AvancedQFile::open(OpenMode mode)
{
    fileError=QFileDevice::NoError;
    fileErrorString.clear();
    WCHAR fileNameW[fileName().size()+1];
    if(QDir::toNativeSeparators("\\\\?\\"+fileName()).toWCharArray(fileNameW)!=fileName().size())
    {
        fileError=QFileDevice::OpenError;
        fileErrorString=tr("Path conversion error");
        return false;
    }
    fileNameW[fileName().size()]='\0';

    DWORD dwDesiredAccess=0;
    if(mode & QIODevice::ReadOnly)
        dwDesiredAccess|=GENERIC_READ;
    if(mode & QIODevice::WriteOnly)
        dwDesiredAccess|=GENERIC_Write;

    DWORD dwCreationDisposition;
    if(mode & QIODevice::WriteOnly)
        dwCreationDisposition=CREATE_ALWAYS;
    else
        dwCreationDisposition=OPEN_EXISTING;

    handle=CreateFile(
      fileNameW,
      dwDesiredAccess,
      0,
      0,
      dwCreationDisposition,
      FILE_FLAG_WRITE_THROUGH | FILE_FLAG_SEQUENTIAL_SCAN,
      0
    );
    if(handle==INVALID_HANDLE_VALUE)
    {
        fileError=QFileDevice::OpenError;
        fileErrorString=getLastWindowsError();
    }
    return (handle!=INVALID_HANDLE_VALUE);
}