!!! 除了IE的ActiveX项目,不推荐ATL !!!
!!! ATL is not recommended except for IE ActiveX projects. !!!
Use Visual Studio’s wizard to create COM
- Visual Studio 2013 ~ 2019
x86 IDE
- Visual Studio 2022
x64 IDE
step by step:
- 建立ATL工程
- 建立ATL Simple Object
- 添加IDL方法
- 测试添加的method
建立ATL工程
File->New->Project
name: testATL
Note: name即是要生成的DLL文件
选择"MFC/ATL"中"ATL Project":
Default
Choice Platform
Build -> Configuration Manager
x64 or x86
取消编译时注册Dll
Project -> Properties
Linker -> Register Output
No
x64 for vs2022+
Linker -> All Options ->
Register Output: No
Add ATL Simple Object
右击项目名称
- vs2013/vs2015:
Add -> Class
- vs2017/vs2019:
Add -> New Item
- vs2022:
Add -> Module
ATL Simple Object:
设置
Short name: testSOW
- Simple Object ID
ProgID: testATL.testSOW
IE支持
- vs2013/vs2015
Options -> Support - vs2017/vs2019/vs2022
Other Options
添加IDL方法
Tab switching from “Solution Explorer” to “Class View”
vs2022: View -> Class View
Select the interface, Right-click -> add -> add method
name: ItestSOW
添加方法名称以及参数
MIDL attribute
[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);
test
- 注册COM
@echo
cd /d %~dp0
Regsvr32 xbfInfo.dll
- Debug版本
拷贝
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所在的目录
VB Project
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即可
Issues
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
仅安装ucrt(通用Windows sdk)会出现:
cannot find the resource compiler dll. please make sure the path is correct.
解决办法:
安装Windows 8.1/Windows 10 SDK