Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Binary file reading and writing


  • Please log in to reply
28 replies to this topic
kiu
  • Members
  • 234 posts
  • Last active: Oct 10 2010 07:30 PM
  • Joined: 18 Dec 2005
if someone is interested:
https://ahknet.autoh...iu-clipsave.exe
use it to save an image on clipboard in png and jpg.When launched it save the image in image.jpg and image.png
____________________
______________________
kiu - www.romeosa.com

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
If someone is interested: :-)
SetClipboardData is a pure AutoHotkey (with DllCall) solution to this problem.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

madewokherd
  • Members
  • 5 posts
  • Last active: Oct 26 2012 09:34 PM
  • Joined: 07 Jul 2007
PhiLho: This is perfect for a problem I have, but the project I'd like to use it in is open source (GPL). You haven't given a license, and I'd like to avoid any potential legal problems. I would expect that since you've posted your source code for all to see, you are ok with this kind of use. Would you give it a license compatible with the GPL so that I can use it?

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
PhiLho is unavailable for a few weeks. Hopefully he'll notice your post the next time he gets a chance to visit the forum.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
If you use my original version, it is absolutely free. You can even sell the script (not AHK) for profit, if you want. However, with the recent AHK updates, all of these functions can be greatly simplified and sped up.

madewokherd
  • Members
  • 5 posts
  • Last active: Oct 26 2012 09:34 PM
  • Joined: 07 Jul 2007
Thanks.

