Declare a size of a string

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Hi all, want to use the API call:

GetVersionEx_()

What I have now is:
-snip-
Structure Version ; 0. The problem is how to allocate 128 byte for
CSDVersion.s and than use it in a structure.

Tryed CSDVersion.s[128] or the Dim command.
Also tryed to use a pointer to the variable but I don't get it.
Is there a possibility?



Have a nice day...
Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

Hi Franco,

try to do it like this:

Code: Select all

;first your structure..
Structure Version
  VersionInfoSize.l
  MajorVersion.l
  MinorVersion.l
  BuildNumber.l
  PlatformId.l
  CSDVersion.b[128] ; declare as a series of bytes instead of a string
EndStructure
;
ver.version\VersionInfoSize=Sizeof(Version) ; Tell how big your structure is
;
if GetVersionEx_(@ver)
  CSDString.s=Peeks(@ver\PlatformID+4) ; This is a workaround it should be @ver\CSDVersion but
                                       ; when trying that you get an assembly error!
  ; now your variable 'ver' of structure Version is filled with the info you want
  ...
endif
Hope that did help!





Edited by - pupil on 03 October 2001 11:36:55
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Hello Pupil,
Did you actually get your code you posted to work??
I pasted it into PB 2.50 and get a:
"Trying to write a string into numeric varable"
error in the line:
CSDString.s=Peeks(@ver\PlatformID+4)

Any ideas??
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Hi Pupil,
thanks. Great idea to declare as bytes and than to get the string with PeekS.

Hi Paul, this code works on my machine:
;-start-
Structure Version
VersionInfoSize.l
MajorVersion.l
MinorVersion.l
MajorBuild.w
MinorBuild.w
PlatformID.l ; 0=Win3.x 1=Win9x 2=WinNT
CSDVersion.b[128] ; declare as a series of bytes instead of a string
EndStructure

Windows.Version\VersionInfoSize=Sizeof(Version) ; Tell how big your structure is

If GetVersionEx_(Windows)
PlatformID$= str(Windows\PlatformID)
MessageRequester("Info PlatformID 0=Win3.x 1=Win9x 2=WinNT",PlatformID$,0)

Version$= str(Windows\MajorVersion)+"."+str(Windows\MinorVersion)
MessageRequester("Info Version",Version$,0)

Build$= str(Windows\MajorBuild)+"."+str(Windows\MinorBuild)
MessageRequester("Info BuildNumber",Build$,0)

CSDVersion$=PeekS(@Windows\PlatformID+4) ;additional information
; This is a workaround it should be @Windows\CSDVersion but
; when trying that you get an assembly error!
MessageRequester("Info CSDVersion",CSDVersion$,0)
Else
MessageRequester("Windows Version","Can't get information!",0)
EndIf
;-end-

Question:
Why I have to code:
CSDVersion$=PeekS(@Windows\PlatformID+4)
instead of:
CSDVersion$=PeekS(Windows\PlatformID+4)
and only for this line?
When I add @ on the other lines I get wrong results.
And this line can be:
If GetVersionEx_(Windows) ;<- this works
or:
If GetVersionEx_(@Windows) ;<- this works
both works fine.



Have a nice day...
Franco


Edited by - Franco on 03 October 2001 17:39:45
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Hello Pupil,
Did you actually get your code you posted to work??
I pasted it into PB 2.50 and get a:
"Trying to write a string into numeric varable"
error in the line:
CSDString.s=Peeks(@ver\PlatformID+4)

Any ideas??
Ehmm, it a small typo when i pulled the code over to the forum. The line should be:
CSDString.s=Peeks(@ver\PlatformId+4) ;see a small d instead of D on the Id part
I think the above line should do the trick!
And yes, this time i really tested the code :wink:
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Question:
Why I have to code:
CSDVersion$=PeekS(@Windows\PlatformID+4)
instead of:
CSDVersion$=PeekS(Windows\PlatformID+4)
and only for this line?
When I add @ on the other lines I get wrong results.
And this line can be:
If GetVersionEx_(Windows) ;<- this works
or:
If GetVersionEx_(@Windows) ;<- this works
both works fine.
I'll try to explain with some commented examples:

Code: Select all

Structure tmp ; Just a small simple structure
  a.w
  b.w
  c.w
EndStructure
;
tmpvar.tmp ; Just init a variable of type 'tmp'
address.l = tmpvar ; This returns the memory location of the structure
                   ; for the variable 'tmpvar'
; The '@' sign can be used to get the memory location of any variable

address.l = tmpvar ; This also returns the memory location of the structure
                   ; for the variable 'tmpvar'
                   ; This behavour is valid only for structures and not for
                   ; one-type variables such as .b .f .l .s .w
;
; In the two examples above we addressed the structure without a path
; now lets see what happends when we add a path.
;
address.l = @tmpvar\a   ; This returns the memory location of 'a' inside
                        ; the structure 'tmp' in the variable 'tmpvar', as it
                        ; is the first instance in the structure it is in 
                        ; fact the same address that you get with @tmpvar
;
; If you instead drop the '@' sign you'll get:
;
address.l = tmpvar\a  ; This will return the value that is contained in the
                      ; variable 'a' inside the structure 'tmp' in our variable
                      ; 'tmpvar'. In this case we might get an error as we're
                      ; putting a word-type into a long-type.
Hope things will get a little clearer and that I didn't confuse you further;)
short recap:
CSDVersion$=PeekS(@Windows\PlatformID+4)
To make it short, we needed the address to the contents in variable 'CSDVersion' inside the structured variable 'Windows' that's why we put the '@' sign in front. Without it you would just add the content of PlatformID with 4.

Also i figured out why i had to do the workaround, in the example change this line:
CSDVersion$=PeekS(@Windows\PlatformID+4)
to this:
CSDVersion$=PeekS(@Windows\CSDVersion[0])

That was a long one :wink:




Edited by - pupil on 03 October 2001 18:50:42

Edited by - pupil on 03 October 2001 20:31:45
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

And this is the last one...

; (c) 2001 - Franco's template - absolutely freeware
; Get System information
Structure Version
VersionInfoSize.l
MajorVersion.l
MinorVersion.l
MajorBuild.w
MinorBuild.w
PlatformID.l ; 0=Win3.x 1=Win9x 2=WinNT
CSDVersion.b[128] ; declare as a series of bytes instead of a string
EndStructure

Windows.Version\VersionInfoSize=Sizeof(Version) ; Tell how big your structure is
If GetVersionEx_(Windows)
ShellAbout_(0, "Franco's very little System Info # Operating System","Version "+str(Windows\MajorVersion)+"."+str(Windows\MinorVersion)+"."+str(Windows\MajorBuild)+"."+str(Windows\MinorBuild)+"."+PeekS(@Windows\CSDVersion[0]), 0)
Else
MessageRequester("Franco's very little System Info","Error: Can't get information!",0)
EndIf

Thanks for explaining Pupil. I will try to catch it



Have a nice day...
Franco
Post Reply