Announcement

Collapse
No announcement yet.

GlobalMem issues

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • GlobalMem issues

    Strange things in the land of globalmem...

    The underlying code works in application A but not in application B
    Especially the Poke$ doesn't work in another application. The calls are the
    same.

    Code:
    Function malloc(ByVal sData As String) As Dword
    
     Local vStr   As Dword ' String Ptr
     Local hPtr   As Long
     Local hLock  As Long
    
     If IsFalse(Len(sData)) Then
        Function = 0
        Exit Function
     End If
    
     GlobalMem Alloc Len(sData) to hPtr
    
     GlobalMem Lock hPtr To vStr
    
     ' Need to use string pointer to allocate the string data instead of @vstr = sData (Bugfix)
     '@vStr = sData
     ' vStr = StrPtr(sData)
     Poke$ vStr, sData
    
     GlobalMem Unlock hPtr To hLock
    
     Function = hPtr
    
    End Function
    In application B where above mentioned codesnippet doesn't work, this will but then this code won't work in application A.

    Code:
    Function malloc(ByVal sData As String) As Dword
    
     Local vStr   As String ptr
     Local hPtr   As Long
     Local hLock  As Long
    
     If IsFalse(Len(sData)) Then
        Function = 0
        Exit Function
     End If
    
     GlobalMem Alloc Len(sData) to hPtr
    
     GlobalMem Lock hPtr To vStr
    
     'Poke$ vStr, sData
     @vStr = sData
    
    
     '@vStr = sData
     GlobalMem Unlock hPtr To hLock
    
     Function = hPtr
    
    End Function
    I going nuts over this. ...

    I want a general wrapper for global mem function that will work in every application.
    So here we are, this is the end.
    But all that dies, is born again.
    - From The Ashes (In This Moment)

  • #2
    What do you want in your memory block? The STRING PTR? The string data?

    If you want the STRING PTR
    Code:
    LOCAL vStr AS STRING PTR, dwAddr AS DWORD
      GLOBALMEM ALLOC  SIZEOF(vStr) TO hMem
      GLOBALMEM LOCK hMem  to dwAddr
      POKE LONG, dwAddr, VARPTR(sData))
    If you want the string data
    Code:
    LOCAL dwAddr AS DWORD
       GLOBAL MEM ALLOC  LEN(sData)  TO hMem
       GLOBAL MEM LOCK   hMem to  dwADDR 
       POKE$   dwAddr, sData
    ...
    I suppose you might want a pointer to the string data (not the same as a STRING PTR). Exercise left to user.

    BTW, I think I'd avoid using "malloc" as a function name for a function which does more than allocate memory and return an address, as "malloc" is a c-language keyword for same and often appears in examples of WinAPI usage on MSDN.


    Ref: malloc, realloc, free for PB/Win32 September 20, 2002
    Last edited by Michael Mattias; 21 Sep 2009, 09:05 AM.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Hi Michael,

      Yes, I want the string data and it is exactly that what I'm doing.

      Code:
      LOCAL dwAddr AS DWORD
         GLOBAL MEM ALLOC  LEN(sData)  TO hMem
         GLOBAL MEM LOCK   hMem to  dwADDR 
         POKE$   dwAddr, sData
      This code above crash my program... but the following does not.

      Code:
       Local vStr   As String ptr
       Local hPtr   As Long
       Local hLock  As Long
      
       If IsFalse(Len(sData)) Then
          Function = 0
          Exit Function
       End If
      
       GlobalMem Alloc Len(sData) to hPtr
      
       If hPtr Then
      
           GlobalMem Lock hPtr To vStr
      
           @vStr = sData
      
           GlobalMem Unlock hPtr To hLock
      
           Function = hPtr
      
       End If
      
      End Function
      So, even though I'm using String Ptr, I'm assigning the sData to the location at which Global Mem locked the memory.
      So here we are, this is the end.
      But all that dies, is born again.
      - From The Ashes (In This Moment)

      Comment


      • #4
        Code:
        FUNCTION Foo() AS LONG
        
         LOCAL dwAddr AS DWORD, sData AS STRING, hMem AS LONG
           
           sData = "foo"
           
           GLOBALMEM ALLOC  LEN(sData)  TO hMem
           GLOBALMEM LOCK   hMem TO  dwADDR
           POKE$   dwAddr, sData
           
        END FUNCTION
        Runs here without incident (Not that it does anything).

        Methinks your problem is elsewhere in your program.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          To MCM - You were right

          Michael, obviously you were right. A lot of times actually

          The problem was not the malloc function even though trace did stop there...
          it was the function to get the value back (i use memstr).

          The function looks like this It is the corrected version.
          I used a string ptr to get the data back from a Poke$ which is of course wrong. I corrected it with a Asciiz Ptr and this is the way to go.

          Thanks !

          Code:
          Function memstr(ByVal hPtr As Dword) As String
          
           Local vStr   As AsciiZ Ptr 'String Ptr
           Local hLock  As Dword
          
           GlobalMem Lock hPtr To vStr
          
           Function = Rtrim$(@vStr, Chr$(0))
          
           GlobalMem Unlock hPtr To hLock
          
          End Function
          So here we are, this is the end.
          But all that dies, is born again.
          - From The Ashes (In This Moment)

          Comment

          Working...
          X