On looking more carefully, I think I misunderstood PhiLho's script, and it doesn't provide something I need (it may not be possible, and certainly isn't trivial). I guess I'm better off reading/writing the whole file as a variable. :/

apocalypse r
  • Guests
  • Last active:
  • Joined: --
these were adapted from the functions in binreadwrite.ahk provided by PhilLo, and were designed for writing or reading just once; they open and close the handles automatically.

readfiletovar(filename, vartowritecontentsoffileto, bytestoread, startlocation, offset)
startlocation can be -1 for dont move, 0 for start at beginning, 1 for current location, 2 for end of file, and offset is the number of bytes to the left of startlocation to begin reading from (which can be negative). 0 stands for at specified start location

readfiletovar(_filename, byref _data, _byteNB = 0, _MoveMethod = -1, _offset = 0)
{
  local handle, granted, read
  handle := Dllcall("CreateFile", "Str", _filename, "UInt", 0x80000000, "UInt", 3, "UInt", 0, "UInt", 3, "UInt", 0, "UInt", 0)
  if (handle = INVALID_HANDLE_VALUE or handle = 0)
  {
    errorlevel = -1
    return, -1
  }
  if (_moveMethod != -1)
  {
    _offset := DllCall("SetFilePointer", "UInt", handle, "Int", _offset, "UInt", 0, "UInt", _moveMethod)
    if (_offset = -1)
    {
      ErrorLevel = -2
      dllcall("CloseHandle", "UInt", handle)
      return -2
    }
  }
  if (_byteNB = 0)
  {
    _byteNB := Dllcall("GetFileSize", "UInt", handle, "UInt", 0)
    if (_byteNB = 0xFFFFFFFF)
    {
      errorlevel = -3
      dllcall("CloseHandle", "UInt", handle)
      return -3
    }
  }
  granted := varsetcapacity(_data, _byteNB, 0)
  if (granted < _byteNB)
  {
    errorlevel := %granted%
    dllcall("CloseHandle", "UInt", handle)
    return -4
  }
  if (dllcall("ReadFile", "UInt", handle, "Str", _data, "UInt", _byteNb, "UInt *", read, "UInt", 0) = 0)
  {
    errorlevel := -5
    dllcall("CloseHandle", "UInt", handle)
    return -5
  }
  dllcall("CloseHandle", "UInt", handle)
  return read
}

writevartofile(filename, varcontainingdatatowritetofile, bytestowrite, startingposition, offset)
startlocation can be -1 for dont move, 0 for start at beginning, 1 for current location, 2 for end of file, and offset is the number of bytes to the left of startlocation to begin reading from (which can be negative). 0 stands for at specified start location

writevartofile(_filename, byref _data, _byteNB, _movemethod, _offset)
{
  local handle, datasize, written
  handle := dllcall("CreateFile", "Str", _filename, "UInt", 0x40000000, "UInt", 3, "UInt", 0, "UInt", 4, "UInt", 0, "UInt", 0)
  if (handle = INVALID_HANDLE_VALUE or handle = -1)
  {
    errorlevel := -1
    return, -1
  }
  if (_moveMethod != -1)
  {
    _offset := DllCall("SetFilePointer", "UInt", handle, "Int", _offset, "UInt", 0, "UInt", _moveMethod)
    if (_offset = -1)
    {
      errorLevel = -2
      dllcall("CloseHandle", "UInt", handle)
      return -2
    }
  }
  datasize := varsetcapacity(_data)
  if (_byteNB < 1 or byteNB > datasize)
    _byteNB := datasize
  if (dllcall("WriteFile", "UInt", handle, "Str", _data, "UInt", _byteNb, "UInt *", written, "UInt", 0) = 0 or written < _byteNB)
  {
    errorlevel := -3
    dllcall("CloseHandle", "UInt", handle)
    return -3
  }
  dllcall("CloseHandle", "UInt", handle)
  return written
}


WankaUSR
  • Members
  • 87 posts
  • Last active: Jul 14 2013 09:59 AM
  • Joined: 14 Aug 2007
can anyone please help me with a gui for this because i don't quite understand how to use functions

amnesiac
  • Members
  • 124 posts
  • Last active: May 01 2014 03:04 AM
  • Joined: 07 Nov 2010
Thank you for Bin2Hex() and Hex2Bin(). I am very insteresting with this conversion, so I write Raw2Hex() and Hex2Raw() and they are only suitable for AutoHotkey_L and can work better for AutoHotkey_L.

16-07-2012 Edit: Change the link to these functions.

Click to download Chinese resource for AutoHotkey.

Recommended: AutoHotkey_L My code is based on it or similar versions, e.g. AutoHotkey_H.
Together with AutoHotkey, we grow and march forward. No matter how the future will be, this period of days is still epic.


szujeq
  • Members
  • 304 posts
  • Last active: Jan 12 2017 09:11 PM
  • Joined: 28 Mar 2011

did ReadFromFile function has limit for offset??

Offset                ID               IDName       Size

(0x7FEFED95) 1F43B675: CLUSTER*[0x171F9B]: here work fine
(0x80070D37) 371F9B: ERROR[0x171F9B]: but while 32th bit appear then my script says error cause wrong read function

 

is it possible to read offset larger than 31bits??



just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

You don't need this functions for AHK1.1, just use the File Object instead.
 
Otherwise you might change the  MoveInFile() function on Win XP and later to

MoveInFile(_handle, _moveMethod=-1, _offset=0)
{       ; http://msdn.microsoft.com/en-us/library/windows/desktop/aa365542(v=vs.85).aspx
	local newfilepointer, result

	result = %INVALID_FILE_SIZE%
	if (_moveMethod != -1)
	{
		result := DllCall("SetFilePointerEx"            ; this function is using large integers (64 bits)
				, "UInt",  _handle		; hFile
				, "Int64", _offset		; liDistanceToMove
				, "Int64P", newfilepointer      ; lpNewFilePointer
				, "UInt", _moveMethod)	        ; dwMoveMethod
		if (result = 0)	                                ; if the function fails, the return value is zero.
		{
			ErrorLevel = -1
		}
		IfNotEqual ErrorLevel, 0, Return -1	        ; Could not make the move
	}
	Return newfilepointer
}

*not tested*


Prefer ahkscript.org for the time being.


szujeq
  • Members
  • 304 posts
  • Last active: Jan 12 2017 09:11 PM
  • Joined: 28 Mar 2011

thanks it work.

Additionaly I want to ask is there any difference on performace between method of set pointer??

For example for me easier is use from begin file, but is it same fast as using from current??

Now I mean build in AHK functions.



tuipveus
  • Members
  • 4 posts
  • Last active: Sep 27 2013 07:57 PM
  • Joined: 20 Sep 2013

I'm trying to write an image contained in clipboard with this code, but it doesn't work
 




file = e:\temp3.bmp
IfExist, %file%
FileDelete, %file%
fh := OpenFileForWrite(file)
l := Hex2Bin(data, ClipboardAll)
WriteInFile(fh,data )
CloseFile(fh)
What's wrong?

 

 

My first post in this forum!

 

I tried similar way other data (not clipboard) but I was not able to write 1 byte. At first I have data 89h 50h 4Eh and what I got was 00h 89h 50h 4Eh.

 

I tried to modify WriteInFile() to write only 1 byte, but not successful. Any ideas?

 

edit: There is always extra 0 coming to beginning. I can write only even bytes, 2, 4, 6, 8, 10 etc... Not odd: 1, 3, 5

 

I tried to shift data with >> and << but it didn't help either. Problem is in WriteInFile I guess and str datatype perhaps?



tuipveus
  • Members
  • 4 posts
  • Last active: Sep 27 2013 07:57 PM
  • Joined: 20 Sep 2013

With Laszlos original functions I can write 1 byte also, but I don't know how can I combine binary- or hex-strings. I have tried to join them and used both join and ".", but no luck. "Original" Laszlos function does not support appending either...