C#DLL不能从VB6应用程序的影响按引用传递的一些值 [英] C# DLL cannot affect value of a number passed by reference from a VB6 application
问题描述
我有一个调用DLL VB6的传统VB6应用程序,我想端口VB6 DLL到C#还没有触及的主要VB6应用code。旧的VB6的DLL不得不通过参考接收到VB6的长(32位整数)的一个接口,并更新该值。在我写的C#DLL,更新后的值永远不会由主VB6应用程序可见。它充当虽然什么真正编组到C#的DLL是对原始数据的一个副本,而不是对原始数据的基准的参考。我可以参考顺利通过阵列,并更新他们,但单值不行为。
C#的DLL code看起来是这样的:
[标记有ComVisible特性(真)]
公共接口IInteropDLL
{
无效增量(REF的Int32 NUM);
}
[标记有ComVisible特性(真)
公共类InteropDLL:IInteropDLL
{
公共无效增量(REF的Int32 NUM){NUM ++; }
}
调用VB6 code看起来是这样的:
私人DLL作为IInteropDLL
私人小组的Form_Load()
设置DLL =新InteropDLL
结束小组
私人小组TestLongReference()
昏暗NUM只要
NUM = 1
dll.Increment(NUM)
Debug.Print NUM打印1,而不是2.为什么?
结束小组
我是什么做错了吗?我会怎么做才能解决这个问题?
先谢谢了。
dll.Increment(NUM)
由于您使用括号,该值被强制按值传递的,而不是由引用(编译器创建一个临时的复制和传递的是的引用)。
删除括号:
dll.Increment NUM
编辑:通过的 =http://stackoverflow.com/users/15639 / markj> markJ 。
I have a legacy VB6 application that calls a VB6 DLL, and I am trying to port the VB6 DLL to C# without yet touching the main VB6 application code. The old VB6 DLL had one interface that received a VB6 long (32-bit integer) by reference, and updated the value. In the C# DLL I have written, the updated value is never seen by the main VB6 application. It acts as though what was really marshalled to the C# DLL was a reference to a copy of the original data, not a reference to the original data. I can successfully pass arrays by reference, and update them, but single values aren't behaving.
The C# DLL code looks something like this:
[ComVisible(true)]
public interface IInteropDLL
{
void Increment(ref Int32 num);
}
[ComVisible(true)]
public class InteropDLL : IInteropDLL
{
public void Increment(ref Int32 num) { num++; }
}
The calling VB6 code looks something like this:
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
What am I doing wrong? What would I have to do to fix it? Thanks in advance.
dll.Increment( num )
Because you are using parentheses, the value is forcibly passed by value, not by reference (the compiler creates a temporary copy and passes that by reference).
Remove the parentheses:
dll.Increment num
EDIT: A more complete explanation by MarkJ.
这篇关于C#DLL不能从VB6应用程序的影响按引用传递的一些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!