CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 1999
    Posts
    1

    Can VB handle structures and pointers like VC



    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;



  2. #2
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Can VB handle structures and pointers like VC

    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

  3. #3
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Can VB handle structures and pointers like VC

    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

  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Can VB handle structures and pointers like VC

    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 :-)




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured