473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.GetIDAsVariant("comm/datamodem")

The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How
can I convert this Byte() in this IntPtr ?
Oct 16 '06 #1
8 5877
Sorry for this double message (I thought the previous one had failed).
Oct 16 '06 #2
TDC
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.GetIDAsVariant("comm/datamodem")

The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How
can I convert this Byte() in this IntPtr ?
Oct 16 '06 #3
I have obtained the TAPI3 file handle with VB and C++ code, like this:

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

The C++ BytePtr2Handle() is :

__declspec(dllexport) 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.GetIDAsVariant("comm/datamodem")

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

Oct 17 '06 #4
Serge,
Dim vFileHandle As Byte() = appel.GetIDAsVariant("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" <Se*******@discussions.microsoft.comwrote in message
news:0B**********************************@microsof t.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.GetIDAsVariant("comm/datamodem")

The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
How
can I convert this Byte() in this IntPtr ?
Oct 17 '06 #5
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 ITLegacyCallMediaControl object (TAPI3 object) was
something like :

BSTR bstrComm = ::SysAllocString(L"comm/datamodem");
BYTE *pDeviceID;
DWORD dwSize;
ITLegacyCallMediaControl *pCallMediaControl;
...
HRESULT hr = pCallMediaControl->GetID(bstrComm, &dwSize, &pDeviceID);
HANDLE handle = *((LPHANDLE)pDeviceID);

So, in VB, it is:

Dim appel As ITLegacyCallMediaControl2
...
Dim obj As Object = callMediaControl.GetIDAsVariant("comm/datamodem")
Dim handle As IntPtr = Marshal.ReadIntPtr(obj, 0)

Thanks for your help.
"Jay B. Harlow" wrote:
Serge,
Dim vFileHandle As Byte() = appel.GetIDAsVariant("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" <Se*******@discussions.microsoft.comwrote in message
news:0B**********************************@microsof t.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.GetIDAsVariant("comm/datamodem")

The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
How
can I convert this Byte() in this IntPtr ?
Oct 17 '06 #6
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 ITLegacyCallMediaControl2 defined?
--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Serge BRIC" <Se*******@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.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 ITLegacyCallMediaControl object (TAPI3 object) was
something like :

BSTR bstrComm = ::SysAllocString(L"comm/datamodem");
BYTE *pDeviceID;
DWORD dwSize;
ITLegacyCallMediaControl *pCallMediaControl;
...
HRESULT hr = pCallMediaControl->GetID(bstrComm, &dwSize, &pDeviceID);
HANDLE handle = *((LPHANDLE)pDeviceID);

So, in VB, it is:

Dim appel As ITLegacyCallMediaControl2
...
Dim obj As Object =
callMediaControl.GetIDAsVariant("comm/datamodem")
Dim handle As IntPtr = Marshal.ReadIntPtr(obj, 0)

Thanks for your help.
"Jay B. Harlow" wrote:
>Serge,
Dim vFileHandle As Byte() = appel.GetIDAsVariant("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" <Se*******@discussions.microsoft.comwrote in message
news:0B**********************************@microso ft.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.GetIDAsVariant("comm/datamodem")

The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
How
can I convert this Byte() in this IntPtr ?
Oct 17 '06 #7
Jay,

ITLegacyCallMediaControl2 is an extension of the ITLegacyCallMediaControl
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
ITLegacyCallMediaControl2 interface (in C++, it is
ITLegacyCallMediaControl::GetID()).

I don't understand either why the return of
ITLegacyCallMediaControl2::GetIDAsVariant() 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 ITBasicCallControl
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.CreateCall(telephoneNumber,
TapiConstants.LINEADDRESSTYPE_PHONENUMBER,
TapiConstants.TAPIMEDIATYPE_DATAMODEM)
appel.Connect(False)
...
'gets the TAPI3 comm port name
Dim portName As Byte() =
appel.GetIDAsVariant("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.GetIDAsVariant("comm/datamodem")
Dim hPort As IntPtr = Marshal.ReadIntPtr(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 ITLegacyCallMediaControl2 defined?
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Serge BRIC" <Se*******@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.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 ITLegacyCallMediaControl object (TAPI3 object) was
something like :

BSTR bstrComm = ::SysAllocString(L"comm/datamodem");
BYTE *pDeviceID;
DWORD dwSize;
ITLegacyCallMediaControl *pCallMediaControl;
...
HRESULT hr = pCallMediaControl->GetID(bstrComm, &dwSize, &pDeviceID);
HANDLE handle = *((LPHANDLE)pDeviceID);

So, in VB, it is:

Dim appel As ITLegacyCallMediaControl2
...
Dim obj As Object =
callMediaControl.GetIDAsVariant("comm/datamodem")
Dim handle As IntPtr = Marshal.ReadIntPtr(obj, 0)

Thanks for your help.
"Jay B. Harlow" wrote:
Serge,
Dim vFileHandle As Byte() = appel.GetIDAsVariant("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" <Se*******@discussions.microsoft.comwrote in message
news:0B**********************************@microsof t.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.GetIDAsVariant("comm/datamodem")

The vFileHandle is supposed to be a file handle (an IntPtr, I suppose).
How
can I convert this Byte() in this IntPtr ?
Oct 18 '06 #8
Serge,
I know what it is. :-|

I wanted to see your VB.NET definition of ITLegacyCallMediaControl2!

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" <Se*******@discussions.microsoft.comwrote in message
news:C0**********************************@microsof t.com...
Jay,

ITLegacyCallMediaControl2 is an extension of the ITLegacyCallMediaControl
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
ITLegacyCallMediaControl2 interface (in C++, it is
ITLegacyCallMediaControl::GetID()).

I don't understand either why the return of
ITLegacyCallMediaControl2::GetIDAsVariant() 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 ITBasicCallControl
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.CreateCall(telephoneNumber,
TapiConstants.LINEADDRESSTYPE_PHONENUMBER,
TapiConstants.TAPIMEDIATYPE_DATAMODEM)
appel.Connect(False)
...
'gets the TAPI3 comm port name
Dim portName As Byte() =
appel.GetIDAsVariant("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.GetIDAsVariant("comm/datamodem")
Dim hPort As IntPtr = Marshal.ReadIntPtr(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 ITLegacyCallMediaControl2 defined?
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Serge BRIC" <Se*******@discussions.microsoft.comwrote in message
news:BA**********************************@microso ft.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 ITLegacyCallMediaControl object (TAPI3 object) was
something like :

BSTR bstrComm = ::SysAllocString(L"comm/datamodem");
BYTE *pDeviceID;
DWORD dwSize;
ITLegacyCallMediaControl *pCallMediaControl;
...
HRESULT hr = pCallMediaControl->GetID(bstrComm, &dwSize, &pDeviceID);
HANDLE handle = *((LPHANDLE)pDeviceID);

So, in VB, it is:

Dim appel As ITLegacyCallMediaControl2
...
Dim obj As Object =
callMediaControl.GetIDAsVariant("comm/datamodem")
Dim handle As IntPtr = Marshal.ReadIntPtr(obj, 0)

Thanks for your help.
"Jay B. Harlow" wrote:

Serge,
Dim vFileHandle As Byte() = appel.GetIDAsVariant("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" <Se*******@discussions.microsoft.comwrote in message
news:0B**********************************@microso ft.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.GetIDAsVariant("comm/datamodem")

The vFileHandle is supposed to be a file handle (an IntPtr, I
suppose).
How
can I convert this Byte() in this IntPtr ?
Oct 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
We are writing some code to interface to a DLL. The DLL has a callback method that returns the address of a byte buffer and the buffer length. I have defined the byte pointer as an IntPtr. ...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
3
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte,...
6
by: centrino | last post by:
hi, I import a funtion fromm dll file: dll function header : DWORD WINAPI Enumerate( HANDLE hWDMHandle, PNDIS_STATUS pNStatus,
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
0
by: Serge BRIC | last post by:
My application, written in VB, tries to get a communication port handle (from a TAPI3 object) with this code : Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The Byte()...
6
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005, .net 2 and C# for windows application. I need to convert a IntPtr to a byte to be able to add a meetingBlob data to the meeting class object in Active Directory schema. I...
1
by: Paul Jarvis | last post by:
I have a large structure, below is a simplistic version of my structure: public struct MeanMinMaxSd { public double mean; public double min; public double max; public double sd; }
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.