StartAppContainer.zip/TestAppContainer/TestAppContainer.cpp
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/shared_ptr.hpp>
#include <Windows.h>
#include <sddl.h>
#include <Userenv.h>
#include <Shlobj.h>
#pragma comment(lib, "Userenv.lib")
void MyLocalFree(PSID sid) {
if (sid != NULL) {
::LocalFree(sid);
}
}
typedef boost::shared_ptr<boost::remove_pointer<PSID>::type> SHARED_SID;
SHARED_SID ToSharedSID(PSID sid) {
return SHARED_SID(sid, &MyLocalFree);
}
void MyFreeSid(PSID sid) {
if (sid != NULL) {
::FreeSid(sid);
}
}
SHARED_SID ToSharedSID2(PSID sid) {
return SHARED_SID(sid, &MyFreeSid);
}
void MyCoTaskMemFree(void * const ptr) {
if (ptr != NULL) {
::CoTaskMemFree(ptr);
}
}
std::wstring getEnv(const wchar_t * const name) {
wchar_t *str;
size_t len;
if (::_wdupenv_s(&str, &len, name) != 0 || str == NULL) {
return std::wstring();
}
const std::wstring result(str);
::free(str);
return result;
}
SHARED_SID GetAppContainerSid(const wchar_t * const name) {
PSID sidImpl;
if (FAILED(::DeriveAppContainerSidFromAppContainerName(name, &sidImpl))) {
return SHARED_SID();
}
return ToSharedSID2(sidImpl);
}
bool GetAppContainerFolderPath(SHARED_SID sid, std::wstring &result) {
wchar_t *sidStrImpl;
if (::ConvertSidToStringSidW(sid.get(), &sidStrImpl) == FALSE) {
return false;
}
const boost::shared_ptr<wchar_t> sidStr(sidStrImpl, &MyLocalFree);
wchar_t *appContainerPathImpl;
if (FAILED(::GetAppContainerFolderPath(sidStr.get(), &appContainerPathImpl))) {
return false;
}
const boost::shared_ptr<wchar_t> appContainerPath(appContainerPathImpl, &MyCoTaskMemFree);
result.assign(appContainerPath.get());
return true;
}
bool GetMusicFolderPath(std::wstring &result) {
wchar_t *pathImpl;
if (FAILED(::SHGetKnownFolderPath(FOLDERID_MusicLibrary, KF_FLAG_DEFAULT_PATH, NULL, &pathImpl))) {
std::wcout << L"SHGetKnownFolderPath error: " << GetLastError() << std::endl;
return false;
}
const boost::shared_ptr<wchar_t> path(pathImpl, &MyCoTaskMemFree);
result.assign(path.get());
return true;
}
void printDir(const boost::filesystem::path &dir) {
try {
BOOST_FOREACH(const boost::filesystem::path &path, std::make_pair(boost::filesystem::directory_iterator(dir), boost::filesystem::directory_iterator())) {
std::cout << path << std::endl;
}
} catch (const boost::filesystem::filesystem_error) {
std::wcout << L"取得に失敗しました" << std::endl;
}
}
int main(const unsigned int argc, const char * const * const argv) {
std::locale::global(std::locale("japanese"));
typedef std::pair<std::wstring, boost::filesystem::path> ITEM;
std::vector<ITEM> list;
list.push_back(std::make_pair(L"SystemRoot", boost::filesystem::path(getEnv(L"SystemRoot"))));
list.push_back(std::make_pair(L"ProgramFiles", boost::filesystem::path(getEnv(L"ProgramFiles"))));
std::wstring applicationFolder;
GetAppContainerFolderPath(GetAppContainerSid(L"AppConteinerTest"), applicationFolder);
list.push_back(std::make_pair(L"アプリケーションフォルダ", boost::filesystem::path(applicationFolder)));
list.push_back(std::make_pair(L"Tempフォルダ", boost::filesystem::path(getEnv(L"TEMP"))));
std::wstring musicFolder;
GetMusicFolderPath(musicFolder);
list.push_back(std::make_pair(L"ミュージックフォルダ", boost::filesystem::path(musicFolder)));
list.push_back(std::make_pair(L"システムドライブ直下", boost::filesystem::path(getEnv(L"SystemDrive") + L"\\")));
list.push_back(std::make_pair(L"ユーザーフォルダ", boost::filesystem::path(getEnv(L"USERPROFILE"))));
BOOST_FOREACH(const ITEM & item, list) {
std::wcout << boost::wformat(L"%1%(%2%)を取得します。\nEnter押下で実行") % item.first % item.second;
std::cin.ignore();
printDir(item.second);
std::wcout << std::endl;
}
std::wcout << L"すべての処理が終了しました" << std::endl;
std::wcout << L"Enter押下で終了します" << std::endl;
std::cin.ignore();
return 0;
}