第三方框架改为仓内子集并完善构建忽略规则
完整 NIM_Duilib_Framework 仓库移至父目录,third_party 仅保留实际使用的 base、duilib、bin/resources 与许可证;.gitignore 覆盖 MSBuild 与打包产物。
This commit is contained in:
1 parent
249e8548de
commit
e1e26acc49
391 files changed
+342850
No files matched your search
@@ -0,0 +1,108 @@
|
||||
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Ported & Modified by wrt(guangguang)
|
||||
// 2013/9/3
|
||||
|
||||
#include "base/win32/object_watcher.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace nbase {
|
||||
namespace win32 {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ObjectWatcher::ObjectWatcher()
|
||||
: object_(NULL),
|
||||
wait_object_(NULL),
|
||||
origin_loop_(NULL) {
|
||||
}
|
||||
|
||||
ObjectWatcher::~ObjectWatcher() {
|
||||
StopWatching();
|
||||
}
|
||||
|
||||
bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) {
|
||||
if (wait_object_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Since our job is to just notice when an object is signaled and report the
|
||||
// result back to this thread, we can just run on a Windows wait thread.
|
||||
DWORD wait_flags = WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE;
|
||||
|
||||
// DoneWaiting can be synchronously called from RegisterWaitForSingleObject,
|
||||
// so set up all state now.
|
||||
callback_ = nbase::Bind(&ObjectWatcher::Signal, this, delegate);
|
||||
callback_ = signal_weakflag_.ToWeakCallback(callback_);
|
||||
object_ = object;
|
||||
origin_loop_ = MessageLoop::current();
|
||||
|
||||
if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting,
|
||||
this, INFINITE, wait_flags)) {
|
||||
object_ = NULL;
|
||||
wait_object_ = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
// We need to know if the current message loop is going away so we can
|
||||
// prevent the wait thread from trying to access a dead message loop.
|
||||
MessageLoop::current()->AddDestructionObserver(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ObjectWatcher::StopWatching() {
|
||||
if (!wait_object_)
|
||||
return false;
|
||||
|
||||
// Make sure ObjectWatcher is used in a single-threaded fashion.
|
||||
assert(origin_loop_ == MessageLoop::current());
|
||||
|
||||
// Blocking call to cancel the wait. Any callbacks already in progress will
|
||||
// finish before we return from this call.
|
||||
if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
signal_weakflag_.Cancel();
|
||||
object_ = NULL;
|
||||
wait_object_ = NULL;
|
||||
|
||||
MessageLoop::current()->RemoveDestructionObserver(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
HANDLE ObjectWatcher::GetWatchedObject() {
|
||||
return object_;
|
||||
}
|
||||
|
||||
// static
|
||||
void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) {
|
||||
assert(!timed_out);
|
||||
|
||||
// The destructor blocks on any callbacks that are in flight, so we know that
|
||||
// that is always a pointer to a valid ObjectWater.
|
||||
ObjectWatcher* that = static_cast<ObjectWatcher*>(param);
|
||||
that->origin_loop_->PostTask(that->callback_);
|
||||
that->callback_ = nullptr;
|
||||
}
|
||||
|
||||
void ObjectWatcher::Signal(Delegate* delegate) {
|
||||
// Signaling the delegate may result in our destruction or a nested call to
|
||||
// StartWatching(). As a result, we save any state we need and clear previous
|
||||
// watcher state before signaling the delegate.
|
||||
HANDLE object = object_;
|
||||
StopWatching();
|
||||
delegate->OnObjectSignaled(object);
|
||||
}
|
||||
|
||||
void ObjectWatcher::PreDestroyCurrentMessageLoop() {
|
||||
// Need to shutdown the watch so that we don't try to access the MessageLoop
|
||||
// after this point.
|
||||
StopWatching();
|
||||
}
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
@@ -0,0 +1,97 @@
|
||||
#ifndef BASE_WIN_OBJECT_WATCHER_H_
|
||||
#define BASE_WIN_OBJECT_WATCHER_H_
|
||||
#pragma once
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include "base/base_config.h"
|
||||
#include "base/framework/message_loop.h"
|
||||
#include "base/callback/callback.h"
|
||||
|
||||
namespace nbase {
|
||||
namespace win32 {
|
||||
|
||||
// A class that provides a means to asynchronously wait for a Windows object to
|
||||
// become signaled. It is an abstraction around RegisterWaitForSingleObject
|
||||
// that provides a notification callback, OnObjectSignaled, that runs back on
|
||||
// the origin thread (i.e., the thread that called StartWatching).
|
||||
//
|
||||
// This class acts like a smart pointer such that when it goes out-of-scope,
|
||||
// UnregisterWaitEx is automatically called, and any in-flight notification is
|
||||
// suppressed.
|
||||
//
|
||||
// Typical usage:
|
||||
//
|
||||
// class MyClass : public base::ObjectWatcher::Delegate {
|
||||
// public:
|
||||
// void DoStuffWhenSignaled(HANDLE object) {
|
||||
// watcher_.StartWatching(object, this);
|
||||
// }
|
||||
// virtual void OnObjectSignaled(HANDLE object) {
|
||||
// // OK, time to do stuff!
|
||||
// }
|
||||
// private:
|
||||
// base::ObjectWatcher watcher_;
|
||||
// };
|
||||
//
|
||||
// In the above example, MyClass wants to "do stuff" when object becomes
|
||||
// signaled. ObjectWatcher makes this task easy. When MyClass goes out of
|
||||
// scope, the watcher_ will be destroyed, and there is no need to worry about
|
||||
// OnObjectSignaled being called on a deleted MyClass pointer. Easy!
|
||||
//
|
||||
class BASE_EXPORT ObjectWatcher : public nbase::SupportWeakCallback, public MessageLoop::DestructionObserver
|
||||
{
|
||||
public:
|
||||
class BASE_EXPORT Delegate {
|
||||
public:
|
||||
virtual ~Delegate() {}
|
||||
// Called from the MessageLoop when a signaled object is detected. To
|
||||
// continue watching the object, StartWatching must be called again.
|
||||
virtual void OnObjectSignaled(HANDLE object) = 0;
|
||||
};
|
||||
|
||||
ObjectWatcher();
|
||||
~ObjectWatcher();
|
||||
|
||||
// When the object is signaled, the given delegate is notified on the thread
|
||||
// where StartWatching is called. The ObjectWatcher is not responsible for
|
||||
// deleting the delegate.
|
||||
//
|
||||
// Returns true if the watch was started. Otherwise, false is returned.
|
||||
//
|
||||
bool StartWatching(HANDLE object, Delegate* delegate);
|
||||
|
||||
// Stops watching. Does nothing if the watch has already completed. If the
|
||||
// watch is still active, then it is canceled, and the associated delegate is
|
||||
// not notified.
|
||||
//
|
||||
// Returns true if the watch was canceled. Otherwise, false is returned.
|
||||
//
|
||||
bool StopWatching();
|
||||
|
||||
// Returns the handle of the object being watched, or NULL if the object
|
||||
// watcher is stopped.
|
||||
HANDLE GetWatchedObject();
|
||||
|
||||
private:
|
||||
// Called on a background thread when done waiting.
|
||||
static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out);
|
||||
|
||||
void Signal(Delegate* delegate);
|
||||
|
||||
// MessageLoop::DestructionObserver implementation:
|
||||
virtual void PreDestroyCurrentMessageLoop();
|
||||
|
||||
// Internal state.
|
||||
nbase::WeakCallbackFlag signal_weakflag_;
|
||||
StdClosure callback_;
|
||||
HANDLE object_; // The object being watched
|
||||
HANDLE wait_object_; // Returned by RegisterWaitForSingleObject
|
||||
MessageLoop* origin_loop_; // Used to get back to the origin thread
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ObjectWatcher);
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // BASE_OBJECT_WATCHER_H_
|
||||
@@ -0,0 +1,164 @@
|
||||
// Copyright (c) 2012, NetEase Inc. All rights reserved.
|
||||
//
|
||||
// wrt(guangguang)
|
||||
// 2012/5/31
|
||||
//
|
||||
// utilities for path operation
|
||||
|
||||
#include "base/win32/path_util.h"
|
||||
#if defined(OS_WIN)
|
||||
#include <shlobj.h> // SHCreateDirectory
|
||||
#include <shellapi.h> // SHFileOperation
|
||||
#include "base/file/file_util.h"
|
||||
//#include "base/log/log.h"
|
||||
#include "base/win32/win_util.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
std::wstring GetModulePathName(HMODULE module_handle)
|
||||
{
|
||||
//DCHECK(IsModuleHandleValid(module_handle));
|
||||
std::wstring mod_path;
|
||||
mod_path.resize(MAX_PATH);
|
||||
mod_path.resize(::GetModuleFileNameW(module_handle, &mod_path[0], (DWORD)mod_path.size()));
|
||||
return mod_path;
|
||||
}
|
||||
|
||||
std::wstring GetModuleDirectory(HMODULE module_handle)
|
||||
{
|
||||
//DCHECK(IsModuleHandleValid(module_handle));
|
||||
std::wstring module_directory;
|
||||
if (FilePathApartDirectory(GetModulePathName(module_handle), module_directory))
|
||||
return module_directory;
|
||||
return L"";
|
||||
}
|
||||
|
||||
std::wstring GetModuleName(HMODULE module_handle)
|
||||
{
|
||||
//DCHECK(IsModuleHandleValid(module_handle));
|
||||
std::wstring module_name;
|
||||
if (FilePathApartFileName(GetModulePathName(module_handle), module_name))
|
||||
return module_name;
|
||||
return L"";
|
||||
}
|
||||
|
||||
std::wstring GetCurrentModulePathName()
|
||||
{
|
||||
return GetModulePathName(GetCurrentModuleHandle());
|
||||
}
|
||||
|
||||
std::wstring GetCurrentModuleDirectory()
|
||||
{
|
||||
return GetModuleDirectory(GetCurrentModuleHandle());
|
||||
}
|
||||
|
||||
std::wstring GetCurrentModuleName()
|
||||
{
|
||||
return GetModuleName(GetCurrentModuleHandle());
|
||||
}
|
||||
|
||||
std::wstring GetWindowsDir()
|
||||
{
|
||||
std::wstring windows_path;
|
||||
windows_path.resize(MAX_PATH);
|
||||
DWORD result = ::GetWindowsDirectory(&windows_path[0], MAX_PATH);
|
||||
windows_path.resize(result);
|
||||
return windows_path;
|
||||
}
|
||||
|
||||
std::wstring GetSystemDir()
|
||||
{
|
||||
std::wstring system_path;
|
||||
system_path.resize(MAX_PATH);
|
||||
DWORD result = ::GetSystemDirectory(&system_path[0], MAX_PATH);
|
||||
system_path.resize(result);
|
||||
return system_path;
|
||||
}
|
||||
|
||||
std::wstring GetTempDir()
|
||||
{
|
||||
std::wstring temp_path;
|
||||
temp_path.resize(MAX_PATH);
|
||||
DWORD result = ::GetTempPath(MAX_PATH, &temp_path[0]);
|
||||
temp_path.resize(result);
|
||||
return temp_path;
|
||||
}
|
||||
|
||||
std::wstring GetLocalAppDataDir(HANDLE token /* = NULL */)
|
||||
{
|
||||
#if (NTDDI_VERSION < NTDDI_VISTA)
|
||||
#ifndef KF_FLAG_CREATE
|
||||
#define KF_FLAG_CREATE 0x00008000
|
||||
#endif
|
||||
#endif
|
||||
|
||||
std::wstring temp_path;
|
||||
if (IsRunningOnVistaOrHigher())
|
||||
{
|
||||
typedef HRESULT (WINAPI *__SHGetKnownFolderPath)(REFKNOWNFOLDERID, DWORD, HANDLE, PWSTR*);
|
||||
HMODULE moudle_handle = ::LoadLibraryW(L"shell32.dll");
|
||||
if (moudle_handle != NULL)
|
||||
{
|
||||
__SHGetKnownFolderPath _SHGetKnownFolderPath =
|
||||
reinterpret_cast<__SHGetKnownFolderPath>(::GetProcAddress(moudle_handle, "SHGetKnownFolderPath"));
|
||||
if (_SHGetKnownFolderPath != NULL)
|
||||
{
|
||||
PWSTR result = NULL;
|
||||
if (S_OK == _SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, token, &result))
|
||||
{
|
||||
temp_path = result;
|
||||
::CoTaskMemFree(result);
|
||||
}
|
||||
}
|
||||
::FreeLibrary(moudle_handle);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// On Windows XP, CSIDL_LOCAL_APPDATA represents "{user}\Local Settings\Application Data"
|
||||
// while CSIDL_APPDATA represents "{user}\Application Data"
|
||||
wchar_t buffer[MAX_PATH];
|
||||
if (S_OK == ::SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, token, SHGFP_TYPE_CURRENT, buffer))
|
||||
temp_path = buffer;
|
||||
}
|
||||
if (!temp_path.empty())
|
||||
if (temp_path.back() != L'\\')
|
||||
temp_path.push_back(L'\\');
|
||||
return temp_path;
|
||||
}
|
||||
|
||||
bool CreateDirectoryRecursively(const wchar_t *full_dir)
|
||||
{
|
||||
HRESULT result = ::SHCreateDirectory(NULL, full_dir);
|
||||
if (result == ERROR_SUCCESS || result == ERROR_FILE_EXISTS || result == ERROR_ALREADY_EXISTS)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DeleteDirectoryRecursively(const wchar_t *full_dir)
|
||||
{
|
||||
DWORD attributes = ::GetFileAttributesW(full_dir);
|
||||
if (attributes == INVALID_FILE_ATTRIBUTES) // not exists
|
||||
return true;
|
||||
if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) // not a file
|
||||
return false;
|
||||
SHFILEOPSTRUCTW file_op = {NULL,
|
||||
FO_DELETE,
|
||||
full_dir,
|
||||
L"",
|
||||
FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT,
|
||||
FALSE,
|
||||
0,
|
||||
L""};
|
||||
if (::SHFileOperation(&file_op) && !file_op.fAnyOperationsAborted)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
@@ -0,0 +1,36 @@
|
||||
// utilities for path operation
|
||||
|
||||
#ifndef BASE_WIN32_PATH_UTIL_H_
|
||||
#define BASE_WIN32_PATH_UTIL_H_
|
||||
|
||||
#include "base/base_config.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include <string>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
BASE_EXPORT std::wstring GetModulePathName(HMODULE module_handle);
|
||||
BASE_EXPORT std::wstring GetModuleDirectory(HMODULE module_handle);
|
||||
BASE_EXPORT std::wstring GetModuleName(HMODULE module_handle);
|
||||
BASE_EXPORT std::wstring GetCurrentModuleName();
|
||||
BASE_EXPORT std::wstring GetCurrentModulePathName();
|
||||
BASE_EXPORT std::wstring GetCurrentModuleDirectory();
|
||||
BASE_EXPORT std::wstring GetWindowsDir();
|
||||
BASE_EXPORT std::wstring GetSystemDir();
|
||||
BASE_EXPORT std::wstring GetTempDir();
|
||||
BASE_EXPORT std::wstring GetLocalAppDataDir(HANDLE token = NULL);
|
||||
|
||||
BASE_EXPORT bool CreateDirectoryRecursively(const wchar_t *full_dir);
|
||||
BASE_EXPORT bool DeleteDirectoryRecursively(const wchar_t *full_dir);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // OS_WIN
|
||||
#endif // BASE_WIN32_PATH_UTIL_H_
|
||||
@@ -0,0 +1,92 @@
|
||||
// Copyright (c) 2013, NetEase Inc. All rights reserved.
|
||||
//
|
||||
// wrt(guangguang)
|
||||
// 2013/8/29
|
||||
//
|
||||
// Multi-byte strings and Unicode strings conversion, etc.
|
||||
|
||||
#include "base/win32/win_util.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
bool MBCSToUnicode(const char *input, std::wstring& output, int code_page)
|
||||
{
|
||||
output.clear();
|
||||
int length = ::MultiByteToWideChar(code_page, 0, input, -1, NULL, 0);
|
||||
if (length <= 0)
|
||||
return false;
|
||||
output.resize(length-1);
|
||||
if (output.empty())
|
||||
return true;
|
||||
::MultiByteToWideChar(code_page,
|
||||
0,
|
||||
input,
|
||||
-1,
|
||||
&output[0],
|
||||
static_cast<int>(output.size()));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MBCSToUnicode(const std::string &input, std::wstring& output, int code_page)
|
||||
{
|
||||
output.clear();
|
||||
int length = ::MultiByteToWideChar(code_page, 0, input.c_str(), static_cast<int>(input.size()), NULL, 0);
|
||||
output.resize(length);
|
||||
if (output.empty())
|
||||
return true;
|
||||
::MultiByteToWideChar(code_page,
|
||||
0,
|
||||
input.c_str(),
|
||||
static_cast<int>(input.size()),
|
||||
&output[0],
|
||||
static_cast<int>(output.size()));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UnicodeToMBCS(const wchar_t *input, std::string &output, int code_page)
|
||||
{
|
||||
output.clear();
|
||||
int length = ::WideCharToMultiByte(code_page, 0, input, -1, NULL, 0, NULL, NULL);
|
||||
if (length <= 0)
|
||||
return false;
|
||||
output.resize(length-1);
|
||||
if (output.empty())
|
||||
return true;
|
||||
::WideCharToMultiByte(code_page,
|
||||
0,
|
||||
input,
|
||||
length-1,
|
||||
&output[0],
|
||||
static_cast<int>(output.size()),
|
||||
NULL,
|
||||
NULL);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UnicodeToMBCS(const std::wstring& input, std::string &output, int code_page)
|
||||
{
|
||||
output.clear();
|
||||
int length = ::WideCharToMultiByte(code_page, 0, input.c_str(), static_cast<int>(input.size()), NULL, 0, NULL, NULL);
|
||||
output.resize(length);
|
||||
if (output.empty())
|
||||
return true;
|
||||
::WideCharToMultiByte(code_page,
|
||||
0,
|
||||
input.c_str(),
|
||||
static_cast<int>(input.size()),
|
||||
&output[0],
|
||||
static_cast<int>(output.size()),
|
||||
NULL,
|
||||
NULL);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
@@ -0,0 +1,28 @@
|
||||
// Multi-byte strings and Unicode strings conversion, etc.
|
||||
|
||||
#ifndef BASE_PLATFORM_STRING_UTIL_H_
|
||||
#define BASE_PLATFORM_STRING_UTIL_H_
|
||||
|
||||
#include "base/base_config.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include <string>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
// the following functions are used to convert encodings between MBCS and Unicode
|
||||
BASE_EXPORT bool MBCSToUnicode(const char *input, std::wstring& output, int code_page = CP_ACP);
|
||||
BASE_EXPORT bool MBCSToUnicode(const std::string &input, std::wstring& output, int code_page = CP_ACP);
|
||||
BASE_EXPORT bool UnicodeToMBCS(const wchar_t *input, std::string &output, int code_page = CP_ACP);
|
||||
BASE_EXPORT bool UnicodeToMBCS(const std::wstring& input, std::string &output, int code_page = CP_ACP);
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
#endif // BASE_PLATFORM_STRING_UTIL_H_
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef BASE_WIN32_SCOPED_WIN_HANDLE_H_
|
||||
#define BASE_WIN32_SCOPED_WIN_HANDLE_H_
|
||||
|
||||
#include "base/base_config.h"
|
||||
#if defined(OS_WIN)
|
||||
|
||||
#include <windows.h>
|
||||
#include "base/base_export.h"
|
||||
#include "base/base_types.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
class BASE_EXPORT ScopedWinHandle
|
||||
{
|
||||
public:
|
||||
|
||||
ScopedWinHandle() : handle_(INVALID_HANDLE_VALUE) {}
|
||||
ScopedWinHandle(HANDLE handle) : handle_(handle) {}
|
||||
~ScopedWinHandle() { Reset(INVALID_HANDLE_VALUE); }
|
||||
|
||||
bool Valid() const { return handle_ != INVALID_HANDLE_VALUE; }
|
||||
HANDLE Get() const { return handle_; }
|
||||
HANDLE Release() { HANDLE old_handle = handle_; handle_ = INVALID_HANDLE_VALUE; return old_handle; }
|
||||
void Reset(HANDLE handle) { if (Valid()) ::CloseHandle(handle_); handle_ = handle; }
|
||||
HANDLE* operator&() { return &handle_; }
|
||||
operator HANDLE() const { return handle_; }
|
||||
|
||||
private:
|
||||
HANDLE handle_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ScopedWinHandle);
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
#endif // BASE_WIN32_SCOPED_WIN_HANDLE_H_
|
||||
@@ -0,0 +1,168 @@
|
||||
// Copyright (c) 2012, NetEase Inc. All rights reserved.
|
||||
//
|
||||
// wrt(guangguang)
|
||||
// 2012/5/31
|
||||
//
|
||||
// utilities for security associated operations
|
||||
|
||||
#include "base/win32/security_util.h"
|
||||
#if defined(OS_WIN)
|
||||
#include <aclapi.h> // ConvertStringSecurityDescriptorToSecurityDescriptorW
|
||||
#include <sddl.h> // SetSecurityInfo
|
||||
#include "base/win32/win_util.h"
|
||||
#include "base/win32/scoped_win_handle.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
static const wchar_t kLowIntegeritySddlSaclW[] = L"S:(ML;;NW;;;LW)";
|
||||
|
||||
bool SetObjectToLowIntegrity(HANDLE hObject, SE_OBJECT_TYPE type /*= SE_KERNEL_OBJECT*/)
|
||||
{
|
||||
if(!IsRunningOnVistaOrHigher())
|
||||
return true;
|
||||
|
||||
bool bRet = false;
|
||||
DWORD dwErr = ERROR_SUCCESS;
|
||||
PSECURITY_DESCRIPTOR pSD = NULL;
|
||||
PACL pSacl = NULL;
|
||||
BOOL fSaclPresent = FALSE;
|
||||
BOOL fSaclDefaulted = FALSE;
|
||||
|
||||
if (::ConvertStringSecurityDescriptorToSecurityDescriptorW(kLowIntegeritySddlSaclW, SDDL_REVISION_1, &pSD, NULL))
|
||||
{
|
||||
if (::GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted))
|
||||
{
|
||||
dwErr = SetSecurityInfo(hObject, type, LABEL_SECURITY_INFORMATION, NULL, NULL, NULL, pSacl);
|
||||
bRet = (ERROR_SUCCESS == dwErr);
|
||||
}
|
||||
::LocalFree(pSD);
|
||||
}
|
||||
return bRet;
|
||||
}
|
||||
|
||||
bool IsUserAdmin()
|
||||
{
|
||||
SID_IDENTIFIER_AUTHORITY nt_authority = SECURITY_NT_AUTHORITY;
|
||||
PSID administrators_group = NULL;
|
||||
BOOL result = ::AllocateAndInitializeSid(&nt_authority,
|
||||
2,
|
||||
SECURITY_BUILTIN_DOMAIN_RID,
|
||||
DOMAIN_ALIAS_RID_ADMINS,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
&administrators_group);
|
||||
if (result)
|
||||
{
|
||||
if (!::CheckTokenMembership(NULL, administrators_group, &result))
|
||||
result = false;
|
||||
::FreeSid(administrators_group);
|
||||
}
|
||||
return !!result;
|
||||
}
|
||||
|
||||
bool IsUserNonElevatedAdmin()
|
||||
{
|
||||
// If pre-Vista return false;
|
||||
if (!IsRunningOnVistaOrHigher())
|
||||
return false;
|
||||
|
||||
bool non_elevated_admin = false;
|
||||
ScopedWinHandle token;
|
||||
if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_READ, &token))
|
||||
{
|
||||
TOKEN_ELEVATION_TYPE elevation_type = TokenElevationTypeDefault;
|
||||
DWORD infoLen = 0;
|
||||
if (::GetTokenInformation(token.Get(),
|
||||
TokenElevationType,
|
||||
reinterpret_cast<void*>(&elevation_type),
|
||||
sizeof(elevation_type),
|
||||
&infoLen))
|
||||
{
|
||||
if (elevation_type == TokenElevationTypeLimited)
|
||||
non_elevated_admin = true;
|
||||
}
|
||||
}
|
||||
|
||||
return non_elevated_admin;
|
||||
}
|
||||
|
||||
HRESULT GetSidIntegrityLevel(PSID sid, MANDATORY_LEVEL* level)
|
||||
{
|
||||
static SID_IDENTIFIER_AUTHORITY mandatory_label_auth =
|
||||
SECURITY_MANDATORY_LABEL_AUTHORITY;
|
||||
|
||||
if (!IsValidSid(sid))
|
||||
return E_FAIL;
|
||||
|
||||
SID_IDENTIFIER_AUTHORITY* authority = ::GetSidIdentifierAuthority(sid);
|
||||
if (!authority)
|
||||
return E_FAIL;
|
||||
|
||||
if (memcmp(authority, &mandatory_label_auth, sizeof(SID_IDENTIFIER_AUTHORITY)))
|
||||
return E_FAIL;
|
||||
|
||||
PUCHAR count = ::GetSidSubAuthorityCount(sid);
|
||||
if (!count || *count != 1)
|
||||
return E_FAIL;
|
||||
|
||||
DWORD* rid = ::GetSidSubAuthority(sid, 0);
|
||||
if (!rid)
|
||||
return E_FAIL;
|
||||
|
||||
if ((*rid & 0xFFF) != 0 || *rid > SECURITY_MANDATORY_PROTECTED_PROCESS_RID)
|
||||
return E_FAIL;
|
||||
|
||||
*level = static_cast<MANDATORY_LEVEL>(*rid >> 12);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT GetProcessIntegrityLevel(DWORD process_id, MANDATORY_LEVEL* level)
|
||||
{
|
||||
if (!IsRunningOnVistaOrHigher())
|
||||
return E_NOTIMPL;
|
||||
|
||||
if (process_id == 0)
|
||||
process_id = ::GetCurrentProcessId();
|
||||
|
||||
HRESULT result = E_FAIL;
|
||||
HANDLE process = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process_id);
|
||||
if (process != NULL)
|
||||
{
|
||||
HANDLE current_token;
|
||||
if (::OpenProcessToken(process,
|
||||
TOKEN_QUERY | TOKEN_QUERY_SOURCE,
|
||||
¤t_token))
|
||||
{
|
||||
DWORD label_size = 0;
|
||||
TOKEN_MANDATORY_LABEL* label;
|
||||
::GetTokenInformation(current_token,
|
||||
TokenIntegrityLevel,
|
||||
NULL,
|
||||
0,
|
||||
&label_size);
|
||||
if (label_size && (label = reinterpret_cast<TOKEN_MANDATORY_LABEL*>
|
||||
(::LocalAlloc(LPTR, label_size))) != NULL)
|
||||
{
|
||||
if (::GetTokenInformation(current_token,
|
||||
TokenIntegrityLevel,
|
||||
label,
|
||||
label_size,
|
||||
&label_size))
|
||||
{
|
||||
result = GetSidIntegrityLevel(label->Label.Sid, level);
|
||||
}
|
||||
::LocalFree(label);
|
||||
}
|
||||
::CloseHandle(current_token);
|
||||
}
|
||||
::CloseHandle(process);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
@@ -0,0 +1,44 @@
|
||||
// utilities for security associated operations
|
||||
|
||||
#ifndef BASE_WIN32_SECURITY_UTIL_H_
|
||||
#define BASE_WIN32_SECURITY_UTIL_H_
|
||||
|
||||
#include "base/base_config.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include <accctrl.h>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
BASE_EXPORT bool SetObjectToLowIntegrity(HANDLE hObject, SE_OBJECT_TYPE type = SE_KERNEL_OBJECT);
|
||||
|
||||
// Determine if the user is part of the adminstators group. This will return
|
||||
// true in case of XP and 2K if the user belongs to admin group. In case of
|
||||
// Vista, it only returns true if the admin is running elevated.
|
||||
BASE_EXPORT bool IsUserAdmin();
|
||||
|
||||
// Returns true if the user is running as a non-elevated admin in case of
|
||||
// Vista. In case of XP always returns false.
|
||||
BASE_EXPORT bool IsUserNonElevatedAdmin();
|
||||
|
||||
// Determine the mandatory level of a SID
|
||||
HRESULT GetSidIntegrityLevel(PSID sid, MANDATORY_LEVEL* level);
|
||||
|
||||
// Determine the mandatory level of a process
|
||||
// processID, the process to query, or (0) to use the current process
|
||||
// On Vista, level should alwys be filled in with either
|
||||
// MandatoryLevelLow (IE)
|
||||
// MandatoryLevelMedium(user), or
|
||||
// MandatoryLevelHigh( Elevated Admin)
|
||||
// On error, level remains unchanged
|
||||
BASE_EXPORT HRESULT GetProcessIntegrityLevel(DWORD processID, MANDATORY_LEVEL* level);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // OS_WIN
|
||||
#endif // BASE_WIN32_SECURITY_UTIL_H_
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright (c) 2011, NetEase Inc. All rights reserved.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Author: wrt(guangguang)
|
||||
// Date: 2011/7/4
|
||||
//
|
||||
// A wrapper class of Windows shared memory
|
||||
|
||||
#include "base/win32/shared_memory.h"
|
||||
#if defined(OS_WIN)
|
||||
#include "base/win32/security_util.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
bool SharedMemory::Create(const wchar_t *name, int size)
|
||||
{
|
||||
assert(mapping_ == NULL);
|
||||
mapping_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
|
||||
0, size, name);
|
||||
if(mapping_)
|
||||
{
|
||||
win32::SetObjectToLowIntegrity(mapping_);
|
||||
}
|
||||
return mapping_ != NULL;
|
||||
}
|
||||
|
||||
|
||||
bool SharedMemory::Open(const wchar_t *name)
|
||||
{
|
||||
assert(mapping_ == NULL);
|
||||
mapping_ = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, name);
|
||||
return mapping_ != NULL;
|
||||
}
|
||||
|
||||
|
||||
void SharedMemory::Close()
|
||||
{
|
||||
if (mapping_ != NULL)
|
||||
{
|
||||
CloseHandle(mapping_);
|
||||
mapping_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool SharedMemory::MappedView::OpenView(SharedMemory *shared_memory, bool read_write)
|
||||
{
|
||||
assert(!view_);
|
||||
DWORD access = FILE_MAP_READ;
|
||||
if (read_write) access |= FILE_MAP_WRITE;
|
||||
view_ = reinterpret_cast<unsigned char*>(::MapViewOfFile(shared_memory->mapping_, access, 0, 0, 0));
|
||||
return view_ != NULL;
|
||||
}
|
||||
|
||||
|
||||
void SharedMemory::MappedView::CloseView()
|
||||
{
|
||||
if (view_)
|
||||
{
|
||||
::UnmapViewOfFile(view_);
|
||||
view_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
@@ -0,0 +1,62 @@
|
||||
// A wrapper class of Windows shared memory
|
||||
|
||||
#ifndef BASE_SHARED_MEMORY_H_
|
||||
#define BASE_SHARED_MEMORY_H_
|
||||
|
||||
#include "base/base_config.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include <assert.h>
|
||||
#include <windows.h>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
class BASE_EXPORT SharedMemory
|
||||
{
|
||||
public:
|
||||
SharedMemory() : mapping_(NULL) {}
|
||||
~SharedMemory() { Close(); };
|
||||
bool Create(const wchar_t *name, int size);
|
||||
bool Open(const wchar_t *name);
|
||||
void Close();
|
||||
|
||||
class MappedView
|
||||
{
|
||||
public:
|
||||
MappedView() : view_(NULL) {}
|
||||
~MappedView() { CloseView(); }
|
||||
bool OpenView(SharedMemory *shared_memory, bool read_write);
|
||||
void CloseView();
|
||||
unsigned char *view() { return view_; }
|
||||
|
||||
private:
|
||||
unsigned char *view_;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class MappedViewOf : public MappedView
|
||||
{
|
||||
public:
|
||||
T *get() { return reinterpret_cast<T*>(view()); }
|
||||
T* operator->()
|
||||
{
|
||||
assert(view() != NULL);
|
||||
return reinterpret_cast<T*>(view());
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
friend class MappedView;
|
||||
HANDLE mapping_;
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
#endif // BASE_SHARED_MEMORY_H_
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Author wrt(guangguang)
|
||||
* Date 2011-06-14
|
||||
* Copyright Hangzhou, Netease Inc.
|
||||
* Brief Utilities for Windows applications
|
||||
*/
|
||||
|
||||
#include "base/win32/win_util.h"
|
||||
#if defined(OS_WIN)
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
HMODULE GetModuleHandleFromAddress(void* address)
|
||||
{
|
||||
MEMORY_BASIC_INFORMATION mbi = {0};
|
||||
DWORD result = ::VirtualQuery(address, &mbi, sizeof(mbi));
|
||||
assert(result == sizeof(mbi));
|
||||
return static_cast<HMODULE>(mbi.AllocationBase);
|
||||
}
|
||||
|
||||
HMODULE GetCurrentModuleHandle()
|
||||
{
|
||||
return GetModuleHandleFromAddress(GetCurrentModuleHandle);
|
||||
}
|
||||
|
||||
bool IsAddressInCurrentModule(void* address)
|
||||
{
|
||||
return GetCurrentModuleHandle() == GetModuleHandleFromAddress(address);
|
||||
}
|
||||
|
||||
std::wstring GetHostName()
|
||||
{
|
||||
std::wstring host_name;
|
||||
DWORD name_len = MAX_COMPUTERNAME_LENGTH + 1;
|
||||
host_name.resize(name_len);
|
||||
bool result = !!::GetComputerName(&host_name[0], &name_len);
|
||||
assert(result);
|
||||
host_name.resize(name_len);
|
||||
return host_name;
|
||||
}
|
||||
|
||||
bool IsModuleHandleValid(HMODULE module_handle)
|
||||
{
|
||||
if (!module_handle)
|
||||
return true;
|
||||
return module_handle == GetModuleHandleFromAddress(module_handle);
|
||||
}
|
||||
|
||||
bool RunApp(const wchar_t *application, HANDLE *process)
|
||||
{
|
||||
return RunAppWithRedirection(application, NULL, NULL, NULL, NULL, process);
|
||||
}
|
||||
|
||||
bool RunAppWithCommand(const wchar_t *application, const wchar_t *command, HANDLE *process)
|
||||
{
|
||||
return RunAppWithRedirection(application, command, NULL, NULL, NULL, process);
|
||||
}
|
||||
|
||||
bool RunAppWithRedirection(const wchar_t *application,
|
||||
const wchar_t *command,
|
||||
HANDLE input,
|
||||
HANDLE output,
|
||||
HANDLE error,
|
||||
HANDLE *process)
|
||||
{
|
||||
PROCESS_INFORMATION pi;
|
||||
STARTUPINFOW si;
|
||||
|
||||
memset(&si, 0, sizeof(si));
|
||||
|
||||
if (!!input || !!output || !!error)
|
||||
si.dwFlags = STARTF_USESTDHANDLES;
|
||||
|
||||
si.cb = sizeof(si);
|
||||
si.hStdInput = input ? input : ::GetStdHandle(STD_INPUT_HANDLE);
|
||||
si.hStdOutput = output ? output : ::GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
si.hStdError = error ? error : ::GetStdHandle(STD_ERROR_HANDLE);
|
||||
|
||||
wchar_t *command_dup = wcsdup(command);
|
||||
|
||||
if (::CreateProcessW(application,
|
||||
command_dup,
|
||||
NULL,
|
||||
NULL,
|
||||
(si.dwFlags & STARTF_USESTDHANDLES) ? TRUE : FALSE,
|
||||
CREATE_NO_WINDOW|CREATE_SUSPENDED,
|
||||
NULL,
|
||||
NULL,
|
||||
&si,
|
||||
&pi))
|
||||
{
|
||||
::CloseHandle(pi.hThread);
|
||||
if (process == NULL)
|
||||
::CloseHandle(pi.hProcess);
|
||||
else
|
||||
*process = pi.hProcess;
|
||||
free(command_dup);
|
||||
return true;
|
||||
}
|
||||
|
||||
free(command_dup);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MinimizeProcessWorkingSize()
|
||||
{
|
||||
OSVERSIONINFOW osvi;
|
||||
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
||||
|
||||
::GetVersionExW(&osvi);
|
||||
if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
|
||||
{
|
||||
::SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T)-1, (SIZE_T)-1);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SingletonRun(const wchar_t *application)
|
||||
{
|
||||
assert(application);
|
||||
if (application == NULL)
|
||||
return false;
|
||||
|
||||
HANDLE hMutex = ::CreateMutexW(NULL, TRUE, application);
|
||||
|
||||
if (hMutex == NULL)
|
||||
return false;
|
||||
|
||||
if (::GetLastError() == ERROR_ALREADY_EXISTS)
|
||||
{
|
||||
::CloseHandle(hMutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 从此这个互斥信号量就被不受控地打开了,直到进程退出 */
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsRunningOnVistaOrHigher()
|
||||
{
|
||||
OSVERSIONINFO os_version = {0};
|
||||
os_version.dwOSVersionInfoSize = sizeof(os_version);
|
||||
GetVersionEx(&os_version);
|
||||
return os_version.dwMajorVersion >= 6;
|
||||
}
|
||||
|
||||
bool OpenResource(const wchar_t *resource,
|
||||
const wchar_t *type,
|
||||
void *&data,
|
||||
unsigned long &size,
|
||||
HMODULE module)
|
||||
{
|
||||
HRSRC resource_handle = ::FindResourceW(module,
|
||||
resource,
|
||||
type);
|
||||
if (resource_handle == NULL)
|
||||
return false;
|
||||
HGLOBAL data_handle = ::LoadResource(module, resource_handle);
|
||||
if (data_handle == NULL)
|
||||
return false;
|
||||
size = ::SizeofResource(module, resource_handle);
|
||||
if (size == 0)
|
||||
return false;
|
||||
data = ::LockResource(data_handle);
|
||||
if (data == NULL)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef BASE_WIN32_WIN_UTIL_H_
|
||||
#define BASE_WIN32_WIN_UTIL_H_
|
||||
|
||||
#include "base/base_config.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
|
||||
#include "base/base_export.h"
|
||||
#include <string>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
BASE_EXPORT bool IsAddressInCurrentModule(void* address);
|
||||
BASE_EXPORT bool IsModuleHandleValid(HMODULE module_handle);
|
||||
BASE_EXPORT HMODULE GetModuleHandleFromAddress(void* address);
|
||||
BASE_EXPORT HMODULE GetCurrentModuleHandle();
|
||||
|
||||
BASE_EXPORT std::wstring GetHostName();
|
||||
|
||||
BASE_EXPORT bool RunApp(const wchar_t *application, HANDLE *process = NULL);
|
||||
BASE_EXPORT bool RunAppWithCommand(const wchar_t *application, const wchar_t *command, HANDLE *process = NULL);
|
||||
BASE_EXPORT bool RunAppWithRedirection(const wchar_t *application,
|
||||
const wchar_t *command,
|
||||
HANDLE input,
|
||||
HANDLE output,
|
||||
HANDLE error,
|
||||
HANDLE *process = NULL);
|
||||
|
||||
BASE_EXPORT bool MinimizeProcessWorkingSize();
|
||||
BASE_EXPORT bool SingletonRun(const wchar_t *application);
|
||||
|
||||
BASE_EXPORT bool IsRunningOnVistaOrHigher();
|
||||
|
||||
BASE_EXPORT bool OpenResource(const wchar_t *resource,
|
||||
const wchar_t *type,
|
||||
void *&data,
|
||||
unsigned long &size,
|
||||
HMODULE module = NULL);
|
||||
|
||||
} // namespace win32
|
||||
} // namespace nbase
|
||||
|
||||
#endif // OS_WIN
|
||||
#endif // BASE_WIN32_WIN_UTIL_H_
|
||||
Reference in new issue
Block a user