Page 1 of 2
Declaring & Variable already declared.. Message
Posted: Wed Feb 12, 2014 11:43 pm
by Thunder93
Hi.
I'm hitting my head up against a wall trying to figure this out. I feel that I should at least know how to-do this, but I'm stumped.
My dilemma..
I make 32App, but to have it support 64bit OS, I have to include 64bit supported structure.
My ignorance is to declaring. If the 32App is running on 64bit OS, use Item.StructB, otherwise use Item.StructA. The Variable name is depended on so It needs to remain the same but declared to either structure based on criteria.
And I read the structure from variable name such as Item. The name must not change but a way to declare differently with alternative structure and both in an If and else cases. Currently I would be receiving already declared message.
Concept code:
Code: Select all
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
If Is64bitOS() : Protected Item.StructB : Else : Protected Item.StructA : EndIf
CompilerElse
Protected Item.StructA
CompilerEndIf
Re: Declaring & Variable already declared.. Message
Posted: Thu Feb 13, 2014 12:16 am
by JHPJHP
Not sure about the following, but I'm interested to see where this thread leads:
Code: Select all
Structure Struct32
test.l
EndStructure
Structure Struct64
test.q
EndStructure
Procedure GetStructure32()
Protected Item.Struct32
ProcedureReturn Item
EndProcedure
Procedure GetStructure64()
Protected Item.Struct64
ProcedureReturn Item
EndProcedure
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
*Item.Struct32 = GetStructure32()
CompilerElse
*Item.Struct64 = GetStructure64()
CompilerEndIf
*Item = AllocateMemory(SizeOf(*Item))
*Item\test = SizeOf(*Item\test)
Debug *Item\test
FreeMemory(*Item)
Re: Declaring & Variable already declared.. Message
Posted: Thu Feb 13, 2014 12:28 am
by luis
@JHP
I think the problem is he wants a single variable being one of two different types selected at runtime, so conditional compilation doesn't help.
I'm not sure I have understood well, but something like this would be acceptable ?
Code: Select all
Procedure Is64bitOS()
ProcedureReturn 0 ; change this
EndProcedure
Structure Struct32 ; 32 bit structure
field.l
EndStructure
Structure Struct64 ; 64 bit structure
field.q
EndStructure
Structure Universal ; union
StructureUnion
S32.Struct32
S64.Struct64
EndStructureUnion
EndStructure
Global Item.Universal ; "Item" is always the same so it's simple declare-wise
; but you have to differentiate it somewhere, maybe it's not a problem ? looks clean enough
If Is64bitOS()
Debug SizeOf(Item\S64)
Item\S64\field = 1
Else
Debug SizeOf(Item\S32)
Item\S32\field = 1
EndIf
Re: Declaring & Variable already declared.. Message
Posted: Thu Feb 13, 2014 1:00 am
by Thunder93
Hey JHPJHP.
Thanks. However the code you provided has limits, one stated by luis.
@luis: Thanks. The problem still exists I'd have to duplicate bunch of code in-order to still be supporting 64bit OS from my 32bit App. I'm still hoping for a convenient method, and avoid lot of extra footprint.
Re: Declaring & Variable already declared.. Message
Posted: Thu Feb 13, 2014 2:41 pm
by Thunder93
Really appreciate you guys helping me, trying to figure this out. I really hope there is a way to-do this with PureBasic.
Currently I could, simply use;
Protected Item2.StructB ;My included x64 Structure to be using when running my 32bit App on x64 OS.
Protected Item1.StructA ;[Pointing to native Structure]
But still comes back to duplicating code and making it twice as big in PB. I should be-able declare Item1 or Item2 based on criteria ..as Item and have my later code dependant on that.
Re: Declaring & Variable already declared.. Message
Posted: Thu Feb 13, 2014 11:06 pm
by jack
how about
Code: Select all
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Protected Item.StructB
CompilerElse
Protected Item.StructA
CompilerEndIf
Re: Declaring & Variable already declared.. Message
Posted: Thu Feb 13, 2014 11:16 pm
by Thunder93
Hi jack.
That won't work for my needs. See my original posted code. Needing single variable being one of two different types selected at runtime.
Re: Declaring & Variable already declared.. Message
Posted: Fri Feb 14, 2014 5:35 am
by Harry0
Would something like this work for you:
Code: Select all
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
If Is64bitOS() : Protected *Item = @ItemB.StructB : Else : Protected *Item = @ItemA.StructA : EndIf
CompilerElse
Protected *Item = @ItemA.StructA
CompilerEndIf
If it is memory space that you want to minimize then try this:
Code: Select all
Structure StructA
Thing1.i
EndStructure
Structure StructB
Thing1.q
EndStructure
Structure items2
StructureUnion
ItemA.StructA
ItemB.StructB
EndStructureUnion
EndStructure
Procedure test()
Items.items2
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
If Is64bitOS() : Protected *Item = @Items\ItemB : Else : Protected *Item = @Items\ItemA : EndIf
CompilerElse
Protected *Item = @Items\ItemA
CompilerEndIf
EndProcedure
And then you just need to reference/use *Item.
NOTE: Have not tested this code but it does pass a syntax check.
Hope that helps!
Harry0
Re: Declaring & Variable already declared.. Message
Posted: Fri Feb 14, 2014 6:26 am
by Thunder93
Hi Harry0.
Still faced with the variable already declared message ...
Error: Line 26 - Local variable already declared: *Item.
... you only going to receive this message if you testing with the PB x86 compiler
And trying with PB x64, I receive...
Line ???: The following variable doesn't have a 'Structure': *Item ... *Item\MyField1 = "Test"
Re: Declaring & Variable already declared.. Message
Posted: Fri Feb 14, 2014 6:49 am
by Danilo
@Thunder93:
Could you please first explain why you need a different structure when the 32bit
program is running on a 64bit OS? It is still a 32bit program and can't access 64bit
stuff anyway, so why would you need a special 64bit structure in a 32bit program?
Re: Declaring & Variable already declared.. Message
Posted: Fri Feb 14, 2014 7:12 am
by Thunder93
Hi Danilo.
32Bit App isn't as limited on x64 OS as you might think. I already know that using 64bit structure in my 32bit App running on x64 OS ... will work.
I actually demonstrated the need w/my last post on 'how enumerate all desktop icons (name/label) ?' -
http://www.purebasic.fr/english/viewtop ... 15#p437640
Re: Declaring & Variable already declared.. Message
Posted: Fri Feb 14, 2014 4:56 pm
by Thunder93
Is the verdict in? Does this need to be a feature request? No other avenues to explore?

