apply a structure to memory data

Just starting out? Need help? Post your questions and find answers here.
frederic
User
User
Posts: 56
Joined: Thu Jan 05, 2006 11:22 pm

apply a structure to memory data

Post by frederic »

Hello, i don't know if this pb is linux specific but i would like to apply a structure to data memory.

The function uname_ return a pointer to some structured information and i want to work with this info easily with the structure "utsname" but how ?

For the moment, i use PeekS, but it stops at the end of first string (normal it's his job).

Code: Select all

Structure  utsname 
       sysname.s
       nodename.s
       release.s
       version.s
       machine.s
EndStructure

Global *test=AllocateMemory(SizeOf(utsname)) 

If uname_(*test) = 0

        Debug PeekS(*test)

Else
        Debug "error"
EndIf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

use other components than .s

.s is a 4byte long pointer to a string, not the string itself.
oh... and have a nice day.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I'm not 100% sure what it is you're asking, but this is how you might set/retrieve the strings in the structure:

Code: Select all

Structure  utsname 
       sysname.s 
       nodename.s 
       release.s 
       version.s 
       machine.s 
EndStructure 

Global *test.utsname = AllocateMemory(SizeOf(utsname)) 

With *test
  \sysname = "MySystem"
  \nodename = "MyNode"
  \release = "1.0"
  \version = "1"
  \machine = "MyMachine"
EndWith

With *test
  Debug \sysname 
  Debug \nodename 
  Debug \release 
  Debug \version 
  Debug \machine 
EndWith
Last edited by netmaestro on Mon Jun 04, 2007 10:11 pm, edited 1 time in total.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Code: Select all

Structure  utsname 
       sysname.s 
       nodename.s 
       release.s 
       version.s 
       machine.s 
EndStructure 

Global *test.utsname=AllocateMemory(SizeOf(utsname)) 
*test\sysname = "Sys"
*test\nodename = "Node"

Debug *test\sysname
Beware though that when freeing the memory pointed to by *test, the memory allocated to the individual strings will not be freed.

There's a thread somewhere in these forums which discusses this and shows a workaround.

**EDIT: too slow by far! :)
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Ha! I did them all and still beatcha!
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

netmaestro wrote:Ha! I did them all and still beatcha!
It's this damn dialup internet connection I'm having to use at the moment!

That's my excuse anyhow!
I may look like a mule, but I'm not a complete ass.
frederic
User
User
Posts: 56
Joined: Thu Jan 05, 2006 11:22 pm

Post by frederic »

Your example doesn't works with the function uname_

I've an Invalid Memory Access on this line " Debug \sysname"

Code: Select all

Structure  utsname 
       sysname.s
       nodename.s
       release.s
       version.s
       machine.s
EndStructure

Global *test.utsname=AllocateMemory(SizeOf(utsname)) 

uname_(*test) 

With *test
  Debug \sysname
  Debug \nodename
  Debug \release
  Debug \version
  Debug \machine
EndWith 
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

What is uname_() ?
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

The structure is probably wrong then, could they be fixedstrings or byte arrays? Something other than null-terminated strings, anyhow.
BERESHEIT
frederic
User
User
Posts: 56
Joined: Thu Jan 05, 2006 11:22 pm

Post by frederic »

uname_ is a linux function that retrieve some system information

Code: Select all

int uname(struct utsname *buf);  
DESCRIPTION
uname returns system information in the structure pointed to by buf. The utsname struct is defined in <sys/utsname.h>:

    struct utsname {
    char sysname[];
    char nodename[];
    char release[];
    char version[];
    char machine[];
    #ifdef _GNU_SOURCE
    char domainname[];
    #endif
    };

The length of the arrays in a struct utsname is unspecified; the fields are NUL-terminated.  
with this i've no error, but i don't know if it's correct

Code: Select all

Structure  utsname 
       sysname.s{255}
       nodename.s{255}
       release.s{255}
       version.s{255}
       machine.s{255}
EndStructure
Last edited by frederic on Mon Jun 04, 2007 10:19 pm, edited 1 time in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Indeed, we can now see that this is an entirely different kind of structure.

Thinking...
I may look like a mule, but I'm not a complete ass.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

another way:

Code: Select all

Structure utsname
	sysname.s
	nodename.s
	release.s
	version.s
	machine.s
EndStructure

Procedure uname_()
	Static MyStruct.utsname
	MyStruct\sysname = "sysname"
	MyStruct\nodename = "nodename"
	MyStruct\release = "release"
	MyStruct\version = "version"
	MyStruct\machine = "machine"
	ProcedureReturn @MyStruct
EndProcedure

Global *test.utsname = uname_()

Debug *test\sysname
Debug *test\nodename
Debug *test\release
Debug *test\version
Debug *test\machine
--Kale

Image
frederic
User
User
Posts: 56
Joined: Thu Jan 05, 2006 11:22 pm

Post by frederic »

ok with a scan of memory it appears that the data are (some datas :)) :

Code: Select all

0807CA74  4C 69 6E 75 78 00 00 00 00 00 00 00 00 00 00 00  Linux...........
0807CA84  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CA94  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAA4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAB4  00 50 75 72 65 66 72 69 65 6E 64 73 00 00 00 00  .Purefriends....
0807CAC4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAD4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAE4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAF4  00 00 32 2E 36 2E 32 30 2D 31 36 2D 67 65 6E 65  ..2.6.20-16-gene
0807CB04  72 69 63 00 00 00 00 00 00 00 00 00 00 00 00 00  ric.............
0807CB14  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CB24  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
so i must to count how many bytes there are for a string or "WE" know how many byte is a char xxxx[]; in linux language
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Not sure you've followed the thread correctly kale. uname_() is an external function.

frederick, I'd be a little surprised if the fixed length strings work if the structure in question is supposed to contain variable length strings with a null terminator.

The thing is about PB structures, string fields are stored as pointers rather than an array of charcaters embedded within the structure. This explains the invalid memory access error etc.

Fixed length strings are moving in the right direction in that the characters are stored directly within the structure etc. but here we stumble because of the the fact that your structures needs variable length strings etc.

I reckon you'll just have to allocate a large buffer and write your own code to read and write the string fields etc. Should be easy enough.
I may look like a mule, but I'm not a complete ass.
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The first time ever i've seen a structure like that...
Post Reply