gpt4 book ai didi

c++ - 使用 COM 互操作时,为什么后期绑定(bind)会修复 "ByRef value type parameter cannot be null"错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:59:24 24 4
gpt4 key购买 nike

我在 VB.NET 项目中使用互操作 dll 来访问 MFC 项目中的某些函数。

Dim bIsSupposedToWork As Boolean = True
Dim api As API = GetObject(, "MyProject.API")

api.Function1() ' << Function1 returns void, and this works OK

' Function2 returns a long:
If Not bIsSupposedToWork Then
' This next line crashes the application with the message "ByRef value type parameter cannot be null"- error
api.Function2()
Else
' But this works fine:
Dim o As Object = api
o.Function2()
End If

为什么?

最佳答案

你在愚弄编译器,阻止它检查你是否编写了正确的代码。 Object.Foo() 调用是后期绑定(bind)的,它会在运行时整理出应该调用哪个确切的函数。编译器不能做任何检查,它不知道对象支持什么方法。底层管道是 COM 的 IDispatch 接口(interface)。

也许不用说,这并没有解决任何问题,它只是在运行时发出一声巨响。

不确定为什么要这样做,也许 MFC 项目中的 .idl 缺少函数返回值的 [out,retval] 属性。

关于c++ - 使用 COM 互操作时,为什么后期绑定(bind)会修复 "ByRef value type parameter cannot be null"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4669341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com