Re: Declaring & Variable already declared.. Message
Posted: Fri Feb 14, 2014 5:45 pm
by Demivec
This should make the code Harry0 posted compile:
Code: Select all
Structure StructA
Thing1.i
EndStructure
Structure StructB
Thing1.q
EndStructure
Structure items2
StructureUnion
ItemA.StructA
ItemB.StructB
EndStructureUnion
EndStructure
Procedure test()
Items.items2
Protected *Item
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
If Is64bitOS() : *Item = @Items\ItemB : Else : *Item = @Items\ItemA : EndIf
CompilerElse
*Item = @Items\ItemA
CompilerEndIf
EndProcedure
The code still has other problems. One of these is that it can't tell which memory layout to use, ItemA or ItemB, for any of the references to the substructure.
You could use an interface and a newItem() function. The newItem() function would reserve the memory for the correct structure (depending on the OS) and then the interfaces functions would set and retrieve the items values. You would also have to destroy the 'object' through another interface call (i.e. before exiting a procedure).
This would limit any duplication to basically the small amount of code in the interface without having to spread it throughout your program if items are referenced from many different areas. It does some add additional statements to create and free the variable's memory and require using the interface to interact with the values.
Re: Declaring & Variable already declared.. Message
Posted: Sat Feb 15, 2014 1:21 am
by Thunder93
Thanks Demivec.
You are pointing into an direction I have not explored before... I'll look towards the angels and pray that my wish comes true.

Re: Declaring & Variable already declared.. Message
Posted: Sun Feb 16, 2014 2:50 pm
by Demivec
Thunder93 wrote:Thanks Demivec.
You are pointing into an direction I have not explored before... I'll look towards the angels and pray that my wish comes true.

You're welcome. Will you post the structure's details? I can share a more meaningful code example if you do.