Example #1
0
ALMRunConfig::ALMRunConfig()
{
	//初始化默认参数
	config[OrderByPre] = false;
	config[NumberKey] = false;
	config[ExecuteIfOnlyOne] = false;
	config[ShowTip] = false;
	config[AutoRun] = false;
	config[AddToSendTo] = false;
	config[PlayPopupNotify] = false;
	config[SpaceKey] = false;
	config[DoubleToggleFunc] = false;
	config[DoubleClick] = false;
	config[DuplicateCMD] = false;
	cfg_changed = false;
	FavoriteList = NULL;
	order_conf = NULL;
	conf = NULL;
	order_cfg_time = 0;
	cfg_time = 0;

	if (wxGetEnv(wxT("ALMRUN_HOME"),&Home))
	{	
		cfg_file = Home + wxT("config/ALMRun.ini");
		order_cfg_file = Home + wxT("config/Order.ini");
		if (wxFileExists(order_cfg_file))
			order_cfg_time =  wxFileModificationTime(order_cfg_file);
		order_conf = new wxFileConfig(wxT("ALMRun"),wxEmptyString,order_cfg_file,wxEmptyString,wxCONFIG_USE_LOCAL_FILE);
		order_conf->SetExpandEnvVars(false);
		if (!wxFileExists(cfg_file))
			MoveFile(Home + wxGetApp().GetAppName().Append(".ini"),cfg_file);
	}
	if (wxFileExists(cfg_file) == false)
		cfg_file = wxEmptyString;
	else
		cfg_time = wxFileModificationTime(cfg_file);

	//lastId = 0;
	conf = new wxFileConfig(wxT("ALMRun"),wxEmptyString,cfg_file,wxEmptyString,7);
	gui_config[ListFont] = conf->Read("/GUI/ListFont");
	MerryListBoxPanel* listBoxPanel = ::wxGetApp().GetFrame().GetListBoxPanel();
	listBoxPanel->SetFont(gui_config[ListFont]);
	conf->SetPath("/Config");
	conf->SetRecordDefaults(false);
	config_u[CONFIG_VER] = conf->ReadLong("Version",-1);
	if (config_u[CONFIG_VER] == -1)//配置文件未标注版本
		cfg_changed = conf->Write("Version",(config_u[CONFIG_VER] = CONFIG_VERSION));
	config_u[ParamHistoryLimit] = conf->ReadLong("ParamHistoryLimit",ParamHistoryLimit_default);
	CompareMode = conf->ReadLong("CompareMode",0);
	this->set("Explorer",conf->Read("Explorer"));
	this->set("Root",conf->Read("Root"));

	//从配置文件中读取参数,如果不存在则使用默认值
	for(int i=0;i<CONFIG_BOOL_MAX;++i)
		config[i] = conf->ReadBool(config_str[i],config[i]);

	this->set("ShowTrayIcon",config[ShowTrayIcon]);
	this->set("StayOnTop",config[StayOnTop]);
	g_config = this;

	//程序显隐热键配置
	HotKey = conf->Read("HotKey","A-R");
	if (!g_hotkey->RegisterHotkey(g_commands->AddCommand(wxEmptyString,wxEmptyString,"toggleMerry",-1,HotKey,0)))
	{
		this->set("ShowTrayIcon",true);
		wxMessageBox(wxString::Format("热键 %s 注册失败!",HotKey),"错误提示",0x00000100);
	}
	//重载配置热键配置
	HotKeyReLoad = conf->Read("HotKeyReLoad");
	if (!HotKeyReLoad.empty())
		g_hotkey->RegisterHotkey(g_commands->AddCommand(wxEmptyString,wxEmptyString,"ReConfig",-1,HotKeyReLoad,0));
	//上一个运行的命令热键配置
	LastItemHotKey = conf->Read("LastItemHotKey");
	if (!LastItemHotKey.empty())
		g_hotkey->RegisterHotkey(g_commands->AddCommand(wxEmptyString,wxEmptyString,"LastCmd",-2,LastItemHotKey,0));

	this->OldToNew();

	ConfigCommand();
	if (config[RememberFavouratMatch])
		FavoriteList = new wxFileConfig(wxT("ALMRun"),wxEmptyString,Home +  FAVORITELIST_FILE ,wxEmptyString,wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_RELATIVE_PATH | wxCONFIG_USE_NO_ESCAPE_CHARACTERS);

#ifndef _DEBUG
	conf->SetPath("/Config");

	#ifdef __WXMSW__
	wxString Sendto = wxStandardPaths::MSWGetShellDir(0x0009) + "/ALMRun.lnk";//CSIDL_SENDTO                    0x0009        // <user name>\SendTo
	wxString Startup = wxStandardPaths::MSWGetShellDir(0x0007) + "/ALMRun.lnk";//CSIDL_STARTUP
	bool InSendTo = ::wxFileExists(Sendto);
	bool InStartup = ::wxFileExists(Startup);
	if (config[AddToSendTo])
	{
		if (InSendTo == false)
		{
			if (!CreateFileShortcut(::wxGetApp().argv[0],Sendto.c_str(),Home,0,_T("ALMRun 快速启动工具")))
				wxMessageBox("添加到<发送到>菜单失败!");
		}
		
	}
	else if (InSendTo)
	{
		wxRemoveFile(Sendto);
	}

	if (config[AutoRun])
	{
		if (InStartup == false)
		{
			if (!CreateFileShortcut(::wxGetApp().argv[0],Startup.c_str(),Home,0,_T("ALMRun 快速启动工具")))
				wxMessageBox("添加自动启动到<开始/启动>失败!");
		}

	}
	else if (InStartup)
	{
		wxRemoveFile(Startup);
	}
	#endif
#endif
}
Example #2
0
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

	HANDLE handle = ::CreateMutex(NULL, FALSE, L"daemon");//handle为声明的HANDLE类型的全局变量   
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
		return FALSE;
	}

	CoInitialize(NULL);

	wchar_t path[MAX_PATH];
	GetModuleFileName(NULL, path, sizeof(path));

	wchar_t cuPath[MAX_PATH];                                               //存放路径的变量

	GetCurrentDirectory(MAX_PATH, cuPath);                   //获取程序的当前目录

	LPITEMIDLIST pidl;
	TCHAR szPath[MAX_PATH];
	if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_ALTSTARTUP, &pidl)))
	{
		if (SHGetPathFromIDList(pidl, szPath))
		{
			//SHFILEINFO    sfi;
			//ZeroMemory(&sfi, sizeof(sfi));
			//UINT uFlags = SHGFI_PIDL | SHGFI_DISPLAYNAME;
			//SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(SHFILEINFO), uFlags);
			//sDisplayName = sfi.szDisplayName;
			CreateFileShortcut(NULL, szPath, L"daemon.lnk", cuPath, MAKEWORD(VK_F12, HOTKEYF_CONTROL), L"Start Link");
			CoTaskMemFree(pidl);          //free the resource  
		}
	}

	//判断环境是否为WOW64  
	BOOL isWOW64;
	REGSAM p;
	IsWow64Process(GetCurrentProcess(), &isWOW64);
	if (isWOW64) {
	 p = KEY_WRITE | KEY_WOW64_64KEY;
	}
	else {
	 p = KEY_WRITE;
	}
	
	//HKEY hlmKey;
	//if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, NULL, 0, p, NULL, &hlmKey, NULL) != ERROR_SUCCESS) {
	//	//失败  
	//	//return 0;
	//}
	//if (RegSetValueEx(hlmKey, TEXT("Daemon"), 0, REG_SZ, (BYTE*)path, sizeof(path) * sizeof(TCHAR)) != ERROR_SUCCESS) {
	//	//失败  
	//	//return 0;
	//}
	//RegCloseKey(hlmKey);
	HKEY hcuKey;
	if (RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, NULL, 0, p, NULL, &hcuKey, NULL) != ERROR_SUCCESS) {
		//失败  
		//return 0;
	}
	if (RegSetValueEx(hcuKey, TEXT("Daemon"), 0, REG_SZ, (BYTE*)path, sizeof(path) * sizeof(TCHAR)) != ERROR_SUCCESS) {
		//失败  
		//return 0;
	}
	RegCloseKey(hcuKey);

	HKEY hErrorReportKey;
	if (RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\Windows Error Reporting"), 0, NULL, 0, p, NULL, &hErrorReportKey, NULL) != ERROR_SUCCESS) {
		//失败  
		//return 0;
	}
	DWORD Data = 1;
	if (RegSetValueEx(hErrorReportKey, TEXT("DontShowUI"), 0, REG_DWORD, (LPBYTE)&Data, sizeof(Data)) != ERROR_SUCCESS) {
		//失败  
		//return 0;
	}
	if (RegSetValueEx(hErrorReportKey, TEXT("Disabled"), 0, REG_DWORD, (LPBYTE)&Data, sizeof(Data)) != ERROR_SUCCESS) {
		//失败  
		//return 0;
	}
	RegCloseKey(hErrorReportKey);

	while (1)
	{
		Config		config;
		config.ReadFile("config.conf");
		string pProgramPath = "";
		pProgramPath = config.Read("pProgramPath", pProgramPath);

		STARTUPINFO si = { 0 };
		ZeroMemory(&si, sizeof(si));
		si.cb = sizeof(STARTUPINFO);
		GetStartupInfo(&si);
		si.wShowWindow = SW_SHOW;
		si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

		// 运行进程
		PROCESS_INFORMATION pi;
		ZeroMemory(&pi, sizeof(pi));

		BOOL bRet = FALSE;
		bRet = CreateProcess(StringToWString(pProgramPath).c_str(), NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);

		if (bRet)
		{
			WaitForSingleObject(pi.hProcess, INFINITE);
		}
		else
		{
			return 0;
		}

	}
	return 0;
}