VC6 ATL DLL interop with VB.NET

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

    #1

    VC6 ATL DLL interop with VB.NET

    I have an existing VC6 ATL COM DLL. It has a number of methods within it
    that take a byte array as the methods parameters. Here's what the IDL looks
    like in the VC6 DLL:

    [id(32), helpstring("met hod GetSystemConfig ")] HRESULT GetSystemConfig ([in,
    out] SAFEARRAY(unsig ned char) *pData, [out, retval] long *retval);

    As you can see it is a byte array (SAFEARRAY)

    In the VB.NET object browser the the prototype looks like this:

    Public Function GetSystemConfig (ByRef pData as System.Array) as Integer

    PROBLEM: Everyone of the API calls made in the following code section throws
    an error:
    "Specified array was not of the expected type. Err.Number = 5 "

    I currently have VB.NET up in the VC 6 debugger just to see if the interface
    ever gets called, and it does not. Which means my problem is in the interop
    dll that Dot net creates automatically.

    Public Function GetSystemConfig (ByRef pBytes As System.Array) As Integer
    On Error GoTo processerror

    Dim pByte() As Byte
    GetSystemConfig = (ESITapi.GetSys temConfig(pByte ) = S_OK)

    Dim pByt(512) As Byte
    GetSystemConfig = (ESITapi.GetSys temConfig(pByt) = S_OK)

    Dim chars As Byte() = System.Text.Enc oding.ASCII.Get Bytes("hello
    world")
    GetSystemConfig = (ESITapi.GetSys temConfig(chars ) = S_OK)

    Dim arr As Array

    GetSystemConfig = (ESITapi.GetSys temConfig(arr) = S_OK)

    Dim strArr() As String = {"1", "2", "3"}
    GetSystemConfig = (ESITapi.GetSys temConfig(strAr r) = S_OK)

    Dim pB(512) As Char
    GetSystemConfig = (ESITapi.GetSys temConfig(pB) = S_OK)

    End Function

    Is there any way I can debug VB.NETS interop DLL ?
    Any idea's what's wrong with the parameters I'm passing ?
    FYI, I'm using VS 2005
    This ATL control was written for use with VB6 and has been in production for
    years, I'm trying to reuse it in the Dot Net world.
    Anyone got any ideas ?

    Dave


  • Peter Huang [MSFT]

    #2
    RE: VC6 ATL DLL interop with VB.NET

    Hi Dave,

    From you description, I understand that you want to call ATL COM DLL in
    VB.NET. The COM DLL has been working with VB6 well.
    If I misunderstood, please feel free to let me know.

    I think so far can you provide the some code about what will you do in the
    method
    [id(32), helpstring("met hod GetSystemConfig ")] HRESULT GetSystemConfig ([in,
    out] SAFEARRAY(unsig ned char) *pData, [out, retval] long *retval);

    Also We have a wizard to upgrade VB6 application to VB.NET, I think you may
    try to use that Wizard to upgrade VB6 program to VB.NET to see if that
    works.

    Please NOTE that may need some tweak after convertion.

    You may have a try and let me know the result.

    If you still have any concern, please feel free to let me know.
    I look forward to hearing from you.

    Best regards,

    Peter Huang

    Microsoft Online Community Support
    =============== =============== =============== =====
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • Dave

      #3
      Re: VC6 ATL DLL interop with VB.NET

      Peter,

      I actually figured this problem out. It took me all day but I determined
      that the SafeArray type that I assigned it in the code and the type that I
      assigned to it in the IDL were different. This didn't cause any problems in
      VB6 but in VB.NET and C# when used with the .Net interop dll it is a
      problem.

      I'm not using VB6, I'm using VB.NET, there is no problems using the DLL with
      VB6 that's what it was initially used for. VB.NET and C# have problems with
      this dll.

      On another note, is there a way to debug the interop DLL ? Like generate a
      project for it for debugging purposes ?

      Here's what needs to be known by anyone that trys to do interop with an ATL
      COM control with SafeArrays as parameters in methods. This code sample was
      cut and pasted in but should provide a decent example.

      [id(32), helpstring("met hod GetSystemConfig ")] HRESULT GetSystemConfig ([in,
      out] SAFEARRAY(unsig ned char) *pData, [out, retval] long *retval);

      STDMETHODIMP CATLTapi::GetSy stemConfig(SAFE ARRAY **pData, long *retval)
      {
      SAFEARRAY* pAr = NULL;
      SAFEARRAYBOUND rgsArrayBound[1];

      rgsArrayBound[0].Lbound = 0;
      rgsArrayBound[0].cElements = 50; // Array length
      pAr = SafeArrayCreate (VT_UI1,1,rgsAr rayBound);

      // Pointer to the pAr's data section
      LPVOID pV;
      HRESULT RES;
      RES = SafeArrayAccess Data(pAr,&pV);

      if(RES)
      {
      LogIt("OnDevSpe cific() SafeArrayAccess Data Error!");
      break;
      }


      memcpy(pV,"1234 567890123456789 012345678901234 567890123456789 0",50);

      SafeArrayUnacce ssData(pAr);

      *pData = pAr;
      .....


      Dave



      ""Peter Huang" [MSFT]" <v-phuang@online.m icrosoft.com> wrote in message
      news:CZoC8vsXGH A.4620@TK2MSFTN GXA01.phx.gbl.. .[color=blue]
      > Hi Dave,
      >
      > From you description, I understand that you want to call ATL COM DLL in
      > VB.NET. The COM DLL has been working with VB6 well.
      > If I misunderstood, please feel free to let me know.
      >
      > I think so far can you provide the some code about what will you do in the
      > method
      > [id(32), helpstring("met hod GetSystemConfig ")] HRESULT[/color]
      GetSystemConfig ([in,[color=blue]
      > out] SAFEARRAY(unsig ned char) *pData, [out, retval] long *retval);
      >
      > Also We have a wizard to upgrade VB6 application to VB.NET, I think you[/color]
      may[color=blue]
      > try to use that Wizard to upgrade VB6 program to VB.NET to see if that
      > works.
      >
      > Please NOTE that may need some tweak after convertion.
      >
      > You may have a try and let me know the result.
      >
      > If you still have any concern, please feel free to let me know.
      > I look forward to hearing from you.
      >
      > Best regards,
      >
      > Peter Huang
      >
      > Microsoft Online Community Support
      > =============== =============== =============== =====
      > When responding to posts, please "Reply to Group" via your newsreader so
      > that others may learn and benefit from your issue.
      > =============== =============== =============== =====
      > This posting is provided "AS IS" with no warranties, and confers no[/color]
      rights.[color=blue]
      >[/color]


      Comment

      • Peter Huang [MSFT]

        #4
        Re: VC6 ATL DLL interop with VB.NET

        Hi Dave,

        From your description, you have resolved the issue, if I have any
        misunderstandin g please feel free to post here.
        I know that your solution worked in VB6, because we can upgrade VB6 Project
        into VB.NET project, so I suggest you have a try.

        To debug an application, we need the symbols otherwisse we will only see if
        the assembly code. Unfortunately , the IDE generated Interop Assembly
        neither generated Symbols(commonl y it is pdb file) nor source code. So we
        can not debug into it.
        But if you have both .NET client source code and C++ source code, we can
        debug into the C++ code when we try to call into C++ aproach from .NET.


        Best regards,

        Peter Huang

        Microsoft Online Community Support
        =============== =============== =============== =====
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        • Dave

          #5
          Re: VC6 ATL DLL interop with VB.NET

          I have already upgraded the VB6 project to VB.NET, but I still use it as a
          reference point.

          I know how to debug the C++ code, I actually launch VS 2005 from the VS 6
          debugger.

          It's too bad the interop can't be debugged seems like there could be some
          value in that.


          ""Peter Huang" [MSFT]" <v-phuang@online.m icrosoft.com> wrote in message
          news:mQ6cqm5XGH A.6000@TK2MSFTN GXA01.phx.gbl.. .[color=blue]
          > Hi Dave,
          >
          > From your description, you have resolved the issue, if I have any
          > misunderstandin g please feel free to post here.
          > I know that your solution worked in VB6, because we can upgrade VB6[/color]
          Project[color=blue]
          > into VB.NET project, so I suggest you have a try.
          >
          > To debug an application, we need the symbols otherwisse we will only see[/color]
          if[color=blue]
          > the assembly code. Unfortunately , the IDE generated Interop Assembly
          > neither generated Symbols(commonl y it is pdb file) nor source code. So we
          > can not debug into it.
          > But if you have both .NET client source code and C++ source code, we can
          > debug into the C++ code when we try to call into C++ aproach from .NET.
          >
          >
          > Best regards,
          >
          > Peter Huang
          >
          > Microsoft Online Community Support
          > =============== =============== =============== =====
          > When responding to posts, please "Reply to Group" via your newsreader so
          > that others may learn and benefit from your issue.
          > =============== =============== =============== =====
          > This posting is provided "AS IS" with no warranties, and confers no[/color]
          rights.[color=blue]
          >[/color]


          Comment

          • Peter Huang [MSFT]

            #6
            Re: VC6 ATL DLL interop with VB.NET

            Hi Dave,

            As I said before, the IDE did not generate Symbol file and source file for
            the Interop Assembly so the any debugger did not know that the code is.
            If you do need to behavior, you have to create the Interop Assembly
            manually so that it is a common class library coded by yourself.
            How to: Create Wrappers Manually
            http://msdn2.microsoft.com/en-us/lib...00(VS.80).aspx

            But we may still need the symbols for the CLR itself.

            Commonly the IDE will generate correct Wrap assembly for us, if you did not
            want the generated Assembly, we have to create one ourselves per the link
            above.

            Anyway, there is a detailed COM Interop Referece Book, you may have a check.
            ..NET and COM: The Complete Interoperabilit y Guide (Paperback)
            by Adam Nathan
            http://www.amazon.com/gp/product/067...3?v=glance&n=2
            83155

            If you still have any concern, please feel free to post here.

            Best regards,

            Peter Huang

            Microsoft Online Community Support
            =============== =============== =============== =====
            When responding to posts, please "Reply to Group" via your newsreader so
            that others may learn and benefit from your issue.
            =============== =============== =============== =====
            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            Working...