Declaring & Variable already declared.. Message

Just starting out? Need help? Post your questions and find answers here.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Here's an example of the use.

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
Prototype GetNativeSystemInfo(*lpSystemInfo.SYSTEM_INFO)

#PROCESSOR_ARCHITECTURE_AMD64 = 9
#PROCESSOR_ARCHITECTURE_IA64  = 6
#PROCESSOR_ARCHITECTURE_INTEL = 0
#PROCESSOR_ARCHITECTURE_UNKNOWN = $ffff

Procedure Is64bitOS()
  Protected kernel32, GetNativeSystemInfo.GetNativeSystemInfo
  Protected info.SYSTEM_INFO
  Protected Result = #False
  
  kernel32 = OpenLibrary(#PB_Any, "Kernel32.dll")
  If kernel32
    GetNativeSystemInfo = GetFunction(kernel32, "GetNativeSystemInfo")
    
    If GetNativeSystemInfo
      GetNativeSystemInfo(@info)
      If info\wProcessorArchitecture <> #PROCESSOR_ARCHITECTURE_INTEL ; x86
        Result = #True
      EndIf      
    EndIf
  
    CloseLibrary(kernel32)
  EndIf  

  ProcedureReturn Result
EndProcedure

  Structure LV_ITEM1 Align 2 
    mask.l
    iItem.l
    iSubItem.l
    state.l
    stateMask.l
    Padding.l
    *pszText
    Padding1.l
    cchTextMax.l
    iImage.l
    lParam.l
    Padding2.l
  EndStructure
CompilerEndIf

; EnableExplicit
Structure DeskInfo
  sz.i
  name.s
  pt.point
EndStructure

Global Dim GlobalDeskinfo.DeskInfo(0)
Global Explorer
Global GlobalLVItem
Global GlobalBuffer

Procedure DesktopHandle()
  Protected hProgman.l, hDesktopWnd.l, hShellViewWin.l, hWorkerW.l, hDesktopListView.l
  
  hProgman = FindWindow_("ProgMan", 0)
  hDesktopWnd = GetDesktopWindow_()
  
  If hProgman
    
    hShellViewWin = FindWindowEx_(hProgman, 0, "SHELLDLL_DefView", 0);
    If hShellViewWin
      hDesktopListView = FindWindowEx_(hShellViewWin, 0, "SysListView32", 0);
    Else
      
      Repeat
        hWorkerW = FindWindowEx_( hDesktopWnd, hWorkerW, "WorkerW", #Null );
        hShellViewWin = FindWindowEx_(hWorkerW, 0, "SHELLDLL_DefView", 0);
      Until hShellViewWin <> #Null And hWorkerW <> #Null
      
      hDesktopListView = FindWindowEx_(hShellViewWin, 0, "SysListView32", 0);      
    EndIf	 
    
    ProcedureReturn hDesktopListView
  EndIf
EndProcedure

Procedure DesktopProcessOpen()
  Protected ExplorerID
  Protected Flags = #PROCESS_VM_OPERATION | #PROCESS_VM_READ | #PROCESS_VM_WRITE | #PROCESS_QUERY_INFORMATION
  
  GetWindowThreadProcessId_(DesktopHandle(),@ExplorerID)
  Explorer = OpenProcess_(Flags,#Null,ExplorerID)
  GlobalLVItem = VirtualAllocEx_(Explorer,#Null,SizeOf(LV_ITEM),#MEM_COMMIT,#PAGE_READWRITE )
  GlobalBuffer = VirtualAllocEx_(Explorer,#Null,#MAX_PATH,#MEM_COMMIT,#PAGE_READWRITE)  
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    If Is64bitOS()
      
      ItemsB.LV_ITEM1      
      With ItemsB
        \mask       = #LVIF_TEXT
        \iSubItem   = 0
        \pszText    = GlobalBuffer
        \cchTextMax = #MAX_PATH
      EndWith : WriteProcessMemory_(Explorer,GlobalLVItem,ItemsB,SizeOf(ItemsB),#Null)
      ProcedureReturn 1      
    EndIf
  CompilerEndIf 
  
  ItemsA.LV_ITEM
  With ItemsA
    \mask       = #LVIF_TEXT
    \iSubItem   = 0
    \pszText    = GlobalBuffer
    \cchTextMax = #MAX_PATH
  EndWith : WriteProcessMemory_(Explorer,GlobalLVItem,ItemsA,SizeOf(ItemsA),#Null)
  
EndProcedure

Procedure DesktopProcessClose ( )
  VirtualFreeEx_(Explorer,GlobalLVItem,#Null,#MEM_RELEASE)
  VirtualFreeEx_(Explorer,GlobalBuffer,#Null,#MEM_RELEASE)
  CloseHandle_(Explorer)
EndProcedure

Procedure DesktopIconCount ( )
  ProcedureReturn SendMessage_(DesktopHandle(),#LVM_GETITEMCOUNT,#Null,#Null)
EndProcedure

Procedure.s DesktopGetIconString(Index)
  Protected String$=Space(#MAX_PATH)
  
  SendMessage_(DesktopHandle(),#LVM_GETITEMTEXT,Index,GlobalLVItem)
  ReadProcessMemory_(Explorer,GlobalBuffer,@String$,#MAX_PATH,#Null)
  
  ProcedureReturn String$   
EndProcedure

Procedure DesktopGetIconPosition(index,*pt.point)
  If SendMessage_(DesktopHandle(),#LVM_GETITEMPOSITION,index,GlobalLVItem )
    ReadProcessMemory_(Explorer,GlobalLVItem,*pt,SizeOf(Point),#Null)
    ProcedureReturn #True
  Else 
    ProcedureReturn #False
  EndIf   
EndProcedure

Procedure DesktopSetIconPos(Index,X,Y)
  ProcedureReturn SendMessage_(DesktopHandle(),#LVM_SETITEMPOSITION,Index,(Y<<16)|(X&$FFFF))
EndProcedure

Procedure SaveDesktopIconsPositions()
  
  Protected fn,Count,index,pt.point,name.s 
  
  fn = OpenFile(-1,"desk.bin")
  
  If fn 
    DesktopProcessOpen()
    Count = DesktopIconCount()
    ReDim GlobalDeskinfo.DeskInfo(count)
    
    WriteData(fn,@count,SizeOf(Integer)) 
    
    For index = 0 To Count - 1
      
      GlobalDeskinfo(index)\name = DesktopGetIconString(index)  :  Debug GlobalDeskinfo(index)\name
      GlobalDeskinfo(index)\sz = StringByteLength(GlobalDeskinfo(index)\name)      
      
      If DesktopGetIconPosition(index,pt)
        GlobalDeskinfo(index)\pt\x = pt\x 
        GlobalDeskinfo(index)\pt\y = pt\y 
      EndIf   
      
      WriteData(fn,@GlobalDeskinfo(index)\sz,SizeOf(Integer))
      WriteData(fn,@GlobalDeskinfo(index)\name,GlobalDeskinfo(index)\sz)
      WriteData(fn,@GlobalDeskinfo(index)\pt,SizeOf(point))
    Next 
    CloseFile(fn)
  EndIf
  
  DesktopProcessClose()
EndProcedure 

Procedure RestoreDesktopIconsPositions()
  Protected count,*buf,name.s,len,pt.point,fn,index  
  *buf = AllocateMemory(#MAX_PATH) 
  count = ArraySize(GlobalDeskinfo())
  
  DesktopProcessOpen()
  If count > 0 
    For index = 0 To count 
      DesktopSetIconPos(index,GlobalDeskinfo(index)\pt\x,GlobalDeskinfo(index)\pt\y) 
    Next 
  Else 
    fn = ReadFile(-1,"desk.bin") 
    If fn 
      ReadData(fn,@count,SizeOf(Integer)) 
      If count > 0  
        For index = 0 To count 
          ReadData(fn,@len,SizeOf(Integer))
          ReadData(fn,*buf,len) 
          name = PeekS(*buf,len) 
          ReadData(fn,@PT,SizeOf(point)) 
          If DesktopGetIconString(index) = name 
            DesktopSetIconPos(index,pt\x,pt\y)
          EndIf 
        Next
      EndIf 
      CloseFile(fn)
    EndIf
  EndIf   
  DesktopProcessClose()
  FreeMemory(*buf) 
  
EndProcedure 

Define Event

OpenWindow(0,200,200,140,30,"desk Icons",#PB_Window_SystemMenu)

ButtonGadget(1,5,5,60,20,"save")
ButtonGadget(2,70,5,60,20,"restore") 

Repeat
  Event = WaitWindowEvent()
  
  Select Event      
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_LeftClick
          Select EventGadget()
            Case 1 : Debug "Saving"+Chr(10) : SaveDesktopIconsPositions()
            Case 2 : Debug "Restoring"+Chr(10) : RestoreDesktopIconsPositions()
          EndSelect          
      EndSelect
  EndSelect
Until Event = #WM_CLOSE
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

I was prolonging things... But I keep smacking into this wall with different projects.

Working with ListView structures, and Vista and newer uses extended structure of LVGROUP. Therefore..., I guess I'll have to see if I can follow your advise and see if that works. :?
Demivec wrote: 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.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply