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

发布于 2024-12-14 16:23:29 字数 806 浏览 3 评论 0原文

我有一个调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2024-12-21 16:23:31
dll.Increment( num )

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

删除括号:

dll.Increment num

编辑:更完整的解释,作者:马克J

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文