Ahankhah Posted March 2, 2011 Posted March 2, 2011 Dear all:), Is it possible to read/write a binary file from Visual LISP:? Quote
Lee Mac Posted March 2, 2011 Posted March 2, 2011 (edited) 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 March 2, 2011 by Lee Mac Quote
Recommended Posts
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.