[Xiao Mu learns C++] Embed 64-bit ActiveX control in C++ MFC (VS2017)

1 Introduction

1.1 MFC

The Microsoft Foundation Classes (MFC) library provides object-oriented wrappers for most Win32 and COM APIs. Although this wrapper can be used to create extremely simple desktop applications, it will be most useful when you need to develop more complex user interfaces with multiple controls. You can use MFC to create applications with Office-style user interfaces.

https://learn.microsoft.com/zh-cn/cpp/mfc/mfc-desktop-applications?view=msvc-170

The MFC library (DLL) for Multibyte Character Encoding (MBCS) is no longer included in Visual Studio, but is available as a Visual Studio add-in.

  • The following figure represents an MFC class derived from CObject:
    Insert image description here
  • The following figure represents the MFC classes derived from CWnd and CCmdTarget:
    Insert image description here
  • The following figure represents an MFC class that does not derive from CObject:
    Insert image description here

1.2 ActiveX

ActiveX controls are a specific type of automation server; they are reusable components. The application that hosts an ActiveX control is the automation client for the control. In Visual C++, you can use MFC or ATL to create ActiveX controls.

  • Interaction between ActiveX control containers and windowed ActiveX controls
    Insert image description here

  • Communication between ActiveX control containers and ActiveX controls
    Insert image description here

  • Windows message handling in windowed ActiveX controls (while active)
    Insert image description here

ActiveX is an old technology and should not be used for new development. You can use modern technologies such as HTML5 and JavaScript, modern browser extensions, or WebAssembly modules to perform many of the functions of ActiveX controls in a simpler and more secure way. For more information, see Say goodbye to the past (Part 2): Say goodbye to ActiveX, VBScript, attachEvent, and native messages, as well as Microsoft Edge extensions and WebAssembly.

https://blogs.windows.com/msedgedev/2015/05/06/a-break-from-the-past-part-2-saying-goodbye-to-activex-vbscript-attachevent/

2. Use ActiveX controls

2.1 Register controls

  • Command line registration

The Regsvr32 command is used to register COM components. It is a command provided by the Windows system to register controls with the system or uninstall controls. It is run in command line mode.
regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname where dllname is the activex control file name. It is recommended to copy it to the system folder before installation.

Regsvr32 [/u] [/n] [/i[:cmdline]] <dllname>
%systemroot%\SysWoW64\regsvr32 <full path of the DLL>

参数解释如下:
/u 				#反注册控件,取消注册服务器
/s 				#指定 regsvr32 安静运行,在成功注册/反注册DLL文件的前提下不显示结果提示框。
/n 				#不调用 DllRegisterServer;此选项必须与 /i 一起使用
/i:cmdline 		#调用 DllInstall,为其传递一个可选 [cmdline];当与 /u 一起使用时,可调用 dll uninstall
dllname 		#指定要注册的 dll 文件名。
# 已管理员权限运行命令行窗口执行如下注册命令
regsvr32 FxActiveX_MfcListCtrl.ocx

# 取消注册
regsvr32 /u FxActiveX_MfcListCtrl.ocx
  • C++ Code Registration
    If you wish, you can write a C++ code installer to register the control directly.
    Use the LoadLibrary Windows API to load the control DLL. Next, use GetProcAddress to get the address of the "DllRegisterServer" function. Finally, the DllRegisterServer function is called. The following code example demonstrates one possible approach, where hLib stores a handle to the control library and lpDllEntryPoint stores the address of the "DllRegisterServer" function.
HINSTANCE hLib = LoadLibrary(pszDllName);

if (hLib < (HINSTANCE)HINSTANCE_ERROR)
{
    
    
   AfxMessageBox(IDS_LOADLIBFAILED); //unable to load DLL
   iReturn = FAIL_LOAD;              //unable to load DLL
}

// Find the entry point.
lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer");
if (lpDllEntryPoint != NULL)
(*lpDllEntryPoint)();
else
;// Unable to locate entry point

OleInitialize should be called before Setup installs the ActiveX control. After the installer completes, OleUnitialize is called. This ensures that the OLE system DLL is in the correct state to register ActiveX controls.

2.2 Add controls

QA: MFC dialog web browser activeX Control will not generate class correctly.
https://developercommunity.visualstudio.com/t/mfc-dialog-web-browser-activex-control-will-not-ge/499195

In order to solve the problem that the x64 ActiveX control cannot be found in the 32-bit IDE, the pre-created and registered FxActiveX_MfcListCtrl (ActiveX control) is still used for testing. The difference is that its configuration is x64.

The adding method is different from that of 32-bit controls (of course, you can also register the corresponding 32-bit control first, add the control code, then register the 64-bit control, and then compile the code project). Here, the method of dynamically creating controls is used to successfully solve the problem of being unable to add. .

  • (1) Create a new dialog-based project.
    Insert image description here
  • (2) Right-click the mouse to pop up the menu "Class Wizard"
    Insert image description here
  • (3) In the "Class Wizard" window, click the drop-down box "Add Class" and select the menu "MFC Class in ActiveX Control".

Insert image description here

  • (4) No information about the related control FxActiveX_MfcListCtrl (ActiveX control) was found among the available ActiveX controls.
    If it is a 32-bit control registered, it can be seen.
    Insert image description here
  • (5) Choose to load directly from file.
    Insert image description here
  • (6) Export the control class in the file.
    Insert image description here
  • (7) Automatically generate corresponding control class code files.
    Insert image description here
  • (8) Add control object definition code.
    Insert image description here
#include "CDXlist0.h"
CDXlist0 m_ctl;
  • (9) Add the control object implementation code.
    Insert image description here
#define IDC_OCX_CTL 1000
	
	CRect rect;
	GetClientRect(&rect);
	m_ctl.Create(NULL, WS_CHILD | WS_VISIBLE, rect, this, IDC_OCX_CTL);
	m_ctl.ShowWindow(SW_SHOW);

	for (int i = 0; i < 100; i++) {
    
    
		CString strText;
		strText.Format(L"hello: %d", i);
		m_ctl.AddTextItem(0, strText);
	}
  • (10) Compile and run the project.
    Insert image description here
    ActiveX control successfully loaded!
  • Summarize
    • (1) Adding in the registry: You need to register the corresponding 32-bit control first. Then select the corresponding control item through the drop-down box of the registry to generate the control class.
    • (2) Add by file method: If there is no corresponding 32-bit control, first register the corresponding 64-bit control, and then add the 64-bit control file through the file method to generate the control class.

Conclusion

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡)
感谢各位大佬童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)! ! !

Guess you like

Origin blog.csdn.net/hhy321/article/details/133198491