Solved

UDT TYPES AGAIN!!!!!!!

Posted on 2001-06-19
7
321 Views
Last Modified: 2008-02-01
Hi,

I have a problem with UDT's. The problem is mainly trying to pass them
around functions as parameters, when the functions receiving them as
parameters need to be generic. The UDTs are required because they represent
C Structures on another UNIX type platform.

Some Code........

In a public .bas file

Public Type TEST1
    str1 as String *10
    str2 as String *20
End Type

Public Type TEST2
    str1 as String *10
    str2 as TEST1
End Type

Public Type TEST4
    str1 as String *10
    str2(21) as TEST2
End Type

In a class module that needs to pass the UDTs around as parameters to other
functions.

Private Type TEST3
    strBuffer as string * len(TEST2) ' I know you can't do this in code but
its here for illustration purposes
End Type

Private Type TEST5
    strBuffer as string * len(TEST4) ' I know you can't do this in code but
its here for illustration purposes
End Type

I can use Lset to do the following
TEST3.strBuffer = "A constant string that represents data in a passed C
structure the length and format of UDT"

Lset TEST4=TEST2
This stuffs TEST2 into TEST4 thereby populating TEST4 with the data in TEST2
in the required format.

However I can't use Lset to do this......

TEST5.strBuffer = "A constant string that represents data in a passed C
structure the length and format of UDT"

Lset TEST4=TEST5

This is supposed to stuff TEST5 into TEST4 thereby populating TEST4 with the
data in TEST2 in the required format.
The problem is that because TEST5 contains an array of UDT's this causes VB
to poke up a type mismatch error.

I can pass around TEST3.strBuffer but not a specific UDT because of the
following

Public Function Test6(strIN as string)

    ' This allows me to pass in any string of any length

End Function

Public Function Test7(udtIN as TEST4)

    ' This allows me to pass in a udt of a predefined specific type
   ' eg TEST4 and doesn't allow me to pass any UDT into this function

End Function

Is there any other way I can do this???

A very fustrated,

Andy
0
Comment
Question by:AA69
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:RodStephens
ID: 6206317
If your goal is to pass various objects to a function written in C/C++, you may be able to get there using the Declare statement.

> Lset TEST4=TEST5

If you just want to pass the data in TEST5 into a function that expects a character pointer to the data, declare the function to take a TEST5 object as a parameter. When you call the routine, the address of the TEST5 is passed into the function. It only knows it has received a pointer, not what kind of thing it points to.

> ' eg TEST4 and doesn't allow me to pass any UDT into
> this function

If you want to be able to pass any UDT into a function, either:

1. Try declaring the argument as Any.
2. Make multiple declarations for the function.

Public Declare Function AbortDoc Lib "gdi32" Alias "AbortDoc" (ByVal hdc As Long) As Long

Public Declare Function Whatever1 Lib "C:\test.dll" _
    Alias "Whatever" (ByVal value As TEST1) As Long
Public Declare Function Whatever2 Lib "C:\test.dll" _
    Alias "Whatever" (ByVal value As TEST2) As Long

Then use the right version of the function depending on the argument you want to pass it.
0
 
LVL 15

Accepted Solution

by:
ameba earned 200 total points
ID: 6206857
Instead of LSet you can use CopyMemory to convert UDT to string
Ref.: http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=325
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=350

' bas module -----------------------------------------------------
Option Explicit
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Type TEST1
   str1 As String * 10
   str2 As String * 20
End Type

Public Type TEST2
   str1 As String * 10
   udtTest1 As TEST1
End Type

Public Type TEST4
   str1 As String * 10
   udtTest2(0 To 10) As TEST2
End Type


' Form1 -----------------------------------------------------
Option Explicit
Private m_complex As TEST4

Private Sub Form_Click()
    Dim s As String ' will hold contents of m_complex
    Dim i As Long
   
    Cls
    Print "COMPLEX CASE ------------------------------------------------"
   
    ' fill udt with some data
    m_complex.str1 = "Mike"
    For i = 0 To 10
        m_complex.udtTest2(i).str1 = "u2_str1_" & i
        m_complex.udtTest2(i).udtTest1.str1 = "u1_str1_" & i
        m_complex.udtTest2(i).udtTest1.str2 = "u1_str2_" & i
    Next

    ' convert udt to string
    s = GetStringFromUDT()
   
    ' convert Variant to udt
    ' empty udt (this step is not needed)
    Dim empty_udt As TEST4
    m_complex = empty_udt
   
    Call ReadStringAndFillUDT(s)  ' convert string to udt
   
    ' check result
    Print m_complex.str1
    For i = 0 To 10
        Print "  " & m_complex.udtTest2(i).str1
        Print "     " & m_complex.udtTest2(i).udtTest1.str1
        Print "     " & m_complex.udtTest2(i).udtTest1.str2
    Next
End Sub

' convert m_complex to string
Public Function GetStringFromUDT() As String
    Dim bytes As Long ' the number of bytes needed to hold udt contents
    bytes = LenB(m_complex)
   
    GetStringFromUDT = Space$((bytes + 1) \ 2) ' make a buffer long enough to hold the UDT
    ' copy the UDTs into the string
    CopyMemory ByVal StrPtr(GetStringFromUDT), ByVal VarPtr(m_complex), bytes
End Function

' convert string to m_complex
Public Sub ReadStringAndFillUDT(ByVal buffer As String)
    Dim bytes As Long ' the number of bytes needed to hold udt contents
    bytes = LenB(m_complex)
   
    ' copy the string into the UDTs
    CopyMemory ByVal VarPtr(m_complex), ByVal StrPtr(buffer), bytes
End Sub
0
 
LVL 15

Expert Comment

by:ameba
ID: 6206946
>"UDT TYPES AGAIN!!!!!!!"
Here is the link to previous question "UDT TYPES!!!" (20 pts)
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20131345
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:AA69
ID: 6206960
Good point I should have put that link in I'll remember next time. I'll try that code and report back again.
0
 

Author Comment

by:AA69
ID: 6207314
Good point I should have put that link in I'll remember next time. I'll try that code and report back again.
0
 

Author Comment

by:AA69
ID: 6449349
Hi Sorry for the delay in giving out the points( holidays and all that). Anyway we actually still used lset and didn't need to use the memcopy function we simply built up the UDT using simple for next loops and concatenating the strings as we went it all works just dandy. But your answer is still valid and I might well use that somewhere else so you get the points. Just one thing could you please point me to Rockford Lhotka's book.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6449365
Thanks
http://www.lhotka.net/rocky/Publications.aspx
Just click on:  "Professional Visual Basic 6 Distributed Objects" book
Note that book is not quite easy to read / understand.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now