Why does it work? (filemapping)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Why does it work? (filemapping)

  1. #1
    Juergen Thuemmler Guest

    Why does it work? (filemapping)

    Hi *.*,

    for testing purposes I've written the little program below (VB5) which
    gets the name and position of desktop items in Win95/98. Normally, it should
    not work, because I give the view of a file mapping object (an address in my
    process) to the explorer instead of the LV_ITEM structure. Well, in
    Win95/98, the address of a view of a file mapping object will be the same in
    all processes which create or open a view to this object. But the explorer
    doesn't know about opening a view ...
    Anyway, it works. But why???

    Juergen.

    '***************************************************************************
    ***************
    This is the content of "Form1.frm" file - the only one in the project ...
    '***************************************************************************
    ***************
    VERSION 5.00
    Begin VB.Form Form1
    BorderStyle = 1 'Fest Einfach
    Caption = "Filemapping Sample"
    ClientHeight = 2715
    ClientLeft = 2850
    ClientTop = 2640
    ClientWidth = 2325
    LinkTopic = "Form1"
    LockControls = -1 'True
    MaxButton = 0 'False
    MinButton = 0 'False
    ScaleHeight = 2715
    ScaleWidth = 2325
    Begin VB.TextBox Text1
    BeginProperty Font
    Name = "MS Sans Serif"
    Size = 8.25
    Charset = 0
    Weight = 700
    Underline = 0 'False
    Italic = 0 'False
    Strikethrough = 0 'False
    EndProperty
    Height = 330
    Left = 1575
    TabIndex = 2
    Text = "0"
    Top = 450
    Width = 420
    End
    Begin VB.CommandButton Command1
    Caption = "Get Desktop Item Name and Position "
    Default = -1 'True
    Height = 645
    Left = 315
    TabIndex = 0
    Top = 1890
    Width = 1680
    End
    Begin VB.Label Label4
    Caption = "Item Number:"
    BeginProperty Font
    Name = "MS Sans Serif"
    Size = 8.25
    Charset = 0
    Weight = 700
    Underline = 0 'False
    Italic = 0 'False
    Strikethrough = 0 'False
    EndProperty
    Height = 240
    Left = 315
    TabIndex = 6
    Top = 495
    Width = 1140
    End
    Begin VB.Label Label3
    Caption = "Y = "
    BeginProperty Font
    Name = "MS Sans Serif"
    Size = 8.25
    Charset = 0
    Weight = 700
    Underline = 0 'False
    Italic = 0 'False
    Strikethrough = 0 'False
    EndProperty
    Height = 240
    Index = 1
    Left = 1170
    TabIndex = 5
    Top = 1485
    Width = 780
    End
    Begin VB.Label Label3
    Caption = "X = "
    BeginProperty Font
    Name = "MS Sans Serif"
    Size = 8.25
    Charset = 0
    Weight = 700
    Underline = 0 'False
    Italic = 0 'False
    Strikethrough = 0 'False
    EndProperty
    Height = 240
    Index = 0
    Left = 315
    TabIndex = 4
    Top = 1485
    Width = 780
    End
    Begin VB.Label Label2
    Caption = "Max. Item-No.: "
    BeginProperty Font
    Name = "MS Sans Serif"
    Size = 8.25
    Charset = 0
    Weight = 700
    Underline = 0 'False
    Italic = 0 'False
    Strikethrough = 0 'False
    EndProperty
    Height = 195
    Left = 315
    TabIndex = 3
    Top = 135
    Width = 1725
    End
    Begin VB.Label Label1
    Alignment = 2 'Zentriert
    BackColor = &H00FFFFFF&
    BeginProperty Font
    Name = "MS Sans Serif"
    Size = 8.25
    Charset = 0
    Weight = 700
    Underline = 0 'False
    Italic = 0 'False
    Strikethrough = 0 'False
    EndProperty
    Height = 420
    Left = 315
    TabIndex = 1
    Top = 900
    Width = 1650
    WordWrap = -1 'True
    End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    '***************************************************************************
    ***************
    Option Explicit
    '***************************************************************************
    ***************
    Private Type POINTAPI
    X As Long
    Y As Long
    End Type
    '-------------------------------------------------
    Private Type LV_ITEM
    mask As Long
    iItem As Long
    iSubItem As Long
    State As Long
    stateMask As Long
    pszText As Long
    cchTextMax As Long
    iImage As Long
    lParam As Long
    End Type 'LV_ITEMA
    '-------------------------------------------------
    'Private Type SECURITY_ATTRIBUTES
    ' nLength As Long
    ' lpSecurityDescriptor As Long
    ' bInheritHandle As Long
    'End Type
    '-------------------------------------------------
    Private Const LVIF_TEXT = &H1
    Private Const LVM_GETITEMRECT = (&H1000 + 14)
    Private Const LVM_GETITEMPOSITION = (&H1000 + 16)
    Private Const LVM_GETITEMTEXT = (&H1000 + 45)
    Private Const LVM_GETITEMCOUNT = (&H1000 + 4)
    Private Const ERROR_ALREADY_EXISTS& = 183&
    Private Const ERROR_BAD_ARGUMENTS& = 160&
    Private Const ERROR_INVALID_DATA& = 13&
    Private Const PAGE_READWRITE& = 4&
    Private Const SECTION_MAP_WRITE& = &H2
    Private Const FILE_MAP_WRITE& = SECTION_MAP_WRITE
    '-------------------------------------------------
    Private Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal
    lpClassName$, ByVal lpWindowName$)
    Private Declare Function FindWindowEx& Lib "user32" Alias "FindWindowExA"
    (ByVal hWndParent&, ByVal hWndChildAfter&, ByVal lpClassName$, ByVal
    lpWindowName$)
    Private Declare Function MapViewOfFile& Lib "kernel32" (ByVal
    hFileMappingObject&, ByVal dwDesiredAccess&, ByVal dwFileOffsetHigh&, ByVal
    dwFileOffsetLow&, ByVal dwNumberOfBytesToMap&)
    Private Declare Function MapViewOfFileEx& Lib "kernel32" (ByVal
    hFileMappingObject&, ByVal dwDesiredAccess&, ByVal dwFileOffsetHigh&, ByVal
    dwFileOffsetLow&, ByVal dwNumberOfBytesToMap&, ByVal lpBaseAddress&)
    Private Declare Function UnmapViewOfFile& Lib "kernel32" (ByVal
    lpBaseAddress&)
    Private Declare Function CreateFileMapping& Lib "kernel32" Alias
    "CreateFileMappingA" (ByVal hFile&, ByVal lpFileMappingAttributes&, ByVal
    flProtect&, ByVal dwMaximumSizeHigh&, ByVal dwMaximumSizeLow&, ByVal
    lpName$)
    Private Declare Function SendMessageByLong& Lib "user32" Alias
    "SendMessageA" (ByVal hwnd&, ByVal wMsg&, ByVal wParam&, ByVal lParam&)
    'Private Declare Function CreateFileMapping& Lib "kernel32" Alias
    "CreateFileMappingA" (ByVal hFile&, lpFileMappingAttributes As
    SECURITY_ATTRIBUTES, ByVal flProtect&, ByVal dwMaximumSizeHigh&, ByVal
    dwMaximumSizeLow&, ByVal lpName$)
    Private Declare Function CloseHandle& Lib "kernel32" (ByVal hObject&)
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest
    As Any, hpvSource As Any, ByVal cbCopy&)
    Private Declare Sub CopyMemoryStr Lib "kernel32" Alias "RtlMoveMemory"
    (hpvDest As Any, ByVal hpvSource$, ByVal cbCopy&)
    Private Declare Sub CopyMemoryToStr Lib "kernel32" Alias "RtlMoveMemory"
    (ByVal hpvDest$, hpvSource As Any, ByVal cbCopy&)
    '***************************************************************************
    ***************
    Private h1&, p1&, h2&, p2&
    Private hLV&, dl&, lcount&
    '-------------------------------------------------
    Private LVI As LV_ITEM, pnt As POINTAPI
    '***************************************************************************
    ***************
    '# Filemapping-Objekte erzeugen
    '# Create Filemapping Objectes
    Sub CreateMap()

    '# Mappingobjekt für Name erstellen
    '# Create Mapping object for name
    h1 = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, Len(LVI), "Name")
    If h1 = 0 Then
    MsgBox Err.LastDllError
    Exit Sub
    End If
    ' Get pointer to mapping
    p1 = MapViewOfFileEx(h1, FILE_MAP_WRITE, 0, 0, 0, 0)
    If p1 = 0 Then CloseHandle h1 ' Undo what we did

    '# Mappingobjekt für Position erstellen
    '# Create Mapping object for position
    h2 = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, Len(pnt), "Posi")
    If h2 = 0 Then
    MsgBox Err.LastDllError
    Exit Sub
    End If
    ' Get pointer to mapping
    p2 = MapViewOfFileEx(h2, FILE_MAP_WRITE, 0, 0, 0, 0)
    If p2 = 0 Then CloseHandle h2 ' Undo what we did
    End Sub

    '***************************************************************************
    ***************
    Private Sub Command1_Click()
    Dim Text$, Item&

    If p1 = 0 Then Beep: Exit Sub
    If Val(Text1) > lcount - 1 Then Beep: Exit Sub

    '# 1. Name ermitteln
    '# 1. Get the name
    '---------------------------------------------------------------------------
    ---------------
    Text = String(50, 0): Item = CLng(Text1.Text)

    '# Struktur LV_ITEM initialisieren
    '# Initialize structure LV_ITEM
    With LVI
    .mask = LVIF_TEXT '# Text ist gefragt ' Text required
    .cchTextMax = 50 '# Buffergröße (Stringlänge) 'Buffer length
    .pszText = p1 + 24 '# Die Bytes für den String beginnen hier! 'String
    starts here
    End With

    '# Struktur in Mappingobjekt kopieren
    '# Copy structur into mapping object
    Call CopyMemory(ByVal p1, LVI, Len(LVI))

    '# Listview (Desktop) abfragen (statt Struktur das Mapping-Objekt übergeben)
    '# Query listview (desktop); pass mapping object instead of structure
    dl = SendMessageByLong(hLV, LVM_GETITEMTEXT, Item, p1)
    'Debug.Print dl
    If dl > 0 Then
    '# Mappingobjekt in Struktur kopieren
    '# Copy mapping object into structure
    Call CopyMemory(LVI, ByVal p1, Len(LVI))
    '# String herauskopieren
    '# Extract string
    Call CopyMemoryToStr(Text, ByVal LVI.pszText, 50)
    '# Desktop-Itemname anzeigen
    '# Display item name
    Label1 = Left$(Text, dl)
    End If
    '---------------------------------------------------------------------------
    ---------------
    '# 2. Position ermitteln
    '# 2. Get the position
    '# Listview (Desktop) abfragen (statt Struktur das Mapping-Objekt übergeben)
    '# Query listview (desktop); pass mapping object instead of structure
    dl = SendMessageByLong(hLV, LVM_GETITEMPOSITION, Item, p2)
    Debug.Print dl
    If dl > 0 Then
    '# Mappingobjekt in Struktur kopieren
    '# Copy mapping object into structure
    Call CopyMemory(pnt, ByVal p2, Len(pnt))
    '# Position anzeigen
    '# Display item position
    Label3(0) = "X = " & Trim$(pnt.X)
    Label3(1) = "Y = " & Trim$(pnt.Y)
    End If
    '---------------------------------------------------------------------------
    ---------------
    With Text1
    .SelStart = 0
    .SelLength = Len(.Text)
    .SetFocus
    End With
    End Sub

    '***************************************************************************
    ***************
    Private Sub Form_Load()

    '# Handle des Desktop (ListView) ermitteln
    '# Get the handle of desktop (listview)
    hLV = FindWindow("progman", vbNullString)
    hLV = FindWindowEx(hLV, 0, "shelldll_defview", vbNullString)
    hLV = FindWindowEx(hLV, 0, "syslistview32", vbNullString)

    '# Gesamtzahl der Desktop-Items ermitteln und anzeigen
    '# Get the total number of desktop items and display it
    lcount = SendMessageByLong(hLV, LVM_GETITEMCOUNT, 0, 0)
    Label2 = Label2 & Trim$(lcount - 1)

    '# Zwei Filemapping-Objekte erzeugen
    '# Create two filemapping objects
    Call CreateMap
    End Sub

    '***************************************************************************
    ***************
    Private Sub Form_Unload(Cancel As Integer)
    '# Objekte freigeben
    '# Release Objects
    UnmapViewOfFile p1
    UnmapViewOfFile p2
    CloseHandle h1
    CloseHandle h2
    End Sub

    '***************************************************************************
    ***************




  2. #2
    Mattias Sjögren Guest

    Re: Why does it work? (filemapping)

    Hi Juergen,

    >for testing purposes I've written the little program below (VB5) which
    >gets the name and position of desktop items in Win95/98. Normally, it should
    >not work, because I give the view of a file mapping object (an address in my
    >process) to the explorer instead of the LV_ITEM structure. Well, in
    >Win95/98, the address of a view of a file mapping object will be the same in
    >all processes which create or open a view to this object. But the explorer
    >doesn't know about opening a view ...
    >Anyway, it works. But why???



    Explorer doesn't have to know anything about it. You just pass it a
    pointer, one that happens to be valid in all processes, and it writes
    data to that memory location.

    Perhaps you're thinking of what would happen if you passed the
    *handle* to the file mapping object to Explorer. It would then have to
    know that what it got was a file mapping handle, and then open it. But
    a pointer doesn't have to be opened in any way.


    Mattias

    ____________________________________________
    Mattias Sjögren - mattiass @ hem.passagen.se
    http://hem.spray.se/mattias.sjogren/

  3. #3
    Mattias Sjögren Guest

    Re: Why does it work? (filemapping)

    Hi Juergen,

    >for testing purposes I've written the little program below (VB5) which
    >gets the name and position of desktop items in Win95/98. Normally, it should
    >not work, because I give the view of a file mapping object (an address in my
    >process) to the explorer instead of the LV_ITEM structure. Well, in
    >Win95/98, the address of a view of a file mapping object will be the same in
    >all processes which create or open a view to this object. But the explorer
    >doesn't know about opening a view ...
    >Anyway, it works. But why???



    Explorer doesn't have to know anything about it. You just pass it a
    pointer, one that happens to be valid in all processes, and it writes
    data to that memory location.

    Perhaps you're thinking of what would happen if you passed the
    *handle* to the file mapping object to Explorer. It would then have to
    know that what it got was a file mapping handle, and then open it. But
    a pointer doesn't have to be opened in any way.


    Mattias

    ____________________________________________
    Mattias Sjögren - mattiass @ hem.passagen.se
    http://hem.spray.se/mattias.sjogren/

  4. #4
    Juergen Thuemmler Guest

    Re: Why does it work? (filemapping)

    Hi Mattias & Eduardo,

    thanks for the quick reply - now I can use this method, knowing it works by
    design and not because of any failure ...

    Juergen.



  5. #5
    Juergen Thuemmler Guest

    Re: Why does it work? (filemapping)

    Hi Mattias & Eduardo,

    thanks for the quick reply - now I can use this method, knowing it works by
    design and not because of any failure ...

    Juergen.



  6. #6
    Eduardo A. Morcillo Guest

    Re: Why does it work? (filemapping)

    From the MSDN:

    Platform SDK: Memory

    Virtual Address Space

    The virtual addresses used by a process do not represent the actual physical
    location of an object in memory. Instead, the system maintains a page map
    for each process, which is an internal data structure used to translate
    virtual addresses into corresponding physical addresses.

    The virtual address space is divided into partitions as follows.

    Windows NT Server Enterprise Edition/Windows 2000 Advanced Server: The 3 GB
    partition in low memory (0x00000000 through 0xBFFFFFFF) is available to the
    process, and the 1 GB partition in high memory (0xC0000000 through
    0xFFFFFFFF) is reserved for the system.

    Windows NT/2000: The 2 GB partition in low memory (0x00000000 through
    0x7FFFFFFF) is available to the process, and the 2 GB partition in high
    memory (0x80000000 through 0xFFFFFFFF) is reserved for the system.

    Windows 95/98: The following are the partitions on Windows 95/98.

    Range Usage
    0K - ~64K (0xFFFF) Not writable. This boundary is approximate due to the way
    the Windows 95/98 loads some features of Microsoft® MS-DOS®. This memory is
    private to the process.
    ~64K (0x10000) -
    4 MB (0x3FFFFF) Reserved for MS-DOS compatibility. This memory is fully
    readable and writable by the process. However, this range of memory may have
    some MS-DOS–related structures or code in it, so processes should not
    arbitrarily read from or write to it. This memory is private to the process.
    4MB (0x400000) -
    2GB (0x7FFFFFFF) Available for code and user data. User data is readable
    and writable by the process. Code is execute-only. This memory is private to
    the process.
    2GB (0x80000000) -
    3GB (0xBFFFFFFF) Shared area, readable and writable by all processes. A
    number of system DLLs and other data are loaded into this space.
    3GB (0xC0000000) -
    4GB (0xFFFFFFFF) System memory, readable or writable by any process.
    However, this is where low-level system code resides, so writing to this
    region may corrupt the system, with potentially catastrophic consequences.


    For more information on virtual memory, see the following topics:

    Virtual Address Space and Physical Storage
    Page State
    Scope of Allocated Memory
    Built on Wednesday, November 10, 1999

    ---- END MSDN ----

    Since views are in shared memory (0x80000000 - 0xBFFFFFFF) the data on them
    can be read from any process.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo
    "Juergen Thuemmler" <thue@gmx.de> wrote in message
    news:39450deb@news.devx.com...
    > Hi *.*,
    >
    > for testing purposes I've written the little program below (VB5) which
    > gets the name and position of desktop items in Win95/98. Normally, it

    should
    > not work, because I give the view of a file mapping object (an address in

    my
    > process) to the explorer instead of the LV_ITEM structure. Well, in
    > Win95/98, the address of a view of a file mapping object will be the same

    in
    > all processes which create or open a view to this object. But the explorer
    > doesn't know about opening a view ...
    > Anyway, it works. But why???
    >
    > Juergen.
    >
    >

    '***************************************************************************
    > ***************
    > This is the content of "Form1.frm" file - the only one in the project ...
    >

    '***************************************************************************
    > ***************
    > VERSION 5.00
    > Begin VB.Form Form1
    > BorderStyle = 1 'Fest Einfach
    > Caption = "Filemapping Sample"
    > ClientHeight = 2715
    > ClientLeft = 2850
    > ClientTop = 2640
    > ClientWidth = 2325
    > LinkTopic = "Form1"
    > LockControls = -1 'True
    > MaxButton = 0 'False
    > MinButton = 0 'False
    > ScaleHeight = 2715
    > ScaleWidth = 2325
    > Begin VB.TextBox Text1
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 330
    > Left = 1575
    > TabIndex = 2
    > Text = "0"
    > Top = 450
    > Width = 420
    > End
    > Begin VB.CommandButton Command1
    > Caption = "Get Desktop Item Name and Position "
    > Default = -1 'True
    > Height = 645
    > Left = 315
    > TabIndex = 0
    > Top = 1890
    > Width = 1680
    > End
    > Begin VB.Label Label4
    > Caption = "Item Number:"
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 240
    > Left = 315
    > TabIndex = 6
    > Top = 495
    > Width = 1140
    > End
    > Begin VB.Label Label3
    > Caption = "Y = "
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 240
    > Index = 1
    > Left = 1170
    > TabIndex = 5
    > Top = 1485
    > Width = 780
    > End
    > Begin VB.Label Label3
    > Caption = "X = "
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 240
    > Index = 0
    > Left = 315
    > TabIndex = 4
    > Top = 1485
    > Width = 780
    > End
    > Begin VB.Label Label2
    > Caption = "Max. Item-No.: "
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 195
    > Left = 315
    > TabIndex = 3
    > Top = 135
    > Width = 1725
    > End
    > Begin VB.Label Label1
    > Alignment = 2 'Zentriert
    > BackColor = &H00FFFFFF&
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 420
    > Left = 315
    > TabIndex = 1
    > Top = 900
    > Width = 1650
    > WordWrap = -1 'True
    > End
    > End
    > Attribute VB_Name = "Form1"
    > Attribute VB_GlobalNameSpace = False
    > Attribute VB_Creatable = False
    > Attribute VB_PredeclaredId = True
    > Attribute VB_Exposed = False
    >

    '***************************************************************************
    > ***************
    > Option Explicit
    >

    '***************************************************************************
    > ***************
    > Private Type POINTAPI
    > X As Long
    > Y As Long
    > End Type
    > '-------------------------------------------------
    > Private Type LV_ITEM
    > mask As Long
    > iItem As Long
    > iSubItem As Long
    > State As Long
    > stateMask As Long
    > pszText As Long
    > cchTextMax As Long
    > iImage As Long
    > lParam As Long
    > End Type 'LV_ITEMA
    > '-------------------------------------------------
    > 'Private Type SECURITY_ATTRIBUTES
    > ' nLength As Long
    > ' lpSecurityDescriptor As Long
    > ' bInheritHandle As Long
    > 'End Type
    > '-------------------------------------------------
    > Private Const LVIF_TEXT = &H1
    > Private Const LVM_GETITEMRECT = (&H1000 + 14)
    > Private Const LVM_GETITEMPOSITION = (&H1000 + 16)
    > Private Const LVM_GETITEMTEXT = (&H1000 + 45)
    > Private Const LVM_GETITEMCOUNT = (&H1000 + 4)
    > Private Const ERROR_ALREADY_EXISTS& = 183&
    > Private Const ERROR_BAD_ARGUMENTS& = 160&
    > Private Const ERROR_INVALID_DATA& = 13&
    > Private Const PAGE_READWRITE& = 4&
    > Private Const SECTION_MAP_WRITE& = &H2
    > Private Const FILE_MAP_WRITE& = SECTION_MAP_WRITE
    > '-------------------------------------------------
    > Private Declare Function FindWindow& Lib "user32" Alias "FindWindowA"

    (ByVal
    > lpClassName$, ByVal lpWindowName$)
    > Private Declare Function FindWindowEx& Lib "user32" Alias "FindWindowExA"
    > (ByVal hWndParent&, ByVal hWndChildAfter&, ByVal lpClassName$, ByVal
    > lpWindowName$)
    > Private Declare Function MapViewOfFile& Lib "kernel32" (ByVal
    > hFileMappingObject&, ByVal dwDesiredAccess&, ByVal dwFileOffsetHigh&,

    ByVal
    > dwFileOffsetLow&, ByVal dwNumberOfBytesToMap&)
    > Private Declare Function MapViewOfFileEx& Lib "kernel32" (ByVal
    > hFileMappingObject&, ByVal dwDesiredAccess&, ByVal dwFileOffsetHigh&,

    ByVal
    > dwFileOffsetLow&, ByVal dwNumberOfBytesToMap&, ByVal lpBaseAddress&)
    > Private Declare Function UnmapViewOfFile& Lib "kernel32" (ByVal
    > lpBaseAddress&)
    > Private Declare Function CreateFileMapping& Lib "kernel32" Alias
    > "CreateFileMappingA" (ByVal hFile&, ByVal lpFileMappingAttributes&, ByVal
    > flProtect&, ByVal dwMaximumSizeHigh&, ByVal dwMaximumSizeLow&, ByVal
    > lpName$)
    > Private Declare Function SendMessageByLong& Lib "user32" Alias
    > "SendMessageA" (ByVal hwnd&, ByVal wMsg&, ByVal wParam&, ByVal lParam&)
    > 'Private Declare Function CreateFileMapping& Lib "kernel32" Alias
    > "CreateFileMappingA" (ByVal hFile&, lpFileMappingAttributes As
    > SECURITY_ATTRIBUTES, ByVal flProtect&, ByVal dwMaximumSizeHigh&, ByVal
    > dwMaximumSizeLow&, ByVal lpName$)
    > Private Declare Function CloseHandle& Lib "kernel32" (ByVal hObject&)
    > Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"

    (hpvDest
    > As Any, hpvSource As Any, ByVal cbCopy&)
    > Private Declare Sub CopyMemoryStr Lib "kernel32" Alias "RtlMoveMemory"
    > (hpvDest As Any, ByVal hpvSource$, ByVal cbCopy&)
    > Private Declare Sub CopyMemoryToStr Lib "kernel32" Alias "RtlMoveMemory"
    > (ByVal hpvDest$, hpvSource As Any, ByVal cbCopy&)
    >

    '***************************************************************************
    > ***************
    > Private h1&, p1&, h2&, p2&
    > Private hLV&, dl&, lcount&
    > '-------------------------------------------------
    > Private LVI As LV_ITEM, pnt As POINTAPI
    >

    '***************************************************************************
    > ***************
    > '# Filemapping-Objekte erzeugen
    > '# Create Filemapping Objectes
    > Sub CreateMap()
    >
    > '# Mappingobjekt für Name erstellen
    > '# Create Mapping object for name
    > h1 = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, Len(LVI), "Name")
    > If h1 = 0 Then
    > MsgBox Err.LastDllError
    > Exit Sub
    > End If
    > ' Get pointer to mapping
    > p1 = MapViewOfFileEx(h1, FILE_MAP_WRITE, 0, 0, 0, 0)
    > If p1 = 0 Then CloseHandle h1 ' Undo what we did
    >
    > '# Mappingobjekt für Position erstellen
    > '# Create Mapping object for position
    > h2 = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, Len(pnt), "Posi")
    > If h2 = 0 Then
    > MsgBox Err.LastDllError
    > Exit Sub
    > End If
    > ' Get pointer to mapping
    > p2 = MapViewOfFileEx(h2, FILE_MAP_WRITE, 0, 0, 0, 0)
    > If p2 = 0 Then CloseHandle h2 ' Undo what we did
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    > Private Sub Command1_Click()
    > Dim Text$, Item&
    >
    > If p1 = 0 Then Beep: Exit Sub
    > If Val(Text1) > lcount - 1 Then Beep: Exit Sub
    >
    > '# 1. Name ermitteln
    > '# 1. Get the name
    >

    '---------------------------------------------------------------------------
    > ---------------
    > Text = String(50, 0): Item = CLng(Text1.Text)
    >
    > '# Struktur LV_ITEM initialisieren
    > '# Initialize structure LV_ITEM
    > With LVI
    > .mask = LVIF_TEXT '# Text ist gefragt ' Text required
    > .cchTextMax = 50 '# Buffergröße (Stringlänge) 'Buffer length
    > .pszText = p1 + 24 '# Die Bytes für den String beginnen hier! 'String
    > starts here
    > End With
    >
    > '# Struktur in Mappingobjekt kopieren
    > '# Copy structur into mapping object
    > Call CopyMemory(ByVal p1, LVI, Len(LVI))
    >
    > '# Listview (Desktop) abfragen (statt Struktur das Mapping-Objekt

    übergeben)
    > '# Query listview (desktop); pass mapping object instead of structure
    > dl = SendMessageByLong(hLV, LVM_GETITEMTEXT, Item, p1)
    > 'Debug.Print dl
    > If dl > 0 Then
    > '# Mappingobjekt in Struktur kopieren
    > '# Copy mapping object into structure
    > Call CopyMemory(LVI, ByVal p1, Len(LVI))
    > '# String herauskopieren
    > '# Extract string
    > Call CopyMemoryToStr(Text, ByVal LVI.pszText, 50)
    > '# Desktop-Itemname anzeigen
    > '# Display item name
    > Label1 = Left$(Text, dl)
    > End If
    >

    '---------------------------------------------------------------------------
    > ---------------
    > '# 2. Position ermitteln
    > '# 2. Get the position
    > '# Listview (Desktop) abfragen (statt Struktur das Mapping-Objekt

    übergeben)
    > '# Query listview (desktop); pass mapping object instead of structure
    > dl = SendMessageByLong(hLV, LVM_GETITEMPOSITION, Item, p2)
    > Debug.Print dl
    > If dl > 0 Then
    > '# Mappingobjekt in Struktur kopieren
    > '# Copy mapping object into structure
    > Call CopyMemory(pnt, ByVal p2, Len(pnt))
    > '# Position anzeigen
    > '# Display item position
    > Label3(0) = "X = " & Trim$(pnt.X)
    > Label3(1) = "Y = " & Trim$(pnt.Y)
    > End If
    >

    '---------------------------------------------------------------------------
    > ---------------
    > With Text1
    > .SelStart = 0
    > .SelLength = Len(.Text)
    > .SetFocus
    > End With
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    > Private Sub Form_Load()
    >
    > '# Handle des Desktop (ListView) ermitteln
    > '# Get the handle of desktop (listview)
    > hLV = FindWindow("progman", vbNullString)
    > hLV = FindWindowEx(hLV, 0, "shelldll_defview", vbNullString)
    > hLV = FindWindowEx(hLV, 0, "syslistview32", vbNullString)
    >
    > '# Gesamtzahl der Desktop-Items ermitteln und anzeigen
    > '# Get the total number of desktop items and display it
    > lcount = SendMessageByLong(hLV, LVM_GETITEMCOUNT, 0, 0)
    > Label2 = Label2 & Trim$(lcount - 1)
    >
    > '# Zwei Filemapping-Objekte erzeugen
    > '# Create two filemapping objects
    > Call CreateMap
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    > Private Sub Form_Unload(Cancel As Integer)
    > '# Objekte freigeben
    > '# Release Objects
    > UnmapViewOfFile p1
    > UnmapViewOfFile p2
    > CloseHandle h1
    > CloseHandle h2
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    >
    >
    >




  7. #7
    Eduardo A. Morcillo Guest

    Re: Why does it work? (filemapping)

    From the MSDN:

    Platform SDK: Memory

    Virtual Address Space

    The virtual addresses used by a process do not represent the actual physical
    location of an object in memory. Instead, the system maintains a page map
    for each process, which is an internal data structure used to translate
    virtual addresses into corresponding physical addresses.

    The virtual address space is divided into partitions as follows.

    Windows NT Server Enterprise Edition/Windows 2000 Advanced Server: The 3 GB
    partition in low memory (0x00000000 through 0xBFFFFFFF) is available to the
    process, and the 1 GB partition in high memory (0xC0000000 through
    0xFFFFFFFF) is reserved for the system.

    Windows NT/2000: The 2 GB partition in low memory (0x00000000 through
    0x7FFFFFFF) is available to the process, and the 2 GB partition in high
    memory (0x80000000 through 0xFFFFFFFF) is reserved for the system.

    Windows 95/98: The following are the partitions on Windows 95/98.

    Range Usage
    0K - ~64K (0xFFFF) Not writable. This boundary is approximate due to the way
    the Windows 95/98 loads some features of Microsoft® MS-DOS®. This memory is
    private to the process.
    ~64K (0x10000) -
    4 MB (0x3FFFFF) Reserved for MS-DOS compatibility. This memory is fully
    readable and writable by the process. However, this range of memory may have
    some MS-DOS–related structures or code in it, so processes should not
    arbitrarily read from or write to it. This memory is private to the process.
    4MB (0x400000) -
    2GB (0x7FFFFFFF) Available for code and user data. User data is readable
    and writable by the process. Code is execute-only. This memory is private to
    the process.
    2GB (0x80000000) -
    3GB (0xBFFFFFFF) Shared area, readable and writable by all processes. A
    number of system DLLs and other data are loaded into this space.
    3GB (0xC0000000) -
    4GB (0xFFFFFFFF) System memory, readable or writable by any process.
    However, this is where low-level system code resides, so writing to this
    region may corrupt the system, with potentially catastrophic consequences.


    For more information on virtual memory, see the following topics:

    Virtual Address Space and Physical Storage
    Page State
    Scope of Allocated Memory
    Built on Wednesday, November 10, 1999

    ---- END MSDN ----

    Since views are in shared memory (0x80000000 - 0xBFFFFFFF) the data on them
    can be read from any process.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo
    "Juergen Thuemmler" <thue@gmx.de> wrote in message
    news:39450deb@news.devx.com...
    > Hi *.*,
    >
    > for testing purposes I've written the little program below (VB5) which
    > gets the name and position of desktop items in Win95/98. Normally, it

    should
    > not work, because I give the view of a file mapping object (an address in

    my
    > process) to the explorer instead of the LV_ITEM structure. Well, in
    > Win95/98, the address of a view of a file mapping object will be the same

    in
    > all processes which create or open a view to this object. But the explorer
    > doesn't know about opening a view ...
    > Anyway, it works. But why???
    >
    > Juergen.
    >
    >

    '***************************************************************************
    > ***************
    > This is the content of "Form1.frm" file - the only one in the project ...
    >

    '***************************************************************************
    > ***************
    > VERSION 5.00
    > Begin VB.Form Form1
    > BorderStyle = 1 'Fest Einfach
    > Caption = "Filemapping Sample"
    > ClientHeight = 2715
    > ClientLeft = 2850
    > ClientTop = 2640
    > ClientWidth = 2325
    > LinkTopic = "Form1"
    > LockControls = -1 'True
    > MaxButton = 0 'False
    > MinButton = 0 'False
    > ScaleHeight = 2715
    > ScaleWidth = 2325
    > Begin VB.TextBox Text1
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 330
    > Left = 1575
    > TabIndex = 2
    > Text = "0"
    > Top = 450
    > Width = 420
    > End
    > Begin VB.CommandButton Command1
    > Caption = "Get Desktop Item Name and Position "
    > Default = -1 'True
    > Height = 645
    > Left = 315
    > TabIndex = 0
    > Top = 1890
    > Width = 1680
    > End
    > Begin VB.Label Label4
    > Caption = "Item Number:"
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 240
    > Left = 315
    > TabIndex = 6
    > Top = 495
    > Width = 1140
    > End
    > Begin VB.Label Label3
    > Caption = "Y = "
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 240
    > Index = 1
    > Left = 1170
    > TabIndex = 5
    > Top = 1485
    > Width = 780
    > End
    > Begin VB.Label Label3
    > Caption = "X = "
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 240
    > Index = 0
    > Left = 315
    > TabIndex = 4
    > Top = 1485
    > Width = 780
    > End
    > Begin VB.Label Label2
    > Caption = "Max. Item-No.: "
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 195
    > Left = 315
    > TabIndex = 3
    > Top = 135
    > Width = 1725
    > End
    > Begin VB.Label Label1
    > Alignment = 2 'Zentriert
    > BackColor = &H00FFFFFF&
    > BeginProperty Font
    > Name = "MS Sans Serif"
    > Size = 8.25
    > Charset = 0
    > Weight = 700
    > Underline = 0 'False
    > Italic = 0 'False
    > Strikethrough = 0 'False
    > EndProperty
    > Height = 420
    > Left = 315
    > TabIndex = 1
    > Top = 900
    > Width = 1650
    > WordWrap = -1 'True
    > End
    > End
    > Attribute VB_Name = "Form1"
    > Attribute VB_GlobalNameSpace = False
    > Attribute VB_Creatable = False
    > Attribute VB_PredeclaredId = True
    > Attribute VB_Exposed = False
    >

    '***************************************************************************
    > ***************
    > Option Explicit
    >

    '***************************************************************************
    > ***************
    > Private Type POINTAPI
    > X As Long
    > Y As Long
    > End Type
    > '-------------------------------------------------
    > Private Type LV_ITEM
    > mask As Long
    > iItem As Long
    > iSubItem As Long
    > State As Long
    > stateMask As Long
    > pszText As Long
    > cchTextMax As Long
    > iImage As Long
    > lParam As Long
    > End Type 'LV_ITEMA
    > '-------------------------------------------------
    > 'Private Type SECURITY_ATTRIBUTES
    > ' nLength As Long
    > ' lpSecurityDescriptor As Long
    > ' bInheritHandle As Long
    > 'End Type
    > '-------------------------------------------------
    > Private Const LVIF_TEXT = &H1
    > Private Const LVM_GETITEMRECT = (&H1000 + 14)
    > Private Const LVM_GETITEMPOSITION = (&H1000 + 16)
    > Private Const LVM_GETITEMTEXT = (&H1000 + 45)
    > Private Const LVM_GETITEMCOUNT = (&H1000 + 4)
    > Private Const ERROR_ALREADY_EXISTS& = 183&
    > Private Const ERROR_BAD_ARGUMENTS& = 160&
    > Private Const ERROR_INVALID_DATA& = 13&
    > Private Const PAGE_READWRITE& = 4&
    > Private Const SECTION_MAP_WRITE& = &H2
    > Private Const FILE_MAP_WRITE& = SECTION_MAP_WRITE
    > '-------------------------------------------------
    > Private Declare Function FindWindow& Lib "user32" Alias "FindWindowA"

    (ByVal
    > lpClassName$, ByVal lpWindowName$)
    > Private Declare Function FindWindowEx& Lib "user32" Alias "FindWindowExA"
    > (ByVal hWndParent&, ByVal hWndChildAfter&, ByVal lpClassName$, ByVal
    > lpWindowName$)
    > Private Declare Function MapViewOfFile& Lib "kernel32" (ByVal
    > hFileMappingObject&, ByVal dwDesiredAccess&, ByVal dwFileOffsetHigh&,

    ByVal
    > dwFileOffsetLow&, ByVal dwNumberOfBytesToMap&)
    > Private Declare Function MapViewOfFileEx& Lib "kernel32" (ByVal
    > hFileMappingObject&, ByVal dwDesiredAccess&, ByVal dwFileOffsetHigh&,

    ByVal
    > dwFileOffsetLow&, ByVal dwNumberOfBytesToMap&, ByVal lpBaseAddress&)
    > Private Declare Function UnmapViewOfFile& Lib "kernel32" (ByVal
    > lpBaseAddress&)
    > Private Declare Function CreateFileMapping& Lib "kernel32" Alias
    > "CreateFileMappingA" (ByVal hFile&, ByVal lpFileMappingAttributes&, ByVal
    > flProtect&, ByVal dwMaximumSizeHigh&, ByVal dwMaximumSizeLow&, ByVal
    > lpName$)
    > Private Declare Function SendMessageByLong& Lib "user32" Alias
    > "SendMessageA" (ByVal hwnd&, ByVal wMsg&, ByVal wParam&, ByVal lParam&)
    > 'Private Declare Function CreateFileMapping& Lib "kernel32" Alias
    > "CreateFileMappingA" (ByVal hFile&, lpFileMappingAttributes As
    > SECURITY_ATTRIBUTES, ByVal flProtect&, ByVal dwMaximumSizeHigh&, ByVal
    > dwMaximumSizeLow&, ByVal lpName$)
    > Private Declare Function CloseHandle& Lib "kernel32" (ByVal hObject&)
    > Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"

    (hpvDest
    > As Any, hpvSource As Any, ByVal cbCopy&)
    > Private Declare Sub CopyMemoryStr Lib "kernel32" Alias "RtlMoveMemory"
    > (hpvDest As Any, ByVal hpvSource$, ByVal cbCopy&)
    > Private Declare Sub CopyMemoryToStr Lib "kernel32" Alias "RtlMoveMemory"
    > (ByVal hpvDest$, hpvSource As Any, ByVal cbCopy&)
    >

    '***************************************************************************
    > ***************
    > Private h1&, p1&, h2&, p2&
    > Private hLV&, dl&, lcount&
    > '-------------------------------------------------
    > Private LVI As LV_ITEM, pnt As POINTAPI
    >

    '***************************************************************************
    > ***************
    > '# Filemapping-Objekte erzeugen
    > '# Create Filemapping Objectes
    > Sub CreateMap()
    >
    > '# Mappingobjekt für Name erstellen
    > '# Create Mapping object for name
    > h1 = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, Len(LVI), "Name")
    > If h1 = 0 Then
    > MsgBox Err.LastDllError
    > Exit Sub
    > End If
    > ' Get pointer to mapping
    > p1 = MapViewOfFileEx(h1, FILE_MAP_WRITE, 0, 0, 0, 0)
    > If p1 = 0 Then CloseHandle h1 ' Undo what we did
    >
    > '# Mappingobjekt für Position erstellen
    > '# Create Mapping object for position
    > h2 = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, Len(pnt), "Posi")
    > If h2 = 0 Then
    > MsgBox Err.LastDllError
    > Exit Sub
    > End If
    > ' Get pointer to mapping
    > p2 = MapViewOfFileEx(h2, FILE_MAP_WRITE, 0, 0, 0, 0)
    > If p2 = 0 Then CloseHandle h2 ' Undo what we did
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    > Private Sub Command1_Click()
    > Dim Text$, Item&
    >
    > If p1 = 0 Then Beep: Exit Sub
    > If Val(Text1) > lcount - 1 Then Beep: Exit Sub
    >
    > '# 1. Name ermitteln
    > '# 1. Get the name
    >

    '---------------------------------------------------------------------------
    > ---------------
    > Text = String(50, 0): Item = CLng(Text1.Text)
    >
    > '# Struktur LV_ITEM initialisieren
    > '# Initialize structure LV_ITEM
    > With LVI
    > .mask = LVIF_TEXT '# Text ist gefragt ' Text required
    > .cchTextMax = 50 '# Buffergröße (Stringlänge) 'Buffer length
    > .pszText = p1 + 24 '# Die Bytes für den String beginnen hier! 'String
    > starts here
    > End With
    >
    > '# Struktur in Mappingobjekt kopieren
    > '# Copy structur into mapping object
    > Call CopyMemory(ByVal p1, LVI, Len(LVI))
    >
    > '# Listview (Desktop) abfragen (statt Struktur das Mapping-Objekt

    übergeben)
    > '# Query listview (desktop); pass mapping object instead of structure
    > dl = SendMessageByLong(hLV, LVM_GETITEMTEXT, Item, p1)
    > 'Debug.Print dl
    > If dl > 0 Then
    > '# Mappingobjekt in Struktur kopieren
    > '# Copy mapping object into structure
    > Call CopyMemory(LVI, ByVal p1, Len(LVI))
    > '# String herauskopieren
    > '# Extract string
    > Call CopyMemoryToStr(Text, ByVal LVI.pszText, 50)
    > '# Desktop-Itemname anzeigen
    > '# Display item name
    > Label1 = Left$(Text, dl)
    > End If
    >

    '---------------------------------------------------------------------------
    > ---------------
    > '# 2. Position ermitteln
    > '# 2. Get the position
    > '# Listview (Desktop) abfragen (statt Struktur das Mapping-Objekt

    übergeben)
    > '# Query listview (desktop); pass mapping object instead of structure
    > dl = SendMessageByLong(hLV, LVM_GETITEMPOSITION, Item, p2)
    > Debug.Print dl
    > If dl > 0 Then
    > '# Mappingobjekt in Struktur kopieren
    > '# Copy mapping object into structure
    > Call CopyMemory(pnt, ByVal p2, Len(pnt))
    > '# Position anzeigen
    > '# Display item position
    > Label3(0) = "X = " & Trim$(pnt.X)
    > Label3(1) = "Y = " & Trim$(pnt.Y)
    > End If
    >

    '---------------------------------------------------------------------------
    > ---------------
    > With Text1
    > .SelStart = 0
    > .SelLength = Len(.Text)
    > .SetFocus
    > End With
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    > Private Sub Form_Load()
    >
    > '# Handle des Desktop (ListView) ermitteln
    > '# Get the handle of desktop (listview)
    > hLV = FindWindow("progman", vbNullString)
    > hLV = FindWindowEx(hLV, 0, "shelldll_defview", vbNullString)
    > hLV = FindWindowEx(hLV, 0, "syslistview32", vbNullString)
    >
    > '# Gesamtzahl der Desktop-Items ermitteln und anzeigen
    > '# Get the total number of desktop items and display it
    > lcount = SendMessageByLong(hLV, LVM_GETITEMCOUNT, 0, 0)
    > Label2 = Label2 & Trim$(lcount - 1)
    >
    > '# Zwei Filemapping-Objekte erzeugen
    > '# Create two filemapping objects
    > Call CreateMap
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    > Private Sub Form_Unload(Cancel As Integer)
    > '# Objekte freigeben
    > '# Release Objects
    > UnmapViewOfFile p1
    > UnmapViewOfFile p2
    > CloseHandle h1
    > CloseHandle h2
    > End Sub
    >
    >

    '***************************************************************************
    > ***************
    >
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center