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

我有一个调用VB6 DLL的遗留VB6应用程序,我试图将VB6 DLL移植到C#而不接触主VB6应用程序代码。 旧的VB6 DLL有一个接口通过引用接收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 ) 

因为您正在使用括号,所以值会被值强制传递,而不是通过引用传递(编译器会创建临时副本并通过引用传递 )。

删除括号:

 dll.Increment num 

编辑: MarkJ的 更完整的解释 。