#include "stdafx.h"
#include "Public.h"
#ifndef DbgOutput
void DbgOutput(TCHAR *fmt,...)
{
TCHAR strDbg[255];
va_list args;
va_start(args, fmt);
#ifdef _UNICODE
vswprintf(strDbg, fmt, args);
#else
vsprintf(strDbg,fmt,args);
#endif
va_end(args);
OutputDebugString(strDbg);
#ifdef DBGOUTFILE
HANDLE hDbgFile = CreateFile("Info.txt",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDbgFile != INVALID_HANDLE_VALUE)
{
DWORD dwBytes;
SetFilePointer(hDbgFile, 0, NULL, FILE_END);
WriteFile(hDbgFile, strDbg, strlen(strDbg), &dwBytes, NULL);
CloseHandle(hDbgFile);
}
#endif
}
#endif
void WINAPI ShowErrorMessage(DWORD dwError, LPCTSTR strCap)
{
if (dwError == 0)
return;
LPVOID lpMsgBuf;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf, 0, NULL))
{
DPrint(("ERROR--%s:%d\n", strCap, dwError));
LocalFree(lpMsgBuf);
}
}
void WINAPI GetErrorMsg(DWORD dwError, LPTSTR strErrMsg)
{
if (strErrMsg == NULL)
return ;
LPVOID lpMsgBuf;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf, 0, NULL))
{
DPrint(("ERROR--:%d\n", dwError));
strcpy(strErrMsg, (const char*)lpMsgBuf);
LocalFree(lpMsgBuf);
}
}
/**********************************************************
弹出保存、打开文件对话框
************************************************************/
BOOL WINAPI ShowDialogForm(HWND hWnd,LPTSTR strFileName,BOOL bOpen,
LPCTSTR lpstrInitialDir, LPCTSTR strFilter, LPCTSTR strDefExt)
{
memset(strFileName,0,MAX_PATH);
OPENFILENAME ofn = { sizeof(OPENFILENAME), hWnd, NULL,
strFilter, NULL,
0, 1, strFileName, MAX_PATH, NULL, 0,
lpstrInitialDir,//lpstrInitialDir
NULL,
OFN_HIDEREADONLY, 0, 0,
TEXT("bin"), 0, NULL, NULL };
if (bOpen)
{
ofn.Flags |= OFN_FILEMUSTEXIST;
return GetOpenFileName(&ofn);
}
else
{
return GetSaveFileName(&ofn);
}
}
int WINAPI BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED)
SendMessage(hWnd, BFFM_SETSELECTION, 1, lpData);
return FALSE;
}
/*******************************************
浏览目录对话筐
hWnd : 父窗口句柄
strPath:选取的路径
*********************************************/
BOOL WINAPI BrowserDirDlg(HWND hWnd, LPTSTR strPath)
{
BOOL fResult = FALSE;
BROWSEINFO bi;
memset(&bi,0,sizeof(bi));
bi.lpszTitle = "Please select a directory";
bi.hwndOwner = hWnd;
bi.lpfn = BrowseCallbackProc;//回调函数
bi.lParam = (LPARAM)strPath;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl != NULL)
{
if (SHGetPathFromIDList(pidl,strPath))
{
fResult = TRUE;
}
GlobalFree(pidl);
}
return fResult;
}
/*******************************************************************************************************
运行指定命令
lpFileName:文件名
lpVerb:执行动作(如 "Open", "Install"...)
lParam:参数
lpDir:有些程序会有一定的工作目录
fWait:是否等待执行完成
nShow:窗口模式(SW_SHOW, SW_HIDE)
********************************************************************************************************/
BOOL WINAPI ExecuteCmd(LPCTSTR lpFileName, LPCTSTR lpVerb, LPCTSTR lParam, LPCTSTR lpDir, BOOL fWait, int nShow)
{
SHELLEXECUTEINFO ExecInfo;
memset(&ExecInfo, 0, sizeof(SHELLEXECUTEINFO));
ExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ExecInfo.lpVerb = lpVerb;
ExecInfo.lpFile = lpFileName;
ExecInfo.nShow = nShow;
ExecInfo.lpDirectory = lpDir;//工作目录
ExecInfo.lpParameters = lParam;//参数
ExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;//不关闭进程句柄
BOOL fResult = ShellExecuteEx(&ExecInfo);
if (fResult && fWait)
{
WaitForSingleObject(ExecInfo.hProcess,INFINITE);
}
return fResult;
}
/*************************************************************************
把文件资源存到指定文件
strFileName: 指定文件
nResID:资源ID
strResType:资源类型
**************************************************************************/
BOOL WINAPI ResourceToFile(LPCTSTR strFileName, UINT nResID, LPCTSTR strResType)
{
BOOL fResult = FALSE;
//查找资源
HRSRC hrSrc = FindResource(NULL, MAKEINTRESOURCE(nResID), strResType);
if (hrSrc != NULL)
{
//载入资源
HGLOBAL hGlobal = LoadResource(NULL, hrSrc);
if (hGlobal != NULL)
{
//取得资源长度
DWORD dwFileSize = SizeofResource(NULL, hrSrc);
//锁定资源, pFile为资源地址
BYTE *pFile = (BYTE*)LockResource(hGlobal);
HANDLE hFile = CreateFile(strFileName, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwRet;
fResult = WriteFile(hFile, pFile, dwFileSize, &dwRet, NULL);
CloseHandle(hFile);
}
UnlockResource(hGlobal);
FreeResource(hGlobal);
}
}
return fResult;
}
/***********************************************************
建立快捷方式
strFileName:目标文件名
strLinkName:快捷方式名
*************************************************************/
BOOL WINAPI CreateLink(LPTSTR strFileName,LPTSTR strLinkName)
{
IShellLinkA *pLink;
IPersistFile *pPFile;
WCHAR wcFile[MAX_PATH];
HRESULT hr = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink,(void **)&pLink) ;
if (FAILED(hr))
return FALSE;
pLink->SetPath(strFileName);
hr = pLink->QueryInterface(IID_IPersistFile, (void**)&pPFile);
if (FAILED(hr))
return FALSE;
MultiByteToWideChar(CP_ACP,0,strLinkName,-1,wcFile,MAX_PATH);
hr = pPFile->Save(wcFile,TRUE);
pPFile->Release();
pLink->Release();
return TRUE;
}
/**************************************************************************************************
安装文件到指定目录
strPath:指定目录
strIniFile:Ini文件
strResType:资源类型
hProgWnd:安装进度条句柄
INI文件中有关安装文件的信息设定:
段名:COPY
KEY:0 对应 文件数量
1 ~ N 对应 资源ID
资源ID 对应 文件名称
***************************************************************************************************/
BOOL WINAPI CopyInstallFile(LPCTSTR strPath, LPCTSTR strIniFile, LPCTSTR strResType, HWND hFileWnd, HWND hProgWnd)
{
TCHAR strFileName[MAX_PATH];
TCHAR strFilePath[MAX_PATH];
TCHAR sKey[64];
UINT nResID;
//取得文件数量
UINT nCount = GetPrivateProfileInt("COPY", "0", 0, strIniFile);
if (hProgWnd != NULL)
{
//设置进度条信息
PostMessage(hProgWnd, PBM_SETSTEP, 1L, 0L);
PostMessage(hProgWnd, PBM_SETRANGE32, 0L, (LPARAM)nCount);
PostMessage(hProgWnd, PBM_SETPOS, 0L, 0L);
}
for(UINT i=0;i<nCount;i++)
{
//取资源ID
nResID = GetPrivateProfileInt("COPY", itoa(i+1, sKey, 10), 0, strIniFile);
if (nResID > 0)
{
//取资源ID对应的文件名称
GetPrivateProfileString("COPY", itoa(nResID, sKey, 10), "", strFileName, MAX_PATH, strIniFile);
if (strFileName[0] != '\0')
{
wsprintf(strFilePath, "%s\\%s", strPath, strFileName);
if (hFileWnd != NULL)
{
SetWindowText(hFileWnd, strFilePath);
//PostMessage(hFileWnd, WM_SETTEXT, 0L, (LPARAM)strFilePath);
DPrint(("Copy %d:%s\n", i, strFilePath));
}
// SetWindowText(hFileWnd, strFilePath);
if (!ResourceToFile(strFilePath, nResID, strResType))
{
return FALSE;
}
}
}
//更新进度条
if (hProgWnd != NULL)
PostMessage(hProgWn