Click to See Complete Forum and Search --> : Can VB handle structures and pointers like VC
Jerry Jones
January 28th, 1999, 11:59 AM
Is there anyway in VB that you can define a String of Data (Buffer) and reference that data using a structure (Type)
with a pointer?
VC Example:
struct EntryStruct
{
char szFldName [50];
int nFldNumber;
};
char szBuffer [500];
char szName [50];
EntryStruct* pPtr;
pPtr = (EntryStruct*) szBuffer;
szName = pPtr.szFldName;
Dr_Michael
July 1st, 1999, 06:32 AM
There is no way to create pointer variables directly. All a pointer needs to do, however, is point to another piece of data. If you create an array of data structures, you can use integers as pointers to the entries in the array. The data to which a pointer points is the array entry with the corresponding index. This technique is called pointer faking.
Michael Vlastos
Company MODUS SA
Development Department
Tel: +3-01-9414900
Chris Eastwood
July 1st, 1999, 07:12 AM
Hi
You can do this will a WinApi called CopyMemory / RTLMoveMemory :
public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination as Any, Source as Any, byval Length as Long)
This will take a block of memory and copy it into another area, so for example, suppose you have a User Defined type as :
public Type RRecord
RECTY as string * 1
Tag as string * 9
FILL01 as string * 16
SHORT as string * 40
LONG as string * 80
FILLER as string * 120
End Type
And then you read in a string from a file which needs to be in this format :
Dim recRRecord as RRecord
Dim sString as string
'
' Do some stuff here to read in sString from the file
'
'.....
'...
'..
'
If len(sString) < len(recRRecord) then
'
' Pad out the string so it's the same length in bytes as our Type
'
sString = sString & string$(len(recRRecord) - len(sString), " ")
'
End If
'
' Now copy the stirng into the UDT
'
CopyMemory recRRecord, byval sString, len(recRRecord)
'
' Now you can access each property of the recRRecord
'
I've used this quite a bit for handling files created by
Mainframes which need importing into PC systems
- works really fast too!
Chris Eastwood
CodeGuru - the website for developers
http://www.codeguru.com/vb
Lothar Haensler
July 1st, 1999, 08:00 AM
you can't do such a thing in VB.
You can't even do it in C, because your C code is bogus! :-))
szName = pPtr.szFldName; is invalid because you declared szname as char szName[50];
szName evaluates to &szName[0] which is not an lvalue.
Even the C compiler would report an error.
Sorry, but I couldn't resist :-)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.