2010-01-10 68 views
1

我一直试图让下面的VB代码在C#中运行数小时。我在CreateStroke()调用中不断收到Value does not fall within the expected range.异常。 此外,here也是带有C++版本的Microsoft文档。帮我将下面的VB/C++代码转换为C#

Option Explicit 
Dim theInkCollector As InkCollector 

Private Sub Form_Load() 
    Set theInkCollector = New InkCollector 
    theInkCollector.hWnd = Me.hWnd 
    theInkCollector.Enabled = True 

    //Create a set of three points, stored as x1, y1, x2, y2, x3, y3 
    //in an array of six Long elements, starting at index 0. 
    Dim ptStrokePoints(5) As Long 
    ptStrokePoints(0) = 200 
    ptStrokePoints(1) = 200 
    ptStrokePoints(2) = 400 
    ptStrokePoints(3) = 600 
    ptStrokePoints(4) = 900 
    ptStrokePoints(5) = 300 

    //The description value is an unused placeholder. 
    Dim theDescription As Variant 
    Dim theStroke As IInkStrokeDisp 
    Set theStroke = theInkCollector.Ink.CreateStroke(ptStrokePoints, theDescription) 
End Sub 

以下是我有:

MSINKAUTLib.InkCollector collector = new MSINKAUTLib.InkCollector(); 
collector.hWnd = (int)(this.Handle); 
collector.Enabled = true; 

long[] pts = new long[6]; 
pts[0] = 200; 
pts[1] = 200; 
pts[2] = 400; 
pts[3] = 600; 
pts[4] = 900; 
pts[5] = 300; 

collector.Ink.CreateStroke(pts, new object()); 
+1

“CreateStroke”的第一个参数需要int []而不是'long []'。 – Seth 2010-01-10 21:35:07

+0

我尝试过int [],它仍然会显示“值不在预期的范围内”。 – Mark 2010-01-10 22:00:53

回答

2

这看起来像是从文档以下错误:

E_INVALIDARG - 无效VARIANT型(仅VT_ARRAY | VT_I4支持)。

C#long类型是一个64位整数,所以你传递一个VT_ARRAY | VT_I8(不是VT_I4)。

更改你的点声明:

int[] pts = new int[6]; 

,你应该是好去。 (int是C#的32位整数类型。)

+0

是的,我已经尝试过...没有运气= =( – Mark 2010-01-10 21:58:32

+0

嗯,很奇怪,我猜这是隐约可能的保留参数正在验证的某种方式:传递'null'而不是'new object()'help?另外,我想根据函数的导入方式,编组可能会出错,尽管在这种情况下我会期待一个不同的错误。你能发布tlbimp'ed方法的签名,包括任何MarshalAs属性吗?例外是ArgumentException对不对, – itowlson 2010-01-10 22:23:12

+0

它不是一个ArgumentException,并且没有将它设置为null是我尝试的第一件事...我真的不明白为什么它会抱怨,我有一种感觉,因为描述需要有效。 – Mark 2010-01-10 22:47:47