poco/Foundation/src/Environment_WIN32.cpp
Guenter Obiltschnig f476bd6b32 initial import
2006-07-11 16:33:40 +00:00

148 lines
4.3 KiB
C++

//
// Environment_WIN32.cpp
//
// $Id: //poco/1.1.0/Foundation/src/Environment_WIN32.cpp#2 $
//
// Library: Foundation
// Package: Core
// Module: Environment
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Foundation/Environment_WIN32.h"
#include "Foundation/Exception.h"
#include <sstream>
#include <windows.h>
Foundation_BEGIN
std::string EnvironmentImpl::getImpl(const std::string& name)
{
DWORD len = GetEnvironmentVariable(name.c_str(), 0, 0);
if (len == 0) throw NotFoundException(name);
char* buffer = new char[len];
GetEnvironmentVariable(name.c_str(), buffer, len);
std::string result(buffer);
delete [] buffer;
return result;
}
bool EnvironmentImpl::hasImpl(const std::string& name)
{
DWORD len = GetEnvironmentVariable(name.c_str(), 0, 0);
return len > 0;
}
void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
{
if (SetEnvironmentVariable(name.c_str(), value.c_str()) == 0)
{
std::string msg = "cannot set environment variable: ";
msg.append(name);
throw SystemException(msg);
}
}
std::string EnvironmentImpl::osNameImpl()
{
OSVERSIONINFO vi;
vi.dwOSVersionInfoSize = sizeof(vi);
if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
switch (vi.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
return "Windows 3.x";
case VER_PLATFORM_WIN32_WINDOWS:
return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
case VER_PLATFORM_WIN32_NT:
return "Windows NT";
default:
return "Unknown";
}
}
std::string EnvironmentImpl::osVersionImpl()
{
OSVERSIONINFO vi;
vi.dwOSVersionInfoSize = sizeof(vi);
if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
std::ostringstream str;
str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
if (vi.szCSDVersion[0]) str << ": " << vi.szCSDVersion;
str << ")";
return str.str();
}
std::string EnvironmentImpl::osArchitectureImpl()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
switch (si.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_INTEL:
return "IA32";
case PROCESSOR_ARCHITECTURE_MIPS:
return "MIPS";
case PROCESSOR_ARCHITECTURE_ALPHA:
return "ALPHA";
case PROCESSOR_ARCHITECTURE_PPC:
return "PPC";
case PROCESSOR_ARCHITECTURE_IA64:
return "IA64";
#ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
return "IA64/32";
#endif
#ifdef PROCESSOR_ARCHITECTURE_AMD64
case PROCESSOR_ARCHITECTURE_AMD64:
return "AMD64";
#endif
default:
return "Unknown";
}
}
std::string EnvironmentImpl::nodeNameImpl()
{
char name[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = sizeof(name);
if (GetComputerName(name, &size) == 0) throw SystemException("Cannot get computer name");
return std::string(name);
}
Foundation_END