LB Booster
« struct type ptr »

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



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 3382 times)
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

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: struct type ptr
« Reply #15 on: Nov 19th, 2015, 4:51pm »

on Nov 19th, 2015, 4:17pm, pnlawrence wrote:
Mr. Gundel needs to hire me to be his "bumbling around and finding problems" guru. cheesy

Although you probably don't want a serious answer to a frivolous post, there's little evidence that Carl cares. angry

In 'updating' LB from 4.04 to 4.5.0 he has failed to address the majority of bugs recorded at the Liberty BASIC Bug Tracker Wiki. I would have expected him to go through that list and either fix every bug or explain why he is unable or unwilling to do so. I am prepared to accept that some bugs may be considered insufficiently important to warrant the time and effort it would take to fix them, and even that some are impractical to fix because of limitations in his tools, but we should be told that.

Instead, major bugs (like TIMER being unable to use a SUB handler) remain unfixed in LB 4.5.0 with no explanation.

Richard.
« Last Edit: Nov 19th, 2015, 4:53pm by Richard Russell » User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

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

After trying to compile the previous code in LB, the compiler error message related that CHAR[12] is an illegal type.

Not a type and not even a reserved word.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

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

I would like to do something like this:

Code:
function WriteINIStruct1(SectionName$,KeyName$,KeyChars$,inifile$)
    ' Returns true (1) if nothing wrong or false (0) if a problem
    'code by Richard Russell
    size = len(KeyChars$) * 2 + 1
    struct structName, keychars$ as CHAR[size]
    structName.keychars$.struct = KeyChars$
    size = len(structName.struct) 
    calldll #kernel32, "WritePrivateProfileStructA", _ 
    SectionName$ as ptr, _  ' Section name 
    KeyName$ as ptr, _    ' Key name 
    structName as struct, _   ' Structure 
    size as ulong, _      ' Size of structure 
    inifile$ as ptr, ret as long
    WriteINIStruct = ret
end function
 


However, LBB seems to choke on this with a "struct not defined error". Either I can't define the struct within the function or it doesn't like the variable within the CHAR[ statement. I can't tell which.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: struct type ptr
« Reply #18 on: Nov 19th, 2015, 5:42pm »

on Nov 19th, 2015, 5:19pm, pnlawrence wrote:
After trying to compile the previous code in LB, the compiler error message related that CHAR[12] is an illegal type.

I don't get an error message from either LB 4.04 or LB 4.5.0 with the code I listed. Are you definitely using exactly the code I listed in my earlier post?

Quote:
I would like to do something like this:
Code:
struct structName, keychars$ as CHAR[size] 

You can't. LB and LBB must know the size of the STRUCT at compile time, so you can't specify something like CHAR[size] because then the size is known only at run time. This is quite normal in other languages too.

You should specify whatever the maximum required size is, so if your string has a maximum length of 255 characters specify CHAR[256] (including the terminating NUL).

Richard.
« Last Edit: Nov 19th, 2015, 5:53pm by Richard Russell » User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

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

I probably had the CHAR[size] code in the first trial. I know where to go now.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: struct type ptr
« Reply #20 on: Nov 21st, 2015, 12:43am »

I can make this code operate, but I can't make the "Get" work as a FUNCTION by returning the value. I had to pass a variable BYREF. It works, but wasn't my goal.

I speculate that since I am passing the STRUCT name that the FUNCTION is working on a copy of STRUCT. Interesting.

Any ideas on returning the STRUCT value with a FUNCTION?

Also, I found out that CHAR[10] really only stores a string of 9 characters. I guess the rest is overhead? I don't understand why specifying [10] in the code doesn't save 10.

OUTPUT LOOKS LIKE THE FOLLOWING:

Code:
[settings]
config=30313233343536373839000D 


Code:
if right$(DefaultDir$,1)<>"\" then
    DefaultDir$=DefaultDir$+"\"
end if
inifile$ = DefaultDir$ + "JunkINIstruct.ini"

print inifile$

struct structName, keychars$ as CHAR[11] ' if CHAR[10] you can only write 9 characters

ret = WriteINIStruct1("settings","config","0123456789",structName,inifile$)
print "ret from write: ";ret

'structName.keychars$.struct = ""
'print "test struct clear: "+structName.keychars$.struct

ret = GetINIStruct1("settings","config",GetKeyChars$,structName,inifile$)
print "ret from get: ";ret
print "Got this: ->"+GetKeyChars$+"<- (from GetINIStruct1)"

end

function WriteINIStruct1(SectionName$,KeyName$,KeyChars$,structName,inifile$)
    ' Returns true (1) if nothing wrong or false (0) if a problem
    'code by Richard Russell
    structName.keychars$.struct = KeyChars$
    size = len(structName.struct) 
    calldll #kernel32, "WritePrivateProfileStructA", _ 
    SectionName$ as ptr, _  ' Section name 
    KeyName$ as ptr, _    ' Key name 
    structName as struct, _   ' Structure 
    size as ulong, _      ' Size of structure 
    inifile$ as ptr, ret as long
    WriteINIStruct1 = ret
end function

function GetINIStruct1(SectionName$,KeyName$,byref GetKeyChars$,structName,inifile$)
    ' Returns true (1) if nothing wrong or false (0) if a problem
    'code by Richard Russell
    size = len(structName.struct) 
    calldll #kernel32, "GetPrivateProfileStructA", _ 
    SectionName$ as ptr, _  ' Section name 
    KeyName$ as ptr, _    ' Key name 
    structName as struct, _   ' Structure 
    size as ulong, _      ' Size of structure 
    inifile$ as ptr, ret as long
    GetKeyChars$ = structName.keychars$.struct
    'GetINIStruct1 = structName.keychars$.struct ' this won't work. "Type mismatch" error.
    GetINIStruct1 = ret
end function
 
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: struct type ptr
« Reply #21 on: Nov 21st, 2015, 03:35am »

on Nov 21st, 2015, 12:43am, pnlawrence wrote:
I can't make the "Get" work as a FUNCTION by returning the value.

I wonder if the problem is as simple as forgetting that a function which returns a string must itself have a name ending in a dollar. So for example the function might be called GetINIStruct1$().

Quote:
I speculate that since I am passing the STRUCT name that the FUNCTION is working on a copy of STRUCT.

STRUCTs in Liberty BASIC are always global. You cannot pass a struct as a parameter to a SUB or FUNCTION. Is that what you were hoping to be able to do?

Quote:
I don't understand why specifying [10] in the code doesn't save 10.

You berated me the other day for not reading all of your messages, now you're not reading all of mine! I wrote: "strings stored in a CHAR member are assumed to be NUL-terminated ... so CHAR[12] will hold at most an 11-character string."

Richard.
« Last Edit: Nov 21st, 2015, 04:07am by Richard Russell » 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