String can't be built

Just starting out? Need help? Post your questions and find answers here.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

String can't be built

Post by PB »

Check this out... it's supposed to get a hard drive serial number and then
show it raw, and with a dash in the middle. But the dash never appears.
In fact, depending on which drive letter I give it, the dash is replaced by
either null, one character, or many characters.

It seems the GetVolumeInformation API is corrupting the strings, including
the second string which is in no way related to the API call in any way.

And it's not just dashes, it can be any character(s) in the middle.

Code: Select all

GetVolumeInformation_("C:\","",255,@s,0,0,"",255)
s$=Hex(s)
Debug s$ ; Raw.
Debug Left(s$,4)+"-"+Right(s$,4) ; Where's the dash?

t$="ABCDEFGH"
Debug Left(t$,4)+"-"+Right(t$,4) ; Also no dash with this different string!
Here's the Debug Output results that I get when I use different drive letters.
As you can see, no dash anywhere to be seen in any output. Weird!

Code: Select all

C:
A488DF69
A488DF69
ABCDEFGH

Code: Select all

D:
F006ED99
F006sED99
ABCDsEFGH

Code: Select all

E:
50714F26
5071ams4F26
samss
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
magicjo
User
User
Posts: 61
Joined: Sun May 07, 2006 10:43 am
Location: Italy

Post by magicjo »

The 4°th parameter is not a pointer to string, the function returns the serial number as pointer to long.
PB Registered User, Egrid Registered User
Win7 x64 Ultimate, 4,00 Gb Mem, Ati Radeon HD4600 Series, Realtek High Definition Audio Integrated
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> The 4°th parameter is not a pointer to string

I know. That's why I've got it going to a long called "s".

[Edit] The 5th parameter was the cause; see my post further below.
Last edited by PB on Sun Jan 13, 2008 10:14 am, edited 1 time in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
maw

Post by maw »

It works just fine here with PB4.20B1, I see the dash in both examples.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

I'm also running v4.20 Beta 1. :(

So I did a clean install of v4.10 Final to a new folder, ran the v4.20 Beta 1
update tool on top of it, ran PureBasic.exe with the code, and still got it:

Image
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Okay, I just found out that the FIFTH parameter shouldn't be 0, so I changed
it to 255 like so, and it all works:

Code: Select all

GetVolumeInformation_("C:\","",255,@s,255,0,"",255)
Now I get the dash as expected. BUT, I still think this is a bug because surely
an invalid parameter in an API call shouldn't break general string handling?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
magicjo
User
User
Posts: 61
Joined: Sun May 07, 2006 10:43 am
Location: Italy

Post by magicjo »

#pb, sorry,i've read the post while still sleeping :D ,
the string corruption happens to me when compile in unicode,is your case ?
PB Registered User, Egrid Registered User
Win7 x64 Ultimate, 4,00 Gb Mem, Ati Radeon HD4600 Series, Realtek High Definition Audio Integrated
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

No, I compile in ANSI (the PureBasic default), not Unicode.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

GetVolumeInformation_("C:\","",255,@s,255,0,"",255)
The first parameter is correct.
The second and third parameter is wrong. The second parameter should be a buffer, the third parameter indicates the length of that buffer. In your case, the buffer is 0 characters (empty string), but you tell the API function that the buffer is 255 characters! When the API function thinks it writes to the buffer it actually writes to some other random memory (probably the next static strings, like the dash). No wonder it breaks.
The fourth parameter is ok.
The fifth parameter is wrong. It should also be a pointer to a long to receive some information. Now the API function will write to address 255 unless it checks for unlikely pointer values.
The sixth parameter is wrong. It should be a pointer to a long, not 0.
The seventh and eigth parameter is wrong. The problem is the same as with the second and third: You give a buffer that is 0 characters, but say it is 255 characters.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Thanks for your detailed info, Trond. I've done an update, which seems correct?

Code: Select all

tmp1$=Space(255) : tmp2$=tmp1$
GetVolumeInformation_("C:\",tmp1$,255,@serial,@tmp1,@tmp2,tmp2$,255)
s$=Hex(serial)
Debug s$
Debug Left(s$,4)+"-"+Right(s$,4)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Yes, seems correct.
Post Reply