From: Scorpion_1982 on
Private Type Header
Typ As Long
Length As Long
End Type
Private Type Dane
Nick As String
IP As Long
End Type

Private Sub Form_Load()
Dim H As Header
Dim D As Dane
Dim FF As Integer
Dim Buf As String
D.Nick = "None"
D.IP = 1
H.Typ = 1
H.Length = Len(D)
FF=FreeFile
Open "C:\FullTxt.txt" For Binary As FF
Put #FF, 1, H
Put #FF, , D
Buf = Space$(LOF(FF))
Get #FF, 1, Buf
Close FF
'how to copy from the string to the structure ?? it can be using win
API
'and different how to copy from sctructure to string
'Every time i am using operations on files to be able to fill
correctly the structures
'How works function "Get"? It attributes nice to the structure
'Buf=010000000800000004004E6F6E6501000000
'How to copy a data from the variable "Buf" to Header and data each
after another?? without operating on files and without get method.
'Thanks for helping, Greetings
End Sub

From: Mike Williams on
On 4 Nov, 21:42, Scorpion_1982 <Scorpion_1...(a)o2.pl> wrote:

I'm not entriely sure what you are asking but it looks as though you
want to transfer a UDT to a "data string" or an "array of data bytes"
without using Put and to end up with the same string that Put would
have written to the file. Also, it appears that you want to take such
a data string or array of bytes and assign it to a UDT without loading
it from a file using Get. If so then there is no "easy way" to do it
and there is certainly no suitable API. VB performs such a task by
analyzing the UDT (checking what kind of variables it contains) and
then splitting up the data accordingly, and if you want to do it
without using Get or Put then you are going to have to do the same
sort of analysis in your code. It can be done of course, but you will
need to look closely at how VB writes UDT data to file. This is
clearly specified in the VB help files. For example, suppose you start
off with the string you have quoted in your own example (with each
character being represented by a hex pair):

010000000800000004004E6F6E6501000000

In order to place that data properly into your two UDTs your code
would need to know the exact layout of the variable types in both UDTs
(the exact same data can mean many completely different things to
different UDTs). In other words, you would need to write your own code
specifically designed to handle a specific UDT. For example, you know
that the only two variables in Private Type Header are Longs, so you
would transfer the first four bytes of data (01000000) into the first
Long (H.Typ) and the second four bytes of data (08000000) into the
second Long (H.Length). You do this only because your code *knows*
that the two variables are both Longs. Now look at the next set of
data in the string (04004E6F6E650100...etc). If you do not know the
exact layout of the variables in the UDT (Private Type Dane) then
those data bytes can mean almost anything! For example, the data bytes
04004E6F could be the decimal value 1867382788 if it were held in a
Long, and indeed if the type of the first variable in the Private Type
Dane UDT was a Long then that is exactly what it would be. However,
your code *knows* that the first variable in the Private Type Dane UDT
is actually a standard variable length String, and so your code needs
to analyze the data accordingly. The first two data bytes (0400)
represent the length of the String (4 characters) and so your code
knows how long the string is going to be (4 characters in this
specific case) and it can then treat the next four data bytes
(4E6F6E65) as the four string characters themselves and your code can
therefore transfer those four data bytes as a string into the
appropriate variable in the UDT. As you will by now appreciate, the
only way your code can make sense of a block of data that is inteneded
to fill a UDT is for your code to know the exact layout of the data
types within that specific UDT. (The VB Get statement does know those
details of course, because an "instance" of that UDT accompanies the
Get statement). If you check out the help files (type the word Put
into some code and hit the F1 key) you'll see how the VB Put statement
saves the data, and you should therefore be able to write your code in
the way that I have described, taking each different possible data
type into account. But, of course, your code definitely needs to know
the precise layout of the UDT for which the data is intended in order
for it to make correct sense of it. All in all I think this is rather
a lot of work simply to avoid writing the data string or data bytes
(which you are presumably getting from a serial port or whatever) to a
temporary file and "Getting" it back into a UDT of the precise format
for which it is destined. It might still be a good programming
exercise though, if that's what you're really after.

Mike

> Private Type Header
> Typ As Long
> Length As Long
> End Type
> Private Type Dane
> Nick As String
> IP As Long
> End Type
>
> Private Sub Form_Load()
> Dim H As Header
> Dim D As Dane
> Dim FF As Integer
> Dim Buf As String
> D.Nick = "None"
> D.IP = 1
> H.Typ = 1
> H.Length = Len(D)
> FF=FreeFile
> Open "C:\FullTxt.txt" For Binary As FF
> Put #FF, 1, H
> Put #FF, , D
> Buf = Space$(LOF(FF))
> Get #FF, 1, Buf
> Close FF
> 'how to copy from the string to the structure ?? it can be using win
> API
> 'and different how to copy from sctructure to string
> 'Every time i am using operations on files to be able to fill
> correctly the structures
> 'How works function "Get"? It attributes nice to the structure
> 'Buf=010000000800000004004E6F6E6501000000
> 'How to copy a data from the variable "Buf" to Header and data each
> after another?? without operating on files and without get method.
> 'Thanks for helping, Greetings
> End Sub


From: Scorpion_1982 on
'---------------CODE------------------
Pivate Type Header
Typ As Long
Lenght As Long
End Type
Private Type Dane
Nick As String
IP As Long
End Type

Dim Buf As String

Dim struct_Header As Header
Dim struct_Dane As Dane

On Error Resume Next
struct_Dane.Nick="some nick"
struct_Dane.IP=2
struct_Header.Typ=1
struct_Header.Lenght=Len(struct_Dane)
'before data send I'm doing this way
Open "C:\tmp.dat" for Binary As 1
Put #1,1,struct_Header
Put #1,,struct_Dane
buf=space(lof(1))
get #1,1,buf
Close 1
Kill "C:\tmp.dat"
Winsock.SendData Buf
'the variable "buf" I'm sending by WinSock
'I'm getting data using WinSock
WinSock.GetData Buf,VbString
i zapisuje to do pliku
Dim struct_Header As Header
Dim struct_Dane As Dane
On Error Resume Next
Open "C:\tmp.dat" for Binary As 1
Put #1,1,Buf
Get #1,1,struct_Header
Get #1,,struct_Dane
Close 1
Kill "C:\tmp.dat"
'-----------------------------------------------
How to do this without using an operation on files because this way is
messing the hard drive :-)
P.S. sorry for my language, I don't know english very well

Many thanks again...

From: Mike Williams on
On 5 Nov, 16:10, Scorpion_1982 <Scorpion_1...(a)o2.pl> wrote:

> How to do this [UDT Get and Put] without
> using an operation on files because this
> way is messing the hard drive :-)

The first question I want to ask is, "Did you see my reply that I
popsted a few hours ago"? If so you have made no mention of it. You
need to check out Get in the help files and it will tell you exactly
how it formats the data for the various data types. You will then be
able to write your own code to do the job you have requested. Have you
read my reply? And have you had a look in the help files?

Secondly, why are you storing the length of the Dane UDT in an element
of the first UDT (Header)? What is the purpose of you doing that? Why
don't you just put the Typ variable in with the second UDT and then
get rid of the first altogether, so you just have one UDT containing
Typ, Nick and IP? That is the best way to do it. The value in the
Length variable in the fist UDT (which will hold the value 8 in your
specific case) does not tell you anything useful at all, so there
doesn't seem to be much point in storing it. All it tells you is the
total length of the various variables in the UDT, but in the case of
the Nick variable (which is a standard variable length String) the
length will be four bytes, regardless of the length of the string that
you place in there, because it is a "pointer" to the string, rather
than the string itself. The Buf string itself (the buffer you are
currently using Put and Get with) can be almost any length at all,
depending on the number of characters in the string. So, before I
respond any further would you like to think about those things and
post back with your comments?

Mike



