Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1217663
  • 博文数量: 950
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 13070
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-04 09:23
文章分类

全部博文(950)

文章存档

2011年(1)

2008年(949)

我的朋友

分类: C/C++

2008-08-04 09:36:59

下载本文示例代码
下载源代码

编译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文件。

欢迎大家交流指正…… 下载本文示例代码
阅读(782) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~