Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#DLL不能影响从VB6应用程序通过引用传递的数字的值_C#_Vb6_Vb6 Migration - Fatal编程技术网

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

C#DLL不能影响从VB6应用程序通过引用传递的数字的值,c#,vb6,vb6-migration,C#,Vb6,Vb6 Migration,我有一个调用VB6 DLL的传统VB6应用程序,我正在尝试将VB6 DLL移植到C#,而不涉及VB6应用程序的主代码。旧的VB6 DLL有一个接口,通过引用接收VB6长(32位整数),并更新该值。在我编写的C#DLL中,主VB6应用程序从未看到更新后的值。这就好像真正封送到C#DLL的是对原始数据副本的引用,而不是对原始数据的引用。我可以通过引用成功地传递数组,并更新它们,但单个值不起作用 C#DLL代码如下所示: [ComVisible(true)] public interface IInt

我有一个调用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++; }
}
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
调用VB6的代码如下所示:

[ComVisible(true)]
public interface IInteropDLL
{
   void Increment(ref Int32 num);
}
[ComVisible(true)]
public class InteropDLL : IInteropDLL
{
    public void Increment(ref Int32 num) { num++; }
}
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


编辑:按。

打得好。绝对是VB6的一个更古怪的特性。这样做是有道理的,但需要一些想象力。默认情况下,参数传递是ByRef。就是这样!哇!我从来不知道VB6的这种行为。现在我知道了这一点,我可以想起我被咬过的其他时候,但我只是找到了其他的解决办法。谢谢哦,请复制并粘贴代码示例,不要重写/混淆,因为它可能会隐藏这样的奇怪之处/错误。本例中的妄想行为会在您使用的调用中发生变化。我总是使用callmymethed(Param)约定来避免这种求值行为。另外,如果调用函数时没有看到偏执,我过去使用的C和Java会感到不安:)如果您看到方法调用和第一个括号
MethodCall(“foo”)
-这是您调查问题的队列-要么删除括号,要么添加
call
。一段时间后,发现这一点成为第二天性!由于C#允许您直接控制ClassID,因此您可以完全模拟odl COM接口,因此也不需要在客户端进行任何更改。