DWORD Inject(HANDLE hProc, LPWSTR engine)
{
	LPVOID lpvAllocAddr = 0;
	DWORD dwWrite = 0x1000, len = 0;
	HANDLE hTH;
	WCHAR path[MAX_PATH];
	LPWSTR p;
	if (!IthCheckFile(DllName)) return -1;
	p = GetMainModulePath();
	len = wcslen(p);
	memcpy(path, p, len << 1);
	memset(path + len, 0, (MAX_PATH - len) << 1);
	for (p = path + len; *p != L'\\'; p--); //Always a \ after drive letter.
	p++;
	wcscpy(p, DllName);

	NtAllocateVirtualMemory(hProc, &lpvAllocAddr, 0, &dwWrite, MEM_COMMIT, PAGE_READWRITE);
	if (lpvAllocAddr == 0) return -1;

	CheckThreadStart();

	//Copy module path into address space of target process.
	NtWriteVirtualMemory(hProc, lpvAllocAddr, path, MAX_PATH << 1, &dwWrite);

	hTH = IthCreateThread(LoadLibrary, (DWORD)lpvAllocAddr, hProc);
	if (hTH == 0 || hTH == INVALID_HANDLE_VALUE)
	{
		ConsoleOutput(ErrorRemoteThread);
		return -1;
	}
	NtWaitForSingleObject(hTH, 0, 0);

	THREAD_BASIC_INFORMATION info;
	NtQueryInformationThread(hTH, ThreadBasicInformation, &info, sizeof(info), &dwWrite);
	NtClose(hTH);
	if (info.ExitStatus != 0)
	{
		wcscpy(p, engine);
		NtWriteVirtualMemory(hProc, lpvAllocAddr, path, MAX_PATH << 1, &dwWrite);
		hTH = IthCreateThread(LoadLibrary, (DWORD)lpvAllocAddr, hProc);
		if (hTH == 0 || hTH == INVALID_HANDLE_VALUE)
		{
			ConsoleOutput(ErrorRemoteThread);
			return -1;
		}
		NtWaitForSingleObject(hTH, 0, 0);
		NtClose(hTH);
	}

	dwWrite = 0;
	NtFreeVirtualMemory(hProc, &lpvAllocAddr, &dwWrite, MEM_RELEASE);
	return info.ExitStatus;
}
// 帮助
void OnHelp()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	DestroyAllDialog();

	CString strMainModulePath;
	GetMainModulePath(strMainModulePath, (HMODULE)theApp.m_hInstance);
	if (strMainModulePath.IsEmpty())
		return;

	CString strHelpFileName = strMainModulePath + L"\\Help\\Smart3DCoating.pdf";
	ShellExecute(NULL, TEXT("open"), strHelpFileName, NULL, NULL, SW_SHOW);
}
BOOL CSDMdlAttrMatCheck::DoAction(void *pData, const CheckData &checkData) 
{
	ProError status;
	ProSolid pMdl = (ProSolid)pData;
	CString strMatName = checkData.checkRule.arrRuleContent[0];
	
	if (strMatName.CompareNoCase(L"null") == 0)
	{
		ProMaterial MdlMat;
		status = ProMaterialCurrentGet((ProSolid)pMdl,&MdlMat);
		status = ProMaterialDelete(&MdlMat);

		return TRUE;
	}

	CString strMatResPath;

	GetMainModulePath((HMODULE)theApp.m_hInstance, strMatResPath);

	strMatResPath = strMatResPath + L"Resource\\MaterialLib\\";

	ProPath mtl_file_path;
	ProPath current_dir;
    ProName mtl_name;

	wcsncpy_s(mtl_name, PRO_NAME_SIZE, (LPCTSTR)strMatName, _TRUNCATE);

	wcsncpy_s(mtl_file_path, PRO_PATH_SIZE, (LPCTSTR)strMatResPath, _TRUNCATE);

	ProDirectoryCurrentGet (current_dir);

	ProDirectoryChange (mtl_file_path);

	status = ProMaterialfileRead (pMdl, mtl_name);

	ProDirectoryChange (current_dir);

	if (status == PRO_TK_E_NOT_FOUND || status == PRO_TK_GENERAL_ERROR)
	{
		return FALSE;
	}

	ProMaterial Mat;
	Mat.part = pMdl;
	ProWstringCopy (mtl_name, Mat.matl_name, PRO_VALUE_UNUSED);

	status = ProMaterialCurrentSet (&Mat);

	return TRUE;
}
void CDlgCheckRulePara::OnBnClickedOpenFile()
{
	UpdateData(TRUE);
	CString strFile;
	int nSel = m_cmbPath.GetCurSel();
	if (m_cmbPath.GetItemData(nSel) == 1) // 内嵌表格
	{
		GetMainModulePath((HMODULE)theApp.m_hInstance, strFile);
		strFile = strFile + L"Resource\\ParaCheckTemplate.xls";
	}
	else
		strFile = m_strPath;

	if (!IsFileExist(strFile))
	{
		MessageBox(L"文件不存在", L"警告", MB_OK|MB_ICONINFORMATION);
	}
	else if (IsFileOpened(strFile))
	{
		MessageBox(L"文件已打开,请先关闭", L"警告", MB_OK|MB_ICONINFORMATION);
	}
	else
		ShellExecute(0, L"open", strFile, L"", L"",SW_SHOWNORMAL);
}
void CDlgCheckRuleMdlMatSystem::OnBnClickedMatFolder()
{
	CString strMatResPath;
	CBpRegKey rk;
	CString strPath = DOCTOR_PRODUCT_KEYROOT;
	strPath += REGISTRY_KEY_OPTION;
	if (rk.Open(HKEY_CURRENT_USER, strPath) == ERROR_SUCCESS)
	{
		// 材料库的路径
		CString strValue;
		if (rk.Read(REGISTRY_KEY_MATERIALLIB_PATH, strValue) == ERROR_SUCCESS)
			strMatResPath = strValue;
	}

	if (strMatResPath.IsEmpty())
	{
		GetMainModulePath(AfxGetInstanceHandle(), strMatResPath);
		strMatResPath = strMatResPath + L"Resource\\MaterialLib\\";
	}
	if (strMatResPath[strMatResPath.GetLength()-1] != L'\\')
		strMatResPath = strMatResPath + L"\\";

	// 允许多选
	CFileDialog dlgFile(TRUE, _T("*.mtl"), NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, _T("材料文件(*.mtl)|*.mtl"));
	if (GetVersion() < 0x80000000) 
		// 运行的操作系统是Windows NT/2000 
		dlgFile.m_ofn.lStructSize = 88;	// 显示新的文件对话框 
	else
		// 运行的操作系统Windows 95/98 
		dlgFile.m_ofn.lStructSize = 76;	// 显示老的文件对话框 
	dlgFile.m_ofn.lpstrInitialDir = strMatResPath;
	
	if (dlgFile.DoModal() == IDOK)
	{
		POSITION pos_file;
		pos_file = dlgFile.GetStartPosition();

		CString strFilePath;
		CStringArray arrMatNames;
		while(pos_file != NULL)
		{
			strFilePath = dlgFile.GetNextPathName(pos_file);
			int nLength = strFilePath.GetLength();
			int nPos = strFilePath.ReverseFind(_T('\\'));
			strFilePath = strFilePath.Mid(nPos+1, nLength-nPos-5);
			arrMatNames.Add(strFilePath);
		}
		int nListCount = (int)m_listMats.GetItemCount();
		for (int i=0; i<arrMatNames.GetCount(); i++)
		{
			BOOL bExist = FALSE;
			for (int j=0; j<nListCount; j++)
			{
				if (arrMatNames[i].CompareNoCase(m_listMats.GetItemText(j, 0)) == 0)
				{
					bExist = TRUE;
				}
			}
			if (!bExist)
				m_listMats.InsertItem(nListCount, arrMatNames[i]);
		}
		UpdateCheckRuleByControl();
		::SendMessage(GetParent()->m_hWnd, WM_MODIFY_CHECK_RULE, 0, (LPARAM)&m_checkRule);
	}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	static WCHAR dll_exist[] = L"ITH_DLL_RUNNING";
	static HANDLE hDllExist;
	switch (fdwReason) 
	{ 
	case DLL_PROCESS_ATTACH:
		{
		LdrDisableThreadCalloutsForDll(hinstDLL);	
		IthBreak();
		module_base = (DWORD)hinstDLL;
		IthInitSystemService();
		DWORD s;
		swprintf(hm_section,L"ITH_SECTION_%d",current_process_id);
		hSection=IthCreateSection(hm_section,0x2000,PAGE_EXECUTE_READWRITE);	
		NtMapViewOfSection(hSection,NtCurrentProcess(),(PVOID*)&hookman,0,
			hook_buff_len,0,&hook_buff_len,ViewUnmap,0,PAGE_EXECUTE_READWRITE);
		LPWSTR p;		
		for (p = GetMainModulePath(); *p; p++);
		for (p = p; *p != L'\\'; p--);
		wcscpy(dll_name,p+1);
		//swprintf(dll_mutex,L"ITH_%.4d_%s",current_process_id,current_dir);
		swprintf(dll_mutex,L"ITH_%d",current_process_id);
		swprintf(hm_mutex,L"ITH_HOOKMAN_%d",current_process_id);
		hmMutex=IthCreateMutex(hm_mutex,0);
		hMutex=IthCreateMutex(dll_mutex,1,&s);
		if (s) return FALSE;
		hDllExist = IthCreateMutex(dll_exist, 0);
		hDLL=hinstDLL; running=true;
		current_available=hookman;
		GetFunctionNames();
		InitFilterTable();
		InitDefaultHook();
		
		hSendThread=IthCreateThread(WaitForPipe,0);
		hCmdThread=IthCreateThread(CommandPipe,0);
		}
		break; 
	case DLL_PROCESS_DETACH:
	{		
		running=false;
		live=false;
		NtWaitForSingleObject(hSendThread,0,0);
		NtWaitForSingleObject(hCmdThread,0,0);
		NtClose(hCmdThread);
		NtClose(hSendThread);
		for (TextHook* man=hookman;man->RemoveHook();man++);
		LARGE_INTEGER lint={-10000,-1};
		while (enter_count) NtDelayExecution(0,&lint);
		for (TextHook* man=hookman;man<hookman+MAX_HOOK;man++) man->ClearHook();
		NtUnmapViewOfSection(NtCurrentProcess(),hookman);
		NtClose(hSection);	
		NtClose(hMutex);

		delete tree;
		IthCloseSystemService();
		NtClose(hmMutex);
		NtClose(hDllExist);
		break;
	}
	default: 
		break; 
	 } 
	return TRUE; 
}