Problems with Interop in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Olbert

    Problems with Interop in C#

    Subject: Problems with Interop in C#

    We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
    Net2 using Vs2005. Below are the signatures of the method that is the
    problem. It is the last argument (e.g., "out obj" in C#) that is returned
    corrupted. It should be an array of integers. In one simple test App this
    argument does return an array of integers reliably. In the actual App it
    returns one or two integer arrays but then it starts returning corrupted
    data. The difference between the two App's is on of complexity and that the
    actual App uses many more threads then the test App.

    We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
    file.

    ANY suggestions would be GREATLY appreciated.

    C# signature--
    bool b=pdc.Get_Array Data(out Transfer_Comple te,out DataNum,out
    SamplingPeriod, out obj);

    IDL signature--
    [id(0x00000012), helpstring("met hod Get_ArrayData")]
    VARIANT_BOOL Get_ArrayData(
    [out] long* Transfer_Comple te,
    [out] long* DataNum,
    [out] double* SamplingPeriod,
    [out] VARIANT* arrayData);

    --
    John Olbert

  • Willy Denoyette [MVP]

    #2
    Re: Problems with Interop in C#


    "John Olbert" <someone@snet.n etwrote in message
    news:69EBC75F-79D8-4C35-A706-1DE5402AF5F7@mi crosoft.com...
    | Subject: Problems with Interop in C#
    |
    | We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
    | Net2 using Vs2005. Below are the signatures of the method that is the
    | problem. It is the last argument (e.g., "out obj" in C#) that is returned
    | corrupted. It should be an array of integers. In one simple test App this
    | argument does return an array of integers reliably. In the actual App it
    | returns one or two integer arrays but then it starts returning corrupted
    | data. The difference between the two App's is on of complexity and that
    the
    | actual App uses many more threads then the test App.
    |
    | We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
    | file.
    |
    | ANY suggestions would be GREATLY appreciated.
    |
    | C# signature--
    | bool b=pdc.Get_Array Data(out Transfer_Comple te,out DataNum,out
    | SamplingPeriod, out obj);
    |
    | IDL signature--
    | [id(0x00000012), helpstring("met hod Get_ArrayData")]
    | VARIANT_BOOL Get_ArrayData(
    | [out] long* Transfer_Comple te,
    | [out] long* DataNum,
    | [out] double* SamplingPeriod,
    | [out] VARIANT* arrayData);
    |
    | --
    | John Olbert
    |

    Make sure your threads are initialized for STA.

    Willy.


    Comment

    • fallenidol

      #3
      Re: Problems with Interop in C#

      as far as im aware you cannot use arrays by reference across the interop
      boundary

      --
      "If ignorance is bliss, then wipe the smile from my face."


      "Willy Denoyette [MVP]" wrote:
      >
      "John Olbert" <someone@snet.n etwrote in message
      news:69EBC75F-79D8-4C35-A706-1DE5402AF5F7@mi crosoft.com...
      | Subject: Problems with Interop in C#
      |
      | We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
      | Net2 using Vs2005. Below are the signatures of the method that is the
      | problem. It is the last argument (e.g., "out obj" in C#) that is returned
      | corrupted. It should be an array of integers. In one simple test App this
      | argument does return an array of integers reliably. In the actual App it
      | returns one or two integer arrays but then it starts returning corrupted
      | data. The difference between the two App's is on of complexity and that
      the
      | actual App uses many more threads then the test App.
      |
      | We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
      | file.
      |
      | ANY suggestions would be GREATLY appreciated.
      |
      | C# signature--
      | bool b=pdc.Get_Array Data(out Transfer_Comple te,out DataNum,out
      | SamplingPeriod, out obj);
      |
      | IDL signature--
      | [id(0x00000012), helpstring("met hod Get_ArrayData")]
      | VARIANT_BOOL Get_ArrayData(
      | [out] long* Transfer_Comple te,
      | [out] long* DataNum,
      | [out] double* SamplingPeriod,
      | [out] VARIANT* arrayData);
      |
      | --
      | John Olbert
      |
      >
      Make sure your threads are initialized for STA.
      >
      Willy.
      >
      >
      >

      Comment

      • Willy Denoyette [MVP]

        #4
        Re: Problems with Interop in C#


        "fallenidol " <fallenidol@dis cussions.micros oft.comwrote in message
        news:D6B3BA42-DC71-408E-8A99-7C7839553C31@mi crosoft.com...
        | as far as im aware you cannot use arrays by reference across the interop
        | boundary
        |
        | --
        | "If ignorance is bliss, then wipe the smile from my face."
        |


        Sure you can, the OP's case is a bit tricky as he is passing a VARIANT*, so
        what you need is to pass an object reference (effectively a VARIANT*).

        Consider following sample...

        ' SomeAXClass in VB6

        Function GetArray(o As Variant) As Boolean
        ' no bound checks in this sample!!!!!
        o(0) = 100
        o(1) = 200
        End Function

        // C#...
        ....
        SomeAXClass o = new SomeAXClass();
        object arr = new object[2];
        o.GetArray(ref arr);
        foreach(object i in arr as object[])
        Console.WriteLi ne(i.ToString() );
        ...

        The only problem I have with the OP's code is that I don't believe this is a
        VB6 AX dll, see my reply to his posting for details on why.


        Willy.


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: Problems with Interop in C#


          "John Olbert" <someone@snet.n etwrote in message
          news:69EBC75F-79D8-4C35-A706-1DE5402AF5F7@mi crosoft.com...
          | Subject: Problems with Interop in C#
          |
          | We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
          | Net2 using Vs2005. Below are the signatures of the method that is the
          | problem. It is the last argument (e.g., "out obj" in C#) that is returned
          | corrupted. It should be an array of integers. In one simple test App this
          | argument does return an array of integers reliably. In the actual App it
          | returns one or two integer arrays but then it starts returning corrupted
          | data. The difference between the two App's is on of complexity and that
          the
          | actual App uses many more threads then the test App.
          |
          | We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
          | file.
          |
          | ANY suggestions would be GREATLY appreciated.
          |
          | C# signature--
          | bool b=pdc.Get_Array Data(out Transfer_Comple te,out DataNum,out
          | SamplingPeriod, out obj);
          |
          | IDL signature--
          | [id(0x00000012), helpstring("met hod Get_ArrayData")]
          | VARIANT_BOOL Get_ArrayData(
          | [out] long* Transfer_Comple te,
          | [out] long* DataNum,
          | [out] double* SamplingPeriod,
          | [out] VARIANT* arrayData);
          |
          | --
          | John Olbert
          |

          Are you sure this (the above IDL) is from a VB6 AXCOM dll?, AFAIK VB6 can't
          pass an argumant as out, it passes arguments as [In, Out] or [In]. Returns
          are passed as [out, retval] and the IDL signature doesn't seem right for
          VB6, it should return an HRESULT
          If i was a VB6 AX object it's methods signature should look something like
          this:

          HRESULT GET_....([In, Out] long* ...., [in, out] VARIANT*, [in, retval]
          VARIANT_BOOL*);


          Willy.



          Comment

          • John Olbert

            #6
            Re: Problems with Interop in C#

            All we have is the raw Dll. Is there a way to find out which compiler was used?

            Thanks for the idea.
            --
            John Olbert



            "Willy Denoyette [MVP]" wrote:
            >
            "John Olbert" <someone@snet.n etwrote in message
            news:69EBC75F-79D8-4C35-A706-1DE5402AF5F7@mi crosoft.com...
            | Subject: Problems with Interop in C#
            |
            | We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
            | Net2 using Vs2005. Below are the signatures of the method that is the
            | problem. It is the last argument (e.g., "out obj" in C#) that is returned
            | corrupted. It should be an array of integers. In one simple test App this
            | argument does return an array of integers reliably. In the actual App it
            | returns one or two integer arrays but then it starts returning corrupted
            | data. The difference between the two App's is on of complexity and that
            the
            | actual App uses many more threads then the test App.
            |
            | We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
            | file.
            |
            | ANY suggestions would be GREATLY appreciated.
            |
            | C# signature--
            | bool b=pdc.Get_Array Data(out Transfer_Comple te,out DataNum,out
            | SamplingPeriod, out obj);
            |
            | IDL signature--
            | [id(0x00000012), helpstring("met hod Get_ArrayData")]
            | VARIANT_BOOL Get_ArrayData(
            | [out] long* Transfer_Comple te,
            | [out] long* DataNum,
            | [out] double* SamplingPeriod,
            | [out] VARIANT* arrayData);
            |
            | --
            | John Olbert
            |
            >
            Are you sure this (the above IDL) is from a VB6 AXCOM dll?, AFAIK VB6 can't
            pass an argumant as out, it passes arguments as [In, Out] or [In]. Returns
            are passed as [out, retval] and the IDL signature doesn't seem right for
            VB6, it should return an HRESULT
            If i was a VB6 AX object it's methods signature should look something like
            this:
            >
            HRESULT GET_....([In, Out] long* ...., [in, out] VARIANT*, [in, retval]
            VARIANT_BOOL*);
            >
            >
            Willy.
            >
            >
            >
            >

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: Problems with Interop in C#


              "John Olbert" <someone@snet.n etwrote in message
              news:AD8DCBC7-0203-4DB9-93D7-AA49C2048641@mi crosoft.com...
              | All we have is the raw Dll. Is there a way to find out which compiler was
              used?
              |

              Not really, all you can do to make sure it's a VB6 dll is run dumpbin.exe
              and check the imports, a VB6 PE must import the VB runtime (MSVBVM60.dll).
              PE files produced by other tools will not import the VB runtime, but you
              won't be able to tell what language compiler was used to build the file.

              Willy.


              Comment

              • John Olbert

                #8
                Re: Problems with Interop in C#

                Off hand how do you make sure that one's .NET2 threads initialized for STA?

                Thanks.
                --
                John Olbert



                "Willy Denoyette [MVP]" wrote:
                >
                "John Olbert" <someone@snet.n etwrote in message
                news:69EBC75F-79D8-4C35-A706-1DE5402AF5F7@mi crosoft.com...
                | Subject: Problems with Interop in C#
                |
                | We are having problems using Interop with a Vb6 ActiveX Dll in C# code in
                | Net2 using Vs2005. Below are the signatures of the method that is the
                | problem. It is the last argument (e.g., "out obj" in C#) that is returned
                | corrupted. It should be an array of integers. In one simple test App this
                | argument does return an array of integers reliably. In the actual App it
                | returns one or two integer arrays but then it starts returning corrupted
                | data. The difference between the two App's is on of complexity and that
                the
                | actual App uses many more threads then the test App.
                |
                | We use Tlbimp.exe in the PreBuild step. We do not have the Vb6 code or Pdb
                | file.
                |
                | ANY suggestions would be GREATLY appreciated.
                |
                | C# signature--
                | bool b=pdc.Get_Array Data(out Transfer_Comple te,out DataNum,out
                | SamplingPeriod, out obj);
                |
                | IDL signature--
                | [id(0x00000012), helpstring("met hod Get_ArrayData")]
                | VARIANT_BOOL Get_ArrayData(
                | [out] long* Transfer_Comple te,
                | [out] long* DataNum,
                | [out] double* SamplingPeriod,
                | [out] VARIANT* arrayData);
                |
                | --
                | John Olbert
                |
                >
                Make sure your threads are initialized for STA.
                >
                Willy.
                >
                >
                >

                Comment

                • Willy Denoyette [MVP]

                  #9
                  Re: Problems with Interop in C#


                  "John Olbert" <someone@snet.n etwrote in message
                  news:DE1DE031-76D4-4321-873E-A74058B23A0C@mi crosoft.com...
                  | Off hand how do you make sure that one's .NET2 threads initialized for
                  STA?
                  |
                  | Thanks.
                  | --
                  | John Olbert
                  |
                  |

                  When it's your main thread, you can apply the [STAThread] attribute to your
                  Main function.
                  When it's an auxiliary thread, initialize the thread instance as STA, using
                  the SetApartentStat e method before it is started.

                  Worker threads picked from the thread pool are MTA and cannot be changed.

                  Willy.



                  Comment

                  Working...