Jump to content
Ahankhah

Read/Write Binary Files

Recommended Posts

Ahankhah

Dear all:),

Is it possible to read/write a binary file from Visual LISP:?:?

Share this post


Link to post
Share on other sites
Lee Mac

You could use the methods associated with the FileSystemObject as demonstrated by these courtesy of MP:

 

http://www.theswamp.org/index.php?topic=17465.0

 

Or you could use the ADO Stream object:

 

;;-----------------=={ Read Binary Stream }==-----------------;;
;;                                                            ;;
;;  Uses the ADO Stream Object to read a supplied file and    ;;
;;  returns a variant of bytes.                               ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  filename - filename of file to read.                      ;;
;;  len      - number of bytes to read                        ;;
;;  (if non-numerical, less than 1, or greater than the size  ;;
;;   of the file, everything is returned).                    ;;
;;------------------------------------------------------------;;
;;  Returns:                                                  ;;
;;  Variant of Binary data which may be converted to a list   ;;
;;  bytes using the relevant VL Variant functions or used     ;;
;;  with LM:WriteBinaryStream.                                ;;
;;------------------------------------------------------------;;

(defun LM:ReadBinaryStream ( filename len / ADOStream result ) (vl-load-com)
 
 (setq result
   (vl-catch-all-apply
     (function
       (lambda ( / size )
         (setq ADOStream (vlax-create-object "ADODB.Stream"))
         (vlax-invoke ADOStream 'Open)
         (vlax-put-property   ADOStream 'type 1)
         (vlax-invoke-method  ADOStream 'loadfromfile filename)
         (vlax-put-property   ADOStream 'position 0)
         (setq size (vlax-get ADOStream 'size))
         (vlax-invoke-method  ADOStream 'read (if (and (numberp len) (< 0 len size)) (fix len) -1))
       )
     )
   )
 )
 (if ADOStream (vlax-release-object ADOStream))

 (if (not (vl-catch-all-error-p result))
   result
 )
)

;;-----------------=={ Write Binary Stream }==----------------;;
;;                                                            ;;
;;  Uses the ADO Stream Object to write a variant of bytes to ;;
;;  a specified file. File is created if non-existent or      ;;
;;  overwritten if found.                                     ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  filename - filename of file to read.                      ;;
;;  data     - variant of binary data to write to the file.   ;;
;;             (as returned by LM:ReadBinaryStream)           ;;
;;------------------------------------------------------------;;
;;  Returns:  Filename of specified file, else nil.           ;;
;;------------------------------------------------------------;;

(defun LM:WriteBinaryStream ( filename data / ADOStream result ) (vl-load-com)
 
 (setq result
   (vl-catch-all-apply
     (function
       (lambda ( )
         (setq ADOStream (vlax-create-object "ADODB.Stream"))
         (vlax-put-property ADOStream 'type 1)
         (vlax-invoke ADOStream 'open)
         (vlax-invoke-method ADOStream 'write data)
         (vlax-invoke ADOStream 'savetofile filename 2)
       )
     )
   )
 )
 (if ADOStream (vlax-release-object ADOStream))

 (if (not (vl-catch-all-error-p result))
   file
 )
)

Edited by Lee Mac

Share this post


Link to post
Share on other sites
Ahankhah

Excellent Lee Mac:D

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...