0

I have a VB class library I built from an existing VB class which wraps an unmanaged DLL. The VB class library contains the DLL functions and various structs and types associated with the DLL functions.

I am using the class lib in a C# project and one of the functions in the class lib requires me to pass a struct as an argument. This is where I am running into trouble.

Here is the VB code for the DLL:

Declare Auto Function CtSetVRegister Lib "Ctccom32v2.dll" _
        (ByVal ConnectID As Integer, ByRef Storage As CT_VARIANT) As Integer

Here is the VB struct:

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure CT_VARIANT
    Dim vRegister As Integer          'Variant Register desired
    Dim type As Integer               'Format want results returned in
    Dim precision As Integer          'Precision desired for floating point conversions
    Dim flags As Integer              'Specially defined flags, 0 for normal, (indirection, etc.)
    Dim cmd As Integer                'Special commands, 0 for normal operation
    Dim taskHandle As Integer         'Alternate task handle for local task register access, 0 = default public
    Dim slength As Integer            'Length of bytes returned in stringVar, not include null
    Dim indexCol As Integer             'Column (X) selection, base 0
    Dim indexRow As Integer             'Row (X) selection base 0
    Dim IntegerIntVar As Integer         '32 bit signed integer storage
    Dim FloatVar As Single         '32 bit float
    Dim DoubleVar As Double        '64 bit double in Microsoft format
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=223)> _
    Public stringVar() As Byte         'null terminated ASCII string of bytes (1 to 224)
End Structure

The C# method I am writing requires me to set the necessary values in the struct and then pass those to the DLL function:

private void btnWriteVReg_Click(object sender, System.EventArgs e) 
{
        int results;
        CTC_Lib.Ctccom32v2.CT_VARIANT Var;            
        Var.vRegister = int.Parse(txtVRegToRead.Text);
        Var.cmd = 0;
        Var.flags = 0;
        Var.FloatVar = 0;
        Var.IntegerIntVar = 0;
        Var.DoubleVar = 0;            
        Var.precision = 6;            
        writeStatus.Text = "";

        Var.type = CTC_Lib.Ctccom32v2.CT_VARIANT_INTEGER;
        Var.IntegerIntVar = Convert.ToInt32(txtVRegVal.Text);            

        Var.taskHandle = 0;
        results = CTC_Lib.Ctccom32v2.CtSetVRegister(CTconnection,ref Var);
        if ((results == SUCCESS)) 
        {
            writeStatus.Text = "SUCCESS";
        }
        else 
        {
            writeStatus.Text = "ERROR";
        }
    }

I get the error:

Use of unassigned local variable 'Var'

I am a bit puzzled as to how to properly pass the struct 'Var' to the VB Class library.

4

2 回答 2

1

初始化变量

CTC_Lib.Ctccom32v2.CT_VARIANT Var = new CTC_Lib.Ctccom32v2.CT_VARIANT();
于 2012-07-02T19:34:05.900 回答
0

您必须创建 Var 的实例,

CTC_Lib.Ctccom32v2.CT_VARIANT Var = new CTC_Lib.Ctccom32v2.CT_VARIANT();
于 2012-07-02T19:34:21.607 回答