gpt4 book ai didi

C# DLL 不能影响从 VB6 应用程序通过引用传递的数字的值

转载 作者:太空狗 更新时间:2023-10-30 00:27:06 24 4
gpt4 key购买 nike

我有一个调用 VB6 DLL 的遗留 VB6 应用程序,我正在尝试将 VB6 DLL 移植到 C# 而无需触及主要的 VB6 应用程序代码。旧的 VB6 DLL 有一个接口(interface),它通过引用接收一个 VB6 长整数(32 位整数),并更新该值。在我编写的 C# DLL 中,主 VB6 应用程序从未看到更新后的值。它的作用就好像真正编码到 C# DLL 的是对原始数据副本的引用,而不是对原始数据的引用。我可以通过引用成功传递数组,并更新它们,但单个值没有行为。

C# DLL 代码看起来像这样:

[ComVisible(true)]
public interface IInteropDLL
{
void Increment(ref Int32 num);
}
[ComVisible(true)]
public class InteropDLL : IInteropDLL
{
public void Increment(ref Int32 num) { num++; }
}

调用 VB6 代码如下所示:

Private dll As IInteropDLL
Private Sub Form_Load()
Set dll = New InteropDLL
End Sub
Private Sub TestLongReference()
Dim num As Long
num = 1
dll.Increment( num )
Debug.Print num ' prints 1, not 2. Why?
End Sub

我做错了什么?我需要做什么来修复它?提前致谢。

最佳答案

dll.Increment( num )

因为您使用的是括号,所以该值被强制按值传递,而不是按引用传递(编译器创建一个临时副本并按引用传递 that)。

去掉括号:

dll.Increment num

编辑:A more complete explanation通过 MarkJ .

关于C# DLL 不能影响从 VB6 应用程序通过引用传递的数字的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8070033/

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