Welcome

首页 / 软件开发 / C++ / 调用HHA_CompileHHP编译chm工程

调用HHA_CompileHHP编译chm工程2010-07-20吴会然编译chm工程(.hhp)有两种方法:

调用hhc.exe

调用HHA_CompileHHP

调用hhc.exe编译,代码如下:

// strChmFileName为你的hhp文件的长文件名
CString strCmdLine;
strCmdLine = "hhc.exe ";
strCmdLine += strChmFileName;
WinExec(strCmdLine, SW_SHOWNORMAL);

用这种方法调用有很多缺点,首先,没有编译进度,没有任何提示,根据我的测试,还不支持有空格的文件名。一般专业的chm制作软件都是调用的hha.dll中的HHA_CompileHHP函数进行编译。我们这里不讨论如何生成chm的工程文件,这个可以参考Html Help WorkShop生成的文件。调用HHA_CompileHHP,微软没有提供函数声明,也没有提供帮助信息。(据说要和微软签订协议,才可以拿到函数声明及相关的头文件)参考了网上一篇介绍用delphi实现函数调用的文章,加上我的一点试验,终于我VC实现了。ok,看看我是怎么实现的。

先说下HHA_CompileHHP的参数:

(const char*, LPVOID, LPVOID, int)

第一个参数为你要编译的hhp文件名

第二个参数是编译日志回调函数

第三个参数是编译进度回调函数

第四个参数不知道什么用,我把他置为0

开始写代码了

///////////////////////////////////
// 下面是两个回调函数的声明
BOOL CALLBACK FunLog(char* pstr);
BOOL CALLBACK FunProc(char* pstr);
//
// CompileHHP
// 调用HHA_CompileHHP编译chm工程
// 参数:pzFileName为待编译hhp文件名
// 作者:吴会然(wuhran@126.com)
// blog: http://blog.sina.com.cn/u/1272907062
////////////////////////////////////////////////////////////////////////////////////
void CCompileCHMDemoDlg::BuildChmFile(CString strHHPFile)
{
HINSTANCE hinstLib;
typedef BOOL (WINAPI *MYFUNC)(const char*, LPVOID, LPVOID, int);
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
MYFUNC ProcAdd = NULL;

// Get a handle to the DLL module.
hinstLib = LoadLibrary("hha.dll");

// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYFUNC) GetProcAddress(hinstLib, "HHA_CompileHHP");

// If the function address is valid, call the function.
LPCSTR pzFileNmae = strHHPFile.GetBuffer(strHHPFile.GetLength());
if (fRunTimeLinkSuccess = (ProcAdd != NULL))
{
if(ProcAdd(pzFileNmae, FunLog, FunProc, 0))
{
}
}

// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}

if(m_pStatusBar)
m_pStatusBar->SetPaneText(0, "编译完成!");
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("message via alternative method ");

}
//////////////////////////////////////////////////////////////////////////////////////
// FunLog()
// 编译日志回调函数
// 参数:日志字符串
// 作者:吴会然(wuhran@126.com)
// blog: http://blog.sina.com.cn/u/1272907062
////////////////////////////////////////////////////////////////
BOOL FunLog(char* pstr)
{
ASSERT(pstr);
CString strMsg;
strMsg.Format("%s", pstr);
// AfxMessageBox(strMsg);

return true;
}
//////////////////////////////////////////////////////////////
//
// FunProc()
// 编译进度回调函数
// 参数:进度字符串
// 作者:吴会然(wuhran@126.com)
// blog: http://blog.sina.com.cn/u/1272907062
//////////////////////////////////////////////////////////////
BOOL FunProc(char* pstr)
{
ASSERT(pstr);
CString strMsg;
strMsg.Format("%s", pstr);

// AfxMessageBox(strMsg);

return true;
}

联系方式:

wuhran@126.com

http://blog.sina.com.cn/u/1272907062

上面的测试代码在VC6.0+WIN2003下编译通过,请确保你的系统目录或程序当前目录下存在hha.dll文件。

欢迎大家交流指正……