C# DLL 无法影响从 VB6 应用程序通过引用传递的数字值
我有一个调用 VB6 DLL 的旧版 VB6 应用程序,并且我正在尝试将 VB6 DLL 移植到 C#,但尚未接触主 VB6 应用程序代码。旧的 VB6 DLL 有一个接口,该接口通过引用接收 VB6 long(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
我做错了什么?我需要做什么来修复它? 提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您使用括号,所以该值强制按值传递,而不是按引用传递(编译器创建一个临时副本并按引用传递该)。
删除括号:
编辑:更完整的解释,作者:马克J。
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:
EDIT: A more complete explanation by MarkJ.