How to Register COM in VS
2012-11-29 21:42
274 查看
在 Visual Studio .Net 部署项目中注册 COM 模块的步骤
将 COM 对象添加到 Visual Studio 部署项目。在解决方案资源管理器中,右键单击刚添加的模块,然后单击属性。
注意:“属性”窗口包含一个表,其中有两列和 x 行(行数取决于项目)。左列列出特定属性。右列将在步骤 4 中解释。
转到此模块的属性(默认情况下位于 .NET 部署项目的右上角),然后单击“注册表属性”。
注意:注册表属性指定在安装过程中,应将文件、程序集还是项目输出组注册在目标计算机上。
在注册表属性的右列中有一个列表框,它显示供您选择的几个选项。请注意解释这些选项的下列详细信息:
对于程序集,通常不需要注册,因而默认值是 DoNotRegister(这意味着安装过程中不会注册该项)。
对于 COM 模块,您可以选择 COM、COMRelativePath 和 COMSelfReg。在安装过程中,这三个选项中的任何一个都将注册 COM 模块。
请注意下列有关每个选项的详细信息:
COM:模块将由 Windows 安装程序引擎注册为 COM 对象。部署项目将更新相应 .msi 文件的 Registry Tables 组中的 Class 表、ProgID 表和其他表。这是注册 COM 模块的建议方式。
COMRelativePath:模块将由 Windows 安装程序引擎注册为独立的 COM 对象。请注意,此模块只能由安装它时使用的应用程序所使用。
COMSelfReg:在安装模块时,安装程序调用此模块的 DllRegisterServer 函数;在卸载模块时,安装程序调用 DllUnregisterServer 函数。部署项目将更新相应 .msi 文件的 SelfReg 表。建议安装包不要使用自注册。相反,安装包应通过创作安装程序所提供的、用于此用途的一个或多个其他表注册模块(即选择 COM 或COMRelativePath 选项)。对于自注册,将失去拥有中央安装程序服务的许多好处,原因是自注册例程常常会隐藏关键配置信息。
现在可以生成部署项目,以允许前面的修改依照在步骤 4 中选择的注册属性选项来注册 COM 对象。
编写批处理脚本.bat文件,使用regsvr32 /s命令注册,调用bat脚本。
用 LoadLibrary 和 GetProcAddress 取得控件中 DllRegisterServer
函数地址,然后调用注册。
typedef LONG (*FUN)(); HMODULE hDll = ::LoadLibrary(TEXT("C:\\WINDOWS\\system32\\Macromed\\Flash\\Flash10e.ocx")); if (!hDll) return; FUN fn = (FUN)GetProcAddress(hDll, "DllRegisterServer"); if (fn) { fn(); } FreeLibrary(hDll);
让应用程序在运行的时候自动检查所需要的支持文件和注册控件,免除安装和手动注册。适当修改下面的代码中的支持文件列表,并在App::InitInstance()中调用。
#include <shlwapi.h> #pragma comment(lib, "shlwapi.lib") //检测需要的文件是否存在 //并且对需要注册的文件进行注册 //输入参数用于指示文件所在的路径 //返回TRUE表示成功 BOOL Check_Environment(LPCTSTR lpszAppPath) //传入文件路径 { //列出所需要的文件列表 typedef struct _FILE_CELL { LPCTSTR szFileName; //文件名 BOOL bNeedRegister; //需要注册? }FILE_CELL; const FILE_CELL Support_FileList[] = { {_T("aaa.ocx"), TRUE}, {_T("bbb.ocx"), TRUE}, {_T("ccc.ocx"), TRUE}, {_T("fff.dll"), TRUE}, {_T("other.sys")}, //other need file 0, //Don't remove this line }; BOOL bRet = 0; TCHAR e_mess[4096], mess[256]; memset(e_mess, 0, sizeof(e_mess)); int uIndex = 0; while(Support_FileList[uIndex].szFileName) { TCHAR fileName[MAX_PATH + 1]; _tcscpy_s(fileName, lpszAppPath); PathAddBackslash(fileName); _tcscat_s(fileName, Support_FileList[uIndex].szFileName); if(PathFileExists(fileName)) { //if(_tcsicmp(PathFindExtension(fileName),_T(".ocx"))==0) //是需要注册的OCX文件 if(Support_FileList[uIndex].bNeedRegister) //需要注册 { //使用短路径名主要是为兼容Win98 TCHAR shortfileName[MAX_PATH + 1]; GetShortPathName(fileName, shortfileName, MAX_PATH); // Path to OLE Control in shortfileName HMODULE hModule = LoadLibrary(shortfileName); if(hModule) { typedef HRESULT (STDAPICALLTYPE *CTLREGPROC)() ; // Requires olectl.h CTLREGPROC DLLRegisterServer = (CTLREGPROC)GetProcAddress(hModule,"DllRegisterServer" ) ; if(DLLRegisterServer) { if(DLLRegisterServer() != S_OK) { _stprintf_s(mess, _T("注册模块失败 %s\r\n"), fileName); _tcscat_s(e_mess, mess); } } else { _stprintf_s(mess, _T("模块%s找不到指定注册入口\r\n"), fileName); _tcscat_s(e_mess, mess); } FreeLibrary(hModule) ; } else { _stprintf_s(mess, _T("加载模块失败 %s\r\n"), fileName); _tcscat_s(e_mess, mess); } } } else //提示缺少必要的支持文件 { _stprintf_s(mess, _T("缺少必要的文件 %s\r\n"), fileName); _tcscat_s(e_mess, mess); } uIndex++; } if(_tcslen(e_mess) > 0) //检测出错提示 { MessageBox(NULL, e_mess, _T("Error"), MB_OK ); } else { bRet = true; } return(bRet); }
相关文章推荐
- How to Register COM in VS
- How to create simple web service in VS2010, NOT WCF service
- How to determine if a driver should have been registered in kernel and lspci vs pci_device_id
- How to debug Control Panel applet (cpl) in VS .NET 2005
- How to build the environment of XPCOM in Windows XP
- How to resolve errors opening currentsettings.vssettings in the final release of VS 2008
- How to Register a Host Concurrent Program in Applications
- All-In-One Code Framework(AIO): 如何使用C#编写进程外的COM组件 (How to write an out-of-proc COM server in C#)
- What is EPOLL? Epoll vs Poll vs Select call ? And How to implement UDP server in Linux using EPOLL?
- How To Check Member In Window VS With CplusPlus?
- MSMQ&Com+ Service: How to create an Com+ Service in .NetFramework
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- how to remote debug in vs 2013
- How to Run MSTest.exe in Destination PC without be installed VS IDE
- How to implement xpcom module in JavaScript
- What is EPOLL? EPOLL vs Select call? And How to implement UDP server in Linux using EPOLL?
- How to register .NET components with COM
- How do I fix the linker error in project upgraded from VS2010 to VS2013 where the linker is looking
- How to Hook Up a VS.NET 2005 Solution With CruiseControl.NET in a Few Minutes
- Tips: How to use system environment variables in VS 2008 Post-Build events?