> '---------------CODE------------------
> Pivate Type Header
> Typ As Long
> Lenght As Long
> End Type
> Private Type Dane
> Nick As String
> IP As Long
> End Type
>
> Dim Buf As String
>
> Dim struct_Header As Header
> Dim struct_Dane As Dane
>
> On Error Resume Next
> struct_Dane.Nick="some nick"
> struct_Dane.IP=2
> struct_Header.Typ=1
> struct_Header.Lenght=Len(struct_Dane)
> 'before data send I'm doing this way
> Open "C:\tmp.dat" for Binary As 1
> Put #1,1,struct_Header
> Put #1,,struct_Dane
> buf=space(lof(1))
> get #1,1,buf
> Close 1
> Kill "C:\tmp.dat"
> Winsock.SendData Buf
> 'the variable "buf" I'm sending by WinSock
> 'I'm getting data using WinSock
> WinSock.GetData Buf,VbString
> i zapisuje to do pliku
> Dim struct_Header As Header
> Dim struct_Dane As Dane
> On Error Resume Next
> Open "C:\tmp.dat" for Binary As 1
> Put #1,1,Buf
> Get #1,1,struct_Header
> Get #1,,struct_Dane
> Close 1
> Kill "C:\tmp.dat"
> '-----------------------------------------------
> How to do this without using an operation on files because this way is
> messing the hard drive :-)
> P.S. sorry for my language, I don't know english very well
>
> Many thanks again...


From: Mike Williams on
On 5 Nov, 16:10, Scorpion_1982 <Scorpion_1...(a)o2.pl> wrote:

> How to do this [UDT Get and Put] without
> using an operation on files [2] . . .

Okay. While you're considering what I said in my previous response
here is some code that shows you how to transfer the data of your two
current UDTs into a Byte Array (or into a VB String) without using a
disk file. It produces (on those two specific UDTs) exactly the same
output as you would achieve if you used the VB Put statement to put
the two UDTs to disk. Now can you see why I said that you need to
write code that is specifically tailored to specific UDTs? It is
possible to automate this a little bit so that it checks the data
types, but doing it manually (as in the following code) is the only
way to achieve exactly what you want under all circumstances (it's
basically what VB itself does when performing this task). The opposite
(getting the resultant data back into two exactly similar UDTs) is
just as easy, basically you use the same process but in reverse.

By the way, if I suddenly disappear for a while and don't answer your
questions then it's because I've got some problems at the moment with
my wife in hospital for an operation (she's doing fine, by the way),
but if you wait long enough I'm sure to be back (and of course there
are lots of other people here who will be only too pleased to help
you).

Mike

Oops. Nearly forgot the code! Here it is:

Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Type Header
Typ As Long
Length As Long
End Type
Private Type Dane
Nick As String
IP As Long
End Type

Private Sub Command1_Click()
Dim Buf() As Byte, bufString As String
Dim temp1 As Integer, temp2() As Byte
Dim struct_Header As Header
Dim struct_Dane As Dane
struct_Dane.Nick = "some nick"
struct_Dane.IP = 2
struct_Header.Typ = 1
struct_Header.Length = Len(struct_Dane)
' the above is your own stuff
ReDim Buf(1 To 10)
CopyMemory Buf(1), struct_Header.Typ, 8 ' the two Longs
temp1 = Len(struct_Dane.Nick)
CopyMemory Buf(9), temp1, 2 ' the string length
temp2() = StrConv(struct_Dane.Nick, vbFromUnicode)
ReDim Preserve Buf(1 To 10 + temp1 + 4)
CopyMemory Buf(11), temp2(0), temp1 ' the string data
CopyMemory Buf(11 + temp1), struct_Dane.IP, 4 ' the Long
' buf() is a Byte array that now contains the same
' data as would be sent to the file using Get on
' these two specific UDTs. If you wopuld prefer to
' have it in a VB string instead of a Byte array
' then you can use the following line:
bufString = StrConv(Buf(), vbUnicode)
End Sub