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

标签 c# vb6 vb6-migration

我有一个调用 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/

相关文章:

c# - 如何在双击时将当前选定的 DataGridView 项目发送到 ViewModel 中的命令

c# - REST Web API : How to make Content-Type header optional?

vb.net - block 注释 VB/VB.NET 代码

c# - 从 Visual Sourcesafe 重新编译旧版本的项目

c# - ASP.NET Core 在默认域 (*.azurewebsites.net) 上跨 Azure Web 应用程序共享身份 Cookie

c# - 如何将字符串转换为变量,vb6/c#/c++?

.net - 为什么我可以从浏览器访问 WSDL 文件,但不能从应用程序访问?

.net - VB6 .Net 互操作。将 .Net Date 属性传递给 VB6 函数 ByRef 未更新

vb.net - 如何卸载 VB.NET 中所有打开的表单?

java - 从 Java 随机访问 VB6 二进制数据