LB Booster
« struct type ptr »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 04:17am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1 2 3  4 Notify Send Topic Print
 veryhotthread  Author  Topic: struct type ptr  (Read 3372 times)
joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx struct type ptr
« Thread started on: Nov 19th, 2015, 10:47am »

It is frustrating to read the "examples" in the LB Encyclopedia for things like "struct". The authors, in the case of "struct" seem to always avoid examples of how to use "ptr". There are examples on how to WRITE, but I can't seem to find an example where the author didn't avoid a GET example.

I've tried different ways to write to an INI and then read back from the INI without success. The "number" parts of "STRUCT" work fine, but I can't see how to recover the "PTR" part of "STRUCT".

EDIT: Perhaps nothing is wrong other than my thinking. It is saving a "PTR" to the string. I guess I was mis-thinking about saving/recovering a string value. :(

For instance :D :

Code:
'code by Richard Russell
'struct config, soundon as long, fullscreen as long 
'config.soundon.struct = 1 
'config.fullscreen.struct = 0 
struct config, soundon$ as ptr, fullscreen as long
SoundOn$ = "sound is on" 
config.soundon$.struct = SoundOn$
config.fullscreen.struct = 4

' locate the ini file
'print DefaultDir$   'no trailing backslash
'print StartupDir$   'has trailing backslash
if right$(DefaultDir$,1)<>"\" then
    DefaultDir$=DefaultDir$+"\"
end if
inifile$ = DefaultDir$ + "JunkINIstruct.ini"

print inifile$
print "Initial values: [soundon$ = ";config.soundon$.struct;"]  [fullscreen = ";config.fullscreen.struct;"]"
 
size = len(config.struct) 
calldll #kernel32, "WritePrivateProfileStructA", _ 
"settings" as ptr, _  ' Section name 
"config" as ptr, _    ' Key name 
config as struct, _   ' Structure 
size as ulong, _      ' Size of structure 
inifile$ as ptr, ret as long
print "ret from write: ";ret
SoundOn$ = ""
FullScreen = 0

'code by Richard Russell
size = len(config.struct) 
calldll #kernel32, "GetPrivateProfileStructA", _ 
"settings" as ptr, _  ' Section name 
"config" as ptr, _    ' Key name 
config as struct, _   ' Structure 
size as ulong, _      ' Size of structure 
inifile$ as ptr, ret as long
print "ret from get: ";ret

'  To retrieve the information from the struct:
'
'SoundOn$ = config.soundon$.struct  
FullScreen = config.fullscreen.struct
print "SoundOn$: ";SoundOn$
print "FullScreen: ";FullScreen
 


Richard! Help! :D
« Last Edit: Nov 19th, 2015, 11:04am by joker » User IP Logged

AAW
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: struct type ptr
« Reply #1 on: Nov 19th, 2015, 11:04am »

on Nov 19th, 2015, 10:47am, pnlawrence wrote:
It is frustrating to read the "examples" in the LB Encyclopedia for things like "struct". The authors, in the case of "struct" seem to always avoid examples of how to use "ptr". There are examples on how to WRITE, but I can't seem to find an example where the author didn't avoid a GET example.


Richard! Help! cheesy


http://lbpe.wikispaces.com/ABCs+of+APIs+10

http://lbpe.wikispaces.com/ABCs+of+APIs+10

User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: struct type ptr
« Reply #2 on: Nov 19th, 2015, 11:09am »

Search on LB forum (for "inifile") returns this:
Anyone having issues with GetPrivateProfileString?
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: struct type ptr
« Reply #3 on: Nov 19th, 2015, 11:09am »

on Nov 19th, 2015, 10:47am, pnlawrence wrote:
The "number" parts of "STRUCT" work fine, but I can't see how to recover the "PTR" part of "STRUCT".

I think you're misunderstanding what the PTR type is. It's an abbreviation for 'pointer', and what it means is that the struct contains a pointer to the string, not the string itself! So when you save the structure in an INI file and later read it back, the pointer is no doubt intact but the string it originally pointed to is no longer there!

What you need in your application is not the PTR type at all but the CHAR type. With the CHAR type the string really is stored in the structure, so it can be safely saved to, and read back from, an INI file (this code is compatible with both LB and LBB):

Code:
struct config, soundon$ as char[12], fullscreen as long
SoundOn$ = "sound is on" 
config.soundon$.struct = SoundOn$
config.fullscreen.struct = 4

' locate the ini file
'print DefaultDir$   'no trailing backslash
'print StartupDir$   'has trailing backslash
if right$(DefaultDir$,1)<>"\" then
    DefaultDir$=DefaultDir$+"\"
end if
inifile$ = DefaultDir$ + "JunkINIstruct.ini"

print inifile$
print "Initial values: [soundon$ = ";config.soundon$.struct;"]  [fullscreen = ";config.fullscreen.struct;"]"
 
size = len(config.struct) 
calldll #kernel32, "WritePrivateProfileStructA", _ 
"settings" as ptr, _  ' Section name 
"config" as ptr, _    ' Key name 
config as struct, _   ' Structure 
size as ulong, _      ' Size of structure 
inifile$ as ptr, ret as long
print "ret from write: ";ret
SoundOn$ = ""
FullScreen = 0

size = len(config.struct) 
calldll #kernel32, "GetPrivateProfileStructA", _ 
"settings" as ptr, _  ' Section name 
"config" as ptr, _    ' Key name 
config as struct, _   ' Structure 
size as ulong, _      ' Size of structure 
inifile$ as ptr, ret as long
print "ret from get: ";ret

'  To retrieve the information from the struct:
'
SoundOn$ = config.soundon$.struct  
FullScreen = config.fullscreen.struct
print "SoundOn$: ";SoundOn$
print "FullScreen: ";FullScreen 

I don't think Alyce's reference to winstring() is very relevant here, because the problem is not how to access the string in memory but that the string no longer is in memory (of course in the case of your test program it will be, but not in general when you are reading the INI file in a different session).

You could use Anatoly's suggestion of WritePrivateProfileString and GetPrivateProfileString, but it's a much more complicated modification. Changing PTR to CHAR[12] (in this case) is all you need to do. Make sure you allow enough space for the longest string you want to store, so (e.g.) CHAR[20] might be safer.

Richard.
« Last Edit: Nov 19th, 2015, 11:46am by Richard Russell » User IP Logged

AAW
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: struct type ptr
« Reply #4 on: Nov 19th, 2015, 12:20pm »

on Nov 19th, 2015, 11:09am, Richard Russell wrote:
I don't think Alyce's reference to winstring() is very relevant here,
Richard.


Nor was it meant to be.I did not even look at his code. I was addressing only the part of the message I quoted, in which pnlawrence said, "always avoid examples of how to use "ptr". There are examples on how to WRITE, but I can't seem to find an example where the author didn't avoid a GET example. "
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #5 on: Nov 19th, 2015, 12:54pm »

Two references to the same page! (I wish I had moderator privileges here so I could delete one of those references, because it is obviously frivolous. tongue )

Neither one of them has to do with an INI file, either.

on Nov 19th, 2015, 11:04am, AAW wrote:
http://lbpe.wikispaces.com/ABCs+of+APIs+10

http://lbpe.wikispaces.com/ABCs+of+APIs+10

User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #6 on: Nov 19th, 2015, 12:56pm »

Thanks, Richard. I did finally clear up with PTR (see my EDIT), but now I see a way to do the INI file. I just wanted a bit of obscurity in my INI file to help a user resist changes.

EDIT: I know why I didn't try CHAR[], here's what the LB help file lists as types:

Code:
Using types with STRUCT and CALLDLL

The STRUCT statement requires that each field be typed to specify what type of data it will contain. The CALLDLL statement also requires that each parameter passed be typed. The types are common to both STRUCT and CALLDLL. Simple data TYPES in Windows programming are often renamed versions of the types below. A program should specify the TYPE according to the chart below.

TYPES

  double(a double floating point)
  ulong(4 bytes, unsigned long integer)
  long(4 bytes, signed long integer)
  short(2 bytes, signed short integer)
  ushort, word(2 bytes, unsigned short integer)
  ptr(4 bytes, long pointer, for passing strings)
  struct(4 bytes, long pointer, for passing structs)
  void, none(a return type only, used when a function doesn't return a value)
  boolean(true/false expression)

 
« Last Edit: Nov 19th, 2015, 1:04pm by joker » User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #7 on: Nov 19th, 2015, 1:15pm »

Well, changing PTR to CHAR[12] didn't change much.

I also tried adding CHR$(0) to the end of the string without change.

Here's what's in the INI file:

Quote:
[settings]
config=736F756E64206973206F6E000400000026


I must have screwed up something different. cheesy
« Last Edit: Nov 19th, 2015, 1:17pm by joker » User IP Logged

AAW
New Member
Image


member is offline

Avatar




PM


Posts: 22
xx Re: struct type ptr
« Reply #8 on: Nov 19th, 2015, 1:21pm »

on Nov 19th, 2015, 12:54pm, pnlawrence wrote:
Two references to the same page! (I wish I had moderator privileges here so I could delete one of those references, because it is obviously frivolous. tongue )

Neither one of them has to do with an INI file, either.



I apologize for duplicating the link. I hope it didn't cause too much confusion.

Did you read the post directly above yours? I responded to Richard.

Quote:
I did not even look at his code. I was addressing only the part of the message I quoted, in which pnlawrence said, "always avoid examples of how to use "ptr". There are examples on how to WRITE, but I can't seem to find an example where the author didn't avoid a GET example. "
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #9 on: Nov 19th, 2015, 1:27pm »

Aww, come on AAW. I'm just messing with you. cheesy

I'm from Texas. We have a saying here, "If you mess with the bull, don't be surprised if you get the horns!" cheesy
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #10 on: Nov 19th, 2015, 1:28pm »

I finally figured out that I had remarked-out the line where I assigned the string the value from the struct.

Works just fine now.

Thanks!

EDIT: Wow! I didn't realize the string would be tokenized in the INI file. That's even better.
EDIT2: Arrg! I forgot that my string was all lower case ASCII. Still it "looks" tokenized. :D
Code:
[settings]
config=736F756E64206973206F6E000000000000000000000000000000000000000400000026
 
« Last Edit: Nov 19th, 2015, 1:36pm by joker » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: struct type ptr
« Reply #11 on: Nov 19th, 2015, 2:02pm »

on Nov 19th, 2015, 1:15pm, pnlawrence wrote:
Well, changing PTR to CHAR[12] didn't change much.

It does, it fixes the problem completely. I also explained in detail why the version with PTR cannot work, and why the version with CHAR does work. I listed the modified program in my post, and I tested it both in LB and LBB, and it works perfectly.

I must say I am disappointed that you should think I would take so little care as to post something which doesn't work.

Richard.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #12 on: Nov 19th, 2015, 2:08pm »

Chill out, Richard and read ALL of my posts. embarassed
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: struct type ptr
« Reply #13 on: Nov 19th, 2015, 2:39pm »

on Nov 19th, 2015, 2:08pm, pnlawrence wrote:
I know why I didn't try CHAR[], here's what the LB help file lists as types

It is certainly extremely strange that CHAR isn't mentioned at all in the LB Help file (as far as I have been able to find), and stranger still that it wasn't added in the recently updated LB 4.5.0 docs. It isn't some minor 'feature' or subtlety that would benefit from being clarified, but an essential language element without which many Windows API functions couldn't readily be used.

I hope the explanation isn't that Carl knows it to be unreliable. There is one issue that I am aware of (fixed in LBB) which is that CHAR[1] is effectively unusable in LB 4.04 and 4.5.0. This code works in LBB but not in LB:

Code:
    struct test, c$ as char[1]
    test.c$.struct = "a"
    print test.c$.struct 

The reason for the failure in LB is that strings stored in a CHAR member are assumed to be NUL-terminated, and if there's space for only a single character there is no room for the termination.

LBB treats this as a special case. If the member is declared as CHAR[2] or larger then a NUL-termination is included just the same as with LB, so CHAR[12] will hold at most an 11-character string. But with CHAR[1] no NUL-termination is added, ensuring that a single-byte structure member can be used successfully.

Richard.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #14 on: Nov 19th, 2015, 4:17pm »

Mr. Gundel needs to hire me to be his "bumbling around and finding problems" guru. cheesy

Along with describing my problematic experience with installing LB, this discovery of the missing "CHAR[i]" type makes two bumbles.

I'd have to charge way too much per bumble to make it a commercial endeavor, though. cheesy
User IP Logged

Pages: 1 2 3  4 Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls