How to convert a Byte() to an IntPtr in VB

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

    How to convert a Byte() to an IntPtr in VB

    My application, written in .NET VB, tries to get a communication port handle
    from a TAPI object with this code:

    Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")

    The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How
    can I convert this Byte() in this IntPtr ?
  • Serge BRIC

    #2
    RE: How to convert a Byte() to an IntPtr in VB

    Sorry for this double message (I thought the previous one had failed).

    Comment

    • TDC

      #3
      Re: How to convert a Byte() to an IntPtr in VB

      The Marshal class (in System.Runtime. Interop) has mothods to convert
      (copy actually) to and from unmanaged memory. If you browse the
      methods there, you'll quickly find what you are looking for in response
      to this and your other similar posted question.

      Tom


      Serge BRIC wrote:
      My application, written in .NET VB, tries to get a communication port handle
      from a TAPI object with this code:
      >
      Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
      >
      The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How
      can I convert this Byte() in this IntPtr ?

      Comment

      • Serge BRIC

        #4
        Re: How to convert a Byte() to an IntPtr in VB

        I have obtained the TAPI3 file handle with VB and C++ code, like this:

        Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
        Dim iFileHandle As IntPtr
        iFileHandle = BytePtr2Handle( vFileHandle)

        The C++ BytePtr2Handle( ) is :

        __declspec(dlle xport) HANDLE BytePtr2Handle( BYTE* p)
        {
        return *((LPHANDLE)p);
        }

        I'm certain it could be done, in a simpler way, in VB (without C++ code) but
        I don't know how.

        "TDC" wrote:
        The Marshal class (in System.Runtime. Interop) has mothods to convert
        (copy actually) to and from unmanaged memory. If you browse the
        methods there, you'll quickly find what you are looking for in response
        to this and your other similar posted question.
        >
        Tom
        >
        >
        Serge BRIC wrote:
        My application, written in .NET VB, tries to get a communication port handle
        from a TAPI object with this code:

        Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")

        The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How
        can I convert this Byte() in this IntPtr ?
        >
        >

        Comment

        • Jay B. Harlow

          #5
          Re: How to convert a Byte() to an IntPtr in VB

          Serge,
          Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
          Why is GetIDAsVariant returning a Byte()? It appears that it really should
          return a COM VARIANT, in the .NET world COM Variants are exposed as
          System.Object.

          As TDC suggested: System.Runtime. Interop.Marshal is your friend here. It has
          methods to marshal (convert/translate) between the COM/C++ (unmanaged) world
          & the .NET (managed) world... However it helps to define the methods &
          interfaces that you use first, as P/Invoke (how you define the method) is
          your first "defense" on converting unmanaged types.


          FWIW The name GetIDAsVariant, suggests it returns a COM Variant, a Google
          search reinforces this notion. I normally check www.pinvoke.net for the
          correct definitions of unmanaged interfaces & methods that I want to use in
          the managed world.

          --
          Hope this helps
          Jay B. Harlow
          ..NET Application Architect, Enthusiast, & Evangelist
          T.S. Bradley - http://www.tsbradley.net


          "Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
          news:0BE821CE-C60D-4438-BAAF-5E876666E541@mi crosoft.com...
          My application, written in .NET VB, tries to get a communication port
          handle
          from a TAPI object with this code:
          >
          Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
          >
          The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
          How
          can I convert this Byte() in this IntPtr ?

          Comment

          • Serge BRIC

            #6
            Re: How to convert a Byte() to an IntPtr in VB

            Jay,

            You're right, GetIDAsVariant( ) returns a COM Variant in C++ exposed as a
            System.Object in .NET. It appears that the type of this .NET object is
            System.Array and that the type of each item of this array is a byte. This is
            why I was trying to interpret it as an array of bytes (Byte()).

            I found on the TAPI newsgroup that the correct way of getting the com port
            handle in C++ from a ITLegacyCallMed iaControl object (TAPI3 object) was
            something like :

            BSTR bstrComm = ::SysAllocStrin g(L"comm/datamodem");
            BYTE *pDeviceID;
            DWORD dwSize;
            ITLegacyCallMed iaControl *pCallMediaCont rol;
            ...
            HRESULT hr = pCallMediaContr ol->GetID(bstrComm , &dwSize, &pDeviceID);
            HANDLE handle = *((LPHANDLE)pDe viceID);

            So, in VB, it is:

            Dim appel As ITLegacyCallMed iaControl2
            ...
            Dim obj As Object = callMediaContro l.GetIDAsVarian t("comm/datamodem")
            Dim handle As IntPtr = Marshal.ReadInt Ptr(obj, 0)

            Thanks for your help.


            "Jay B. Harlow" wrote:
            Serge,
            Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
            Why is GetIDAsVariant returning a Byte()? It appears that it really should
            return a COM VARIANT, in the .NET world COM Variants are exposed as
            System.Object.
            >
            As TDC suggested: System.Runtime. Interop.Marshal is your friend here. It has
            methods to marshal (convert/translate) between the COM/C++ (unmanaged) world
            & the .NET (managed) world... However it helps to define the methods &
            interfaces that you use first, as P/Invoke (how you define the method) is
            your first "defense" on converting unmanaged types.
            >
            >
            FWIW The name GetIDAsVariant, suggests it returns a COM Variant, a Google
            search reinforces this notion. I normally check www.pinvoke.net for the
            correct definitions of unmanaged interfaces & methods that I want to use in
            the managed world.
            >
            --
            Hope this helps
            Jay B. Harlow
            .NET Application Architect, Enthusiast, & Evangelist
            T.S. Bradley - http://www.tsbradley.net
            >
            >
            "Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
            news:0BE821CE-C60D-4438-BAAF-5E876666E541@mi crosoft.com...
            My application, written in .NET VB, tries to get a communication port
            handle
            from a TAPI object with this code:

            Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")

            The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
            How
            can I convert this Byte() in this IntPtr ?
            >

            Comment

            • Jay B. Harlow

              #7
              Re: How to convert a Byte() to an IntPtr in VB

              Serge,
              I would not expect a Handle to be returned as a Byte().

              I would expect a Handle to be returned as an Integer...

              How is your ITLegacyCallMed iaControl2 defined?


              --
              Hope this helps
              Jay B. Harlow
              ..NET Application Architect, Enthusiast, & Evangelist
              T.S. Bradley - http://www.tsbradley.net


              "Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
              news:BAEF8A64-1BDF-4D86-9340-B2B5A4E975A9@mi crosoft.com...
              Jay,
              >
              You're right, GetIDAsVariant( ) returns a COM Variant in C++ exposed as a
              System.Object in .NET. It appears that the type of this .NET object is
              System.Array and that the type of each item of this array is a byte. This
              is
              why I was trying to interpret it as an array of bytes (Byte()).
              >
              I found on the TAPI newsgroup that the correct way of getting the com port
              handle in C++ from a ITLegacyCallMed iaControl object (TAPI3 object) was
              something like :
              >
              BSTR bstrComm = ::SysAllocStrin g(L"comm/datamodem");
              BYTE *pDeviceID;
              DWORD dwSize;
              ITLegacyCallMed iaControl *pCallMediaCont rol;
              ...
              HRESULT hr = pCallMediaContr ol->GetID(bstrComm , &dwSize, &pDeviceID);
              HANDLE handle = *((LPHANDLE)pDe viceID);
              >
              So, in VB, it is:
              >
              Dim appel As ITLegacyCallMed iaControl2
              ...
              Dim obj As Object =
              callMediaContro l.GetIDAsVarian t("comm/datamodem")
              Dim handle As IntPtr = Marshal.ReadInt Ptr(obj, 0)
              >
              Thanks for your help.
              >
              >
              "Jay B. Harlow" wrote:
              >
              >Serge,
              Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
              >Why is GetIDAsVariant returning a Byte()? It appears that it really
              >should
              >return a COM VARIANT, in the .NET world COM Variants are exposed as
              >System.Objec t.
              >>
              >As TDC suggested: System.Runtime. Interop.Marshal is your friend here. It
              >has
              >methods to marshal (convert/translate) between the COM/C++ (unmanaged)
              >world
              >& the .NET (managed) world... However it helps to define the methods &
              >interfaces that you use first, as P/Invoke (how you define the method) is
              >your first "defense" on converting unmanaged types.
              >>
              >>
              >FWIW The name GetIDAsVariant, suggests it returns a COM Variant, a Google
              >search reinforces this notion. I normally check www.pinvoke.net for the
              >correct definitions of unmanaged interfaces & methods that I want to use
              >in
              >the managed world.
              >>
              >--
              >Hope this helps
              >Jay B. Harlow
              >.NET Application Architect, Enthusiast, & Evangelist
              >T.S. Bradley - http://www.tsbradley.net
              >>
              >>
              >"Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
              >news:0BE821C E-C60D-4438-BAAF-5E876666E541@mi crosoft.com...
              My application, written in .NET VB, tries to get a communication port
              handle
              from a TAPI object with this code:
              >
              Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
              >
              The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
              How
              can I convert this Byte() in this IntPtr ?
              >>

              Comment

              • Serge BRIC

                #8
                Re: How to convert a Byte() to an IntPtr in VB

                Jay,

                ITLegacyCallMed iaControl2 is an extension of the ITLegacyCallMed iaControl
                which is one of the interfaces of the TAPI3 call object created when an
                address connects to another one. GetIDAsVariant( ) is a method of the
                ITLegacyCallMed iaControl2 interface (in C++, it is
                ITLegacyCallMed iaControl::GetI D()).

                I don't understand either why the return of
                ITLegacyCallMed iaControl2::Get IDAsVariant() is so opaque. It seems like it's
                a low level function.

                Here are some pieces of the related code :

                Dim tapi As ITTAPI
                Dim adresse As ITAddress
                Dim appel As ITBasicCallCont rol
                Dim telephoneNumber as String

                'Create the tapi object.
                tapi = New TAPI
                'Call Initialize before calling any other TAPI function.
                Call tapi.Initialize ()
                ...
                'adresse is one of the tapi.Addresses
                ...
                'create a data call to a phone number
                appel = adresse.CreateC all(telephoneNu mber,
                TapiConstants.L INEADDRESSTYPE_ PHONENUMBER,
                TapiConstants.T APIMEDIATYPE_DA TAMODEM)
                appel.Connect(F alse)
                ...
                'gets the TAPI3 comm port name
                Dim portName As Byte() =
                appel.GetIDAsVa riant("comm/datamodem/portname")
                'portName has to be converted to a String, each byte becoming a Char
                of the String

                'gets the TAPI3 comm port handle
                Dim obj As Object = appel.GetIDAsVa riant("comm/datamodem")
                Dim hPort As IntPtr = Marshal.ReadInt Ptr(obj, 0)

                'now, you can use this handle in Win32 WriteFile(), ReadFile() calls
                ...

                "Jay B. Harlow" wrote:
                Serge,
                I would not expect a Handle to be returned as a Byte().
                >
                I would expect a Handle to be returned as an Integer...
                >
                How is your ITLegacyCallMed iaControl2 defined?
                >
                >
                --
                Hope this helps
                Jay B. Harlow
                .NET Application Architect, Enthusiast, & Evangelist
                T.S. Bradley - http://www.tsbradley.net
                >
                >
                "Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
                news:BAEF8A64-1BDF-4D86-9340-B2B5A4E975A9@mi crosoft.com...
                Jay,

                You're right, GetIDAsVariant( ) returns a COM Variant in C++ exposed as a
                System.Object in .NET. It appears that the type of this .NET object is
                System.Array and that the type of each item of this array is a byte. This
                is
                why I was trying to interpret it as an array of bytes (Byte()).

                I found on the TAPI newsgroup that the correct way of getting the com port
                handle in C++ from a ITLegacyCallMed iaControl object (TAPI3 object) was
                something like :

                BSTR bstrComm = ::SysAllocStrin g(L"comm/datamodem");
                BYTE *pDeviceID;
                DWORD dwSize;
                ITLegacyCallMed iaControl *pCallMediaCont rol;
                ...
                HRESULT hr = pCallMediaContr ol->GetID(bstrComm , &dwSize, &pDeviceID);
                HANDLE handle = *((LPHANDLE)pDe viceID);

                So, in VB, it is:

                Dim appel As ITLegacyCallMed iaControl2
                ...
                Dim obj As Object =
                callMediaContro l.GetIDAsVarian t("comm/datamodem")
                Dim handle As IntPtr = Marshal.ReadInt Ptr(obj, 0)

                Thanks for your help.


                "Jay B. Harlow" wrote:
                Serge,
                Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
                Why is GetIDAsVariant returning a Byte()? It appears that it really
                should
                return a COM VARIANT, in the .NET world COM Variants are exposed as
                System.Object.
                >
                As TDC suggested: System.Runtime. Interop.Marshal is your friend here. It
                has
                methods to marshal (convert/translate) between the COM/C++ (unmanaged)
                world
                & the .NET (managed) world... However it helps to define the methods &
                interfaces that you use first, as P/Invoke (how you define the method) is
                your first "defense" on converting unmanaged types.
                >
                >
                FWIW The name GetIDAsVariant, suggests it returns a COM Variant, a Google
                search reinforces this notion. I normally check www.pinvoke.net for the
                correct definitions of unmanaged interfaces & methods that I want to use
                in
                the managed world.
                >
                --
                Hope this helps
                Jay B. Harlow
                .NET Application Architect, Enthusiast, & Evangelist
                T.S. Bradley - http://www.tsbradley.net
                >
                >
                "Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
                news:0BE821CE-C60D-4438-BAAF-5E876666E541@mi crosoft.com...
                My application, written in .NET VB, tries to get a communication port
                handle
                from a TAPI object with this code:

                Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")

                The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
                How
                can I convert this Byte() in this IntPtr ?
                >
                >

                Comment

                • Jay B. Harlow

                  #9
                  Re: How to convert a Byte() to an IntPtr in VB

                  Serge,
                  I know what it is. :-|

                  I wanted to see your VB.NET definition of ITLegacyCallMed iaControl2!

                  I wanted to make sure you had that defined correctly...

                  --
                  Hope this helps
                  Jay B. Harlow
                  ..NET Application Architect, Enthusiast, & Evangelist
                  T.S. Bradley - http://www.tsbradley.net


                  "Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
                  news:C098F5A6-1C85-4665-8A0C-ECC323E70783@mi crosoft.com...
                  Jay,
                  >
                  ITLegacyCallMed iaControl2 is an extension of the ITLegacyCallMed iaControl
                  which is one of the interfaces of the TAPI3 call object created when an
                  address connects to another one. GetIDAsVariant( ) is a method of the
                  ITLegacyCallMed iaControl2 interface (in C++, it is
                  ITLegacyCallMed iaControl::GetI D()).
                  >
                  I don't understand either why the return of
                  ITLegacyCallMed iaControl2::Get IDAsVariant() is so opaque. It seems like
                  it's
                  a low level function.
                  >
                  Here are some pieces of the related code :
                  >
                  Dim tapi As ITTAPI
                  Dim adresse As ITAddress
                  Dim appel As ITBasicCallCont rol
                  Dim telephoneNumber as String
                  >
                  'Create the tapi object.
                  tapi = New TAPI
                  'Call Initialize before calling any other TAPI function.
                  Call tapi.Initialize ()
                  ...
                  'adresse is one of the tapi.Addresses
                  ...
                  'create a data call to a phone number
                  appel = adresse.CreateC all(telephoneNu mber,
                  TapiConstants.L INEADDRESSTYPE_ PHONENUMBER,
                  TapiConstants.T APIMEDIATYPE_DA TAMODEM)
                  appel.Connect(F alse)
                  ...
                  'gets the TAPI3 comm port name
                  Dim portName As Byte() =
                  appel.GetIDAsVa riant("comm/datamodem/portname")
                  'portName has to be converted to a String, each byte becoming a
                  Char
                  of the String
                  >
                  'gets the TAPI3 comm port handle
                  Dim obj As Object = appel.GetIDAsVa riant("comm/datamodem")
                  Dim hPort As IntPtr = Marshal.ReadInt Ptr(obj, 0)
                  >
                  'now, you can use this handle in Win32 WriteFile(), ReadFile()
                  calls
                  ...
                  >
                  "Jay B. Harlow" wrote:
                  >
                  >Serge,
                  >I would not expect a Handle to be returned as a Byte().
                  >>
                  >I would expect a Handle to be returned as an Integer...
                  >>
                  >How is your ITLegacyCallMed iaControl2 defined?
                  >>
                  >>
                  >--
                  >Hope this helps
                  >Jay B. Harlow
                  >.NET Application Architect, Enthusiast, & Evangelist
                  >T.S. Bradley - http://www.tsbradley.net
                  >>
                  >>
                  >"Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
                  >news:BAEF8A6 4-1BDF-4D86-9340-B2B5A4E975A9@mi crosoft.com...
                  Jay,
                  >
                  You're right, GetIDAsVariant( ) returns a COM Variant in C++ exposed as
                  a
                  System.Object in .NET. It appears that the type of this .NET object is
                  System.Array and that the type of each item of this array is a byte.
                  This
                  is
                  why I was trying to interpret it as an array of bytes (Byte()).
                  >
                  I found on the TAPI newsgroup that the correct way of getting the com
                  port
                  handle in C++ from a ITLegacyCallMed iaControl object (TAPI3 object) was
                  something like :
                  >
                  BSTR bstrComm = ::SysAllocStrin g(L"comm/datamodem");
                  BYTE *pDeviceID;
                  DWORD dwSize;
                  ITLegacyCallMed iaControl *pCallMediaCont rol;
                  ...
                  HRESULT hr = pCallMediaContr ol->GetID(bstrComm , &dwSize, &pDeviceID);
                  HANDLE handle = *((LPHANDLE)pDe viceID);
                  >
                  So, in VB, it is:
                  >
                  Dim appel As ITLegacyCallMed iaControl2
                  ...
                  Dim obj As Object =
                  callMediaContro l.GetIDAsVarian t("comm/datamodem")
                  Dim handle As IntPtr = Marshal.ReadInt Ptr(obj, 0)
                  >
                  Thanks for your help.
                  >
                  >
                  "Jay B. Harlow" wrote:
                  >
                  >Serge,
                  Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
                  >Why is GetIDAsVariant returning a Byte()? It appears that it really
                  >should
                  >return a COM VARIANT, in the .NET world COM Variants are exposed as
                  >System.Objec t.
                  >>
                  >As TDC suggested: System.Runtime. Interop.Marshal is your friend here.
                  >It
                  >has
                  >methods to marshal (convert/translate) between the COM/C++ (unmanaged)
                  >world
                  >& the .NET (managed) world... However it helps to define the methods &
                  >interfaces that you use first, as P/Invoke (how you define the method)
                  >is
                  >your first "defense" on converting unmanaged types.
                  >>
                  >>
                  >FWIW The name GetIDAsVariant, suggests it returns a COM Variant, a
                  >Google
                  >search reinforces this notion. I normally check www.pinvoke.net for
                  >the
                  >correct definitions of unmanaged interfaces & methods that I want to
                  >use
                  >in
                  >the managed world.
                  >>
                  >--
                  >Hope this helps
                  >Jay B. Harlow
                  >.NET Application Architect, Enthusiast, & Evangelist
                  >T.S. Bradley - http://www.tsbradley.net
                  >>
                  >>
                  >"Serge BRIC" <SergeBRIC@disc ussions.microso ft.comwrote in message
                  >news:0BE821C E-C60D-4438-BAAF-5E876666E541@mi crosoft.com...
                  My application, written in .NET VB, tries to get a communication
                  port
                  handle
                  from a TAPI object with this code:
                  >
                  Dim vFileHandle As Byte() = appel.GetIDAsVa riant("comm/datamodem")
                  >
                  The vFileHandle is supposed to be a file handle (an IntPtr, I
                  suppose).
                  How
                  can I convert this Byte() in this IntPtr ?
                  >>
                  >>

                  Comment

                  Working...