Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing data from C to VB

Status
Not open for further replies.

Galyl

Programmer
Nov 21, 2001
16
0
0
ZA
Im using a C dll to retrieve data via a comm port. It passes back a pointer to an array of bytes after it has retreived the data. How do I use that pointer to access the data in the memory location in VB. At the moment the data is passed back in a stream of bytes, which is not very efficient. Please advise.
 
I can think of 2 things u can try:

1.) Recieve the C pointer as a VB Variant. Once the Variant is assigned to the pointer you should be able to treat it like an array.

2.) Modify the C function to return void and recieve the array via a pointer in the argument list, then pass the VB array ByRef for the C function to populate it. If you do this then it will be a Declare Sub, not Function.

I haven't done this before, but I've seen API functions use the 2nd approach so I know it's possible.

Hope that helps.

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
I tried declaring a variant variable in VB, and using that to accept the pointer. The variant then contains the size of the memory buffer and not the data. I did it as follows:

Private Declare Function GetData Lib "vc2vb.dll" () As Byte
function for returning pointer to array

BYTE *GetData()
{
return g_UIB->data_buf;
}
C function that returns pointer to array

Private vDataBuf as Variant
variable to contain array

vDataBuf = GetData
assigning poiter to variant

Is this the correct way to do it?

Please advise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top