⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wtl_app.cpp

📁 j2me is based on j2mepolish, client & server for mobile application.
💻 CPP
字号:

//         Copyright E骾n O'Callaghan 2007 - 2008.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)


#include <boost/array.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>

#define AUX_APP_IMPL_UNIT
#include "wtl_app.hpp"
#include "string_conv.hpp"

namespace aux
{

static boost::shared_ptr<app_impl> s_app_impl;

class app_impl
{
public:
	app_impl() :
		hmod_(NULL),
		instance_(_Module.GetModuleInstance()),
		initial_path_(boost::filesystem::initial_path<boost::filesystem::wpath>())
	{
		LPWSTR *szArglist; int nArgs;		
		szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
		
		if (NULL == szArglist)
		{
		}
		else
		{
			exe_string_  = szArglist[0];
			exe_path_ = boost::filesystem::wpath(exe_string_);
			
			for (int i=1; i<nArgs; ++i) 
				command_args_.push_back(szArglist[i]);
				
			working_directory_ = exe_path_.branch_path();

			TCHAR szPath[MAX_PATH];
			if(SUCCEEDED(SHGetFolderPath(NULL, 
                 CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, 
                 NULL, 
                 0, 
                 szPath)))
			{
				non_roaming_directory_ = boost::filesystem::wpath(szPath);
			}	

		}		
		LocalFree(szArglist);	
	}

	const boost::filesystem::wpath set_working_directory(const boost::filesystem::wpath& wd)
	{
		boost::filesystem::wpath ret = working_directory_;
		working_directory_ = wd;

		return ret;
	}

	void set_app_dir(std::wstring app_dir)
	{
		non_roaming_directory_ /= app_dir;
	}

	friend class app_module;

private:
	HMODULE hmod_;
	HINSTANCE instance_;
	std::wstring exe_string_;
	std::wstring res_dll_;
	std::wstring app_name_;
	
	boost::filesystem::wpath exe_path_;
	boost::filesystem::wpath initial_path_;
	boost::filesystem::wpath working_directory_;
	boost::filesystem::wpath non_roaming_directory_;
	
	std::vector<std::wstring> command_args_;	
};

app_module::app_module()
{	
	init();
}

void app_module::init()
{
	if (!s_app_impl)
		s_app_impl.reset(new app_impl());

	pimpl = s_app_impl;
}

const std::wstring& app_module::exe_string() const 
{ 
	return pimpl->exe_string_; 
}

const boost::filesystem::wpath& app_module::exe_path() const 
{ 
	return pimpl->exe_path_; 
}

const boost::filesystem::wpath& app_module::initial_path() const 
{ 
	return pimpl->initial_path_; 
}

const boost::filesystem::wpath app_module::set_working_directory(const boost::filesystem::wpath& wd)
{
	return pimpl->set_working_directory(wd);
}

const boost::filesystem::wpath& app_module::working_directory() const 
{ 
	return pimpl->working_directory_; 
}

void app_module::set_app_dir(std::wstring app_dir)
{
	pimpl->set_app_dir(app_dir);
}

const boost::filesystem::wpath& app_module::non_roaming_directory() const 
{ 
	if (!boost::filesystem::exists(pimpl->non_roaming_directory_))
		boost::filesystem::create_directory(pimpl->non_roaming_directory_);

	return pimpl->non_roaming_directory_; 
}
	
const std::vector<std::wstring>& app_module::command_args() const 
{ 
	return pimpl->command_args_; 
}
	
void app_module::res_revert()
{
	if (pimpl->hmod_) FreeLibrary(pimpl->hmod_);
	_Module.SetResourceInstance(pimpl->instance_);
}

void app_module::res_set_dll(std::wstring dll)
{
	if (pimpl->hmod_) FreeLibrary(pimpl->hmod_);
	pimpl->res_dll_ = dll;
	
	HMODULE hmod_ = ::LoadLibraryEx(dll.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
	_Module.SetResourceInstance(reinterpret_cast<HINSTANCE>(hmod_));
}

std::wstring app_module::res_wstr(unsigned uID)
{
	// The upper size limit ain't nice, but at least it's safe from buffer overflow
	win_c_str<std::wstring> str(2048);
	
	int size = ::LoadString(_Module.GetResourceInstance(), uID, str, str.size());
	assert(size != 0);
	
	return str;
}

std::pair<void*,size_t> app_module::res_find_lock(unsigned name, unsigned type)
{
	HRSRC rsrc = FindResource(_Module.GetResourceInstance(), (LPCTSTR)name, (LPCTSTR)type);
	assert(rsrc);
	
	HGLOBAL global = LoadResource(_Module.GetResourceInstance(), rsrc);
	assert(global);
	
	void* ptr = LockResource(global);
	assert(ptr);
	
	return std::pair<void*,size_t>(ptr, SizeofResource(_Module.GetResourceInstance(), rsrc));
}

} // namespace aux

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -