Chinaunix首页 | 论坛 | 博客
  • 博客访问: 454950
  • 博文数量: 724
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(724)

文章存档

2011年(1)

2008年(723)

我的朋友

分类:

2008-10-13 17:20:50

调用HHA_CompileHHP编译chm工程

作者:

编译chm工程(.hhp)有两种方法:

  1. 调用hhc.exe
  2. 调用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\n"); 
	
}

//////////////////////////////////////////////////////////////////////////////////////
// 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文件。

欢迎大家交流指正……
--------------------next---------------------

#pragma once

class CChmCompile
{
public:
typedef BOOL (WINAPI *MYPROC)(PCSTR);
typedef BOOL (WINAPI *FUNC)(PCSTR, MYPROC, MYPROC, int);

CChmCompile()
{
m_hLib = NULL;
m_pfnHHA_CompileHHP = NULL;
m_hLib = LoadLibrary(_T("hha.dll")); 
if (m_hLib != NULL)
{
m_pfnHHA_CompileHHP = (FUNC) GetProcAddress(m_hLib, "HHA_CompileHHP"); 



}
~CChmCompile()
{
if(m_hLib)
FreeLibrary(m_hLib); 
}

BOOL Run(LPCSTR pszFilename)
{
if(m_pfnHHA_CompileHHP != NULL) {
return m_pfnHHA_CompileHHP(pszFilename, FunLog, FunProc, 0); //return 0
}
return FALSE;
}

private:
HINSTANCE m_hLib; 
FUNC m_pfnHHA_CompileHHP;
static BOOL CALLBACK FunLog(PCSTR pstr)
{
ATLTRACE("%s\n", pstr);
return 0;
}
static BOOL CALLBACK FunProc(PCSTR pstr)
{
ATLTRACE("%s\n", pstr); // print filename
return 0;
}

};

根据作者的代码重新封装了一个类, 但我调用没有成功,函数返回0, 不知为何? ( yuanyuankang 发表于 2007-5-1 10:43:00)
 


《COM应用程序框架》发部源代码了。

上有下载。 ( 七月寒雪 发表于 2007-4-27 21:15:00)
 
.......................................................

--------------------next---------------------

阅读(288) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~