ATL Project Wizard
2025-01-19
Written by: tdtc
Recommendation: Starting October 14, 2025, do not use ATL or MFC in new projects.
!!! 除了IE的ActiveX项目,不推荐ATL !!!
!!! ATL is not recommended except for IE ActiveX projects. !!!
Use Visual Studio’s wizard to create COM
x86 IDE
x64 IDE
step by step:
File->New->Project
name: testATL
Note: name即是要生成的DLL文件
选择”MFC/ATL”中”ATL Project”:
Default
Build -> Configuration Manager
x64 or x86
Project -> Properties
Linker -> Register Output
No
Linker -> All Options ->
Register Output: No
右击项目名称
ATL Simple Object:
Short name: testSOW
Tab switching from “Solution Explorer” to “Class View”
vs2022: View -> Class View
Select the interface, Right-click -> add -> add method
name: ItestSOW
添加方法名称以及参数
[id(1), helpstring("add method")] HRESULT Add([in] LONG x, [in] LONG y, [out, retval] LONG* ret);
[id(2), helpstring("sub method")] HRESULT Sub([in] LONG x, [in] LONG y, [out, retval] LONG* ret);
@echo
cd /d %~dp0
Regsvr32 xbfInfo.dll
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Redist\MSVC\14.42.34433\x86\Microsoft.VC143.CRT\vcruntime140d.dll
and
C:\Windows\SysWOW64\ucrtbased.dll
放到testATL.dll所在的目录
Project -> Add Reference
按钮事件:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As New testATLLib.testSOW
Dim int1 As Long
'vs2010/vs2013'int1 = obj.Add(2, 3)
obj.Add(2, 3, int1)
Label1.Text = int1
End Sub
拷贝Interop.testATLLib.dll和WindowsApplication1.exe即可
Q: Vs 2012+
System.Runtime.InteropServices.COMException (0x80040154):
Retrieving the COM class factory for component with CLSID
{7A6E7413-F0E2-41B8-9BB7-036229C90FE5} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
A: vs高版本已经使用32/64位
It means the class id 877AA945-1CB2-411C-ACD7-C70B1F9E2E32 is not in the registry.
You can verify this by opening regedit.exe, browsing to HKEY_CLASSES_ROOT\CLSID{877AA945-1CB2-411C-ACD7-C70B1F9E2E32}.
If your running a 32-bit app on a 64 bit OS, look for HKEY_CLASSES_ROOT\Wow6432Node\CLSID{877AA945-1CB2-411C-ACD7-C70B1F9E2E32}
If it is there, it may be some other issue but it is probably missing.
To resolve this you will usually run the installer that distributes this COM object.
If you don’t have one and you know what dll implements the object, you can run regsvr32.exe (or regasm.exe for a managed dll).
仅安装ucrt(通用Windows sdk)会出现:
cannot find the resource compiler dll. please make sure the path is correct.
解决办法:
安装Windows 8.1/Windows 10 SDK