Announcement

Collapse
No announcement yet.

hWrite problem

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

  • hWrite problem

    Dear Group,

    I am trying to convert a hWrite sample from C.

    The code is as follows
    Code:
    void FAR*  lpdata;
    
    DWORD  dwWritten;
    
    OFSTRUCT MyOF;
    
    HFILE   hFile;
    
    HGLOBAL hGlobal = 0;
    
    DWORD   dwSize = 0;
    
     
    
    /* to store the data */
    
    if (!PEstore (hWndPE, &hGlobal, &dwSize))
    
    { /* error handler */ }
    
     
    
    /* hGlobal now contains a valid handle 
    
    and dwSize specifies the size stored. 
    
    Use GlobalLock to get a pointer to data
    
    and then call GlobalUnlock when finished
    
    with the pointer. */
    
     
    
    lpdata = GlobalLock(hGlobal);
    
    /* open file */
    
    hFile = OpenFile("Test.dat", &MyOF, OF_CREATE|OF_READWRITE); 
    
    /* save size so we know how much to allocate later when loading */
    
    dwWritten = hwrite(hFile, dwSize, 4); 
    
    /* save actual data */
    
    dwWritten = hwrite(hFile, lpdata, dwSize); 
    
    lclose(hFile);
    
    GlobalUnlock(hGlobal);
    
    GlobalFree(hGlobal);
    Any assistance would be appreciated.

    The problem I am having is the first instance of hWrite, which is expecting an ASCIIZ PTR, but I have DWORD data to write.

    Regards

    Andrew

    ------------------
    --------------
    andrew dot lindsay at westnet dot com dot au
    --------------
    andrew dot lindsay at westnet dot com dot au

  • #2
    I can't even find 'hwrite' in my SDK doc, although I did find it in my Win32API.Inc. (Meaning? Perhaps it's a leftover from Windows 3?)

    I you really have your heart set on using WinAPI file access, use WriteFile.

    But if you really are converting from C to PB, just open the file for BINARY and use PUT.

    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Code:
      #INCLUDE "win32api.inc"
      
      FUNCTION PEstore(BYVAL hWndPE AS DWORD, BYVAL hGlobal AS DWORD PTR, BYVAL dwSize AS DWORD PTR) AS LONG
      END FUNCTION
      
      FUNCTION Foo(BYVAL hWndPE AS DWORD) AS LONG
          LOCAL lpdata    AS DWORD    ';void FAR*  lpdata;
          LOCAL dwWritten AS DWORD    'dwWritten;
          LOCAL MyOF      AS OFSTRUCT ';
          LOCAL hFile     AS DWORD    ';HFILE   hFile;
          LOCAL hGlobal   AS DWORD    ' = 0;  HGLOBAL hGlobal = 0;
          LOCAL dwSize    AS DWORD    ';DWORD   dwSize = 0;
      
          '/* to store the data */
          IF PEstore(BYVAL hWndPE, BYVAL VARPTR(hGlobal), BYVAL VARPTR(dwSize) )<> 0 THEN
             '{ /* error handler */ }
             '/* hGlobal now contains a valid handle and dwSize specifies the size stored.
             'Use GlobalLock to get a pointer to data and then call GlobalUnlock when finished
             'with the pointer. */
      
             lpdata = GlobalLock(hGlobal)';
             '/* open file */
             hFile = OpenFile("Test.dat", BYVAL VARPTR(MyOF), %OF_CREATE OR %OF_READWRITE)
      
             '/* save size so we know how much to allocate later when loading */
             dwWritten = hwrite(BYVAL hFile, BYVAL dwSize, BYVAL 4)
      
             '/* save actual data */
             dwWritten = hwrite(BYVAL hFile, BYVAL lpdata, BYVAL dwSize)';
      
             lclose(BYVAL hFile)';
             GlobalUnlock(BYVAL hGlobal)';
             GlobalFree(BYVAL hGlobal)';
          END IF
      END FUNCTION
      
      FUNCTION PBMAIN()
      END FUNCTION
      P.S.

      >>The problem I am having is the first instance of hWrite, which is expecting an ASCIIZ PTR, but I have DWORD data to write.
      You can use UNION for convertion
      UNION convert
      lpSz AS ASCIIZ PTR
      dw AS DWORD
      END UNION

      Or in your case hWrite second param=lpBuffer AS ASCIIZ
      which mean its enough if you will send address of memory
      as BYVAL VARPTR (lpBuffer)


      To MCM:
      Code:
      DECLARE FUNCTION hwrite LIB "KERNEL32.DLL" ALIAS "_hwrite" (BYVAL hFile AS DWORD, lpBuffer AS ASCIIZ, BYVAL lBytes AS LONG) AS LONG
      last win32api.inc which located @ PB site.

      ------------------
      -=Alex=-



      [This message has been edited by Aleksandr Dobrev (edited July 23, 2006).]
      -=Alex=-

      Comment


      • #4
        Code:
        '/* open file */ 
         hFile = OpenFile("Test.dat", BYVAL VARPTR(MyOF), %OF_CREATE OR %OF_READWRITE)
        '/* save size so we know how much to allocate later when loading */
         dwWritten = hwrite(BYVAL hFile, BYVAL dwSize, BYVAL 4)
         '/* save actual data */ 
        dwWritten = hwrite(BYVAL hFile, BYVAL lpdata, BYVAL dwSize) 
        '
         lclose(BYVAL hFile)'
        ===>
        Code:
        '/* open file */ 
         hFile   = FREEFILE
         OPEN      "test.dat" for BINARY ACCESS READ WRITE LOCK READ WRITE as hfile
        '/* save size so we know how much to allocate later when loading */
         PUT hFile, , dwsize
         '/* save actual data */ 
          w$ = PEEK$(lpdata, dwSize)
         PUT$ hFile, w$
         CLOSE hFile
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment

        Working...
        X