Pulling information from a mrimg file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Pulling information from a mrimg file

16 May 2023, 18:20

Macrium Reflect image files have comments. I know a very clunky way to get this information from an mrimg file selected in explorer by bringing up the proprieties dialogue selecting the Macrium Reflect tab and pulling the text from the edit1 field which has the comment. Does anyone know of a more robust method to pull the comment from an mrimg file selected from an ahk fileselectfile dialogue? Thanks.

Here's the code I'm using:

Code: Select all

if instr(ext,"mrimg")
{
	send {appskey}{up}{enter}
	sleep 300
	SendMessage, 0x1330, 1,, SysTabControl321, A ; 0x1330 is TCM_SETCURFOCUS.	
	sleep 100
	ControlgetText, mtxt, Edit1, A
	send {esc}
	mtxt := trim(mtxt, " `n`r")
	
	Gui, img:new, +AlwaysOnTop  -dpiscale
	Gui, Color, 0086e0, white
	Gui, margin, 25, 25
	Gui, font, s20 normal
	Gui, add, text, w600 cwhite vchk, % mtxt

	Guicontrolget, chk, img:pos
	if chkh > 800
	Guicontrol, img:move, chk, %  "w" 1200 "h" Ceil(chkh * 0.51)

	Gui, show, x2000 ycenter autosize, %file%		

	return
	
	imgGuiEscape:
	Gui, img:destroy
	return
}
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Pulling information from a mrimg file

16 May 2023, 19:35

Thanks for the reply. I hadn't and I did and I got an out of memory error message. Mrimg files are over 40 gb in size. I tried the *m1024 parameter but got a blank msgbox.
User avatar
mikeyww
Posts: 27373
Joined: 09 Sep 2014, 18:38

Re: Pulling information from a mrimg file

16 May 2023, 22:11

OK. That was my only idea!
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Pulling information from a mrimg file

16 May 2023, 22:39

mikeyww wrote:
16 May 2023, 22:11
OK. That was my only idea!
But good to know, I never think about FileRead.
RussF
Posts: 1312
Joined: 05 Aug 2021, 06:36

Re: Pulling information from a mrimg file

17 May 2023, 05:37

I haven't tried this, so I'm just spit-ballin' here, but if the metadata you're looking for is at the beginning of the file and you know where it is (you may be able to determine that by looking at it in a hex editor such as the extension available for VSCode), then could you use the *m option of FileRead to limit the number of bytes read to only that which contains the metadata? Once you've determined the offset from the beginning of the file, you could use SubStr() to extract the portion you're looking for before sending it to a MsgBox.

Russ
RussF
Posts: 1312
Joined: 05 Aug 2021, 06:36

Re: Pulling information from a mrimg file

17 May 2023, 14:29

So I went way farther down this rabbit hole than I had intended - I hope my boss doesn't find out.

Since I use Macrium, I thought this would be a fun "little" project to spend a little time on. Macrium saves its metadata in XML byte format (one byte per character rather than 2) within the MRIMG file. The comments are between <comment></comment> tags. I figured I could just use a file object to read the file in chunks and search for the text. Wrong. AHK's string functions (RegExMatch as well) do not work when there are embedded zeros (nulls) within the haystack.

After a search, I found some archived AHK functions here that seemed to fit the bill perfectly. The poster had put inline machine code in them to search regardless of nulls. After much trial and error, however, I could never get them to work for me.

More searching brought me to some other posts where scripts had been created to copy the file buffer they were using, byte by byte, to a secondary buffer, replacing the nulls with other codes along the way and then searching the secondary buffer. After all, we don't care about anything but the XML text anyway.

I don't typically fill in the comment fields on my images, so I created a small one of a leftover 16MB partition on my drive and wrote the code below. After getting all the offsets correct, it finally worked. I then created another image of my recovery partition of about 500 MB. I ran the script and thought my machine had hung. It took about 5 minutes to finally come up with the comments. The metadata was located near the end of the file and AHK had a LOT of nulls to replace. And I have a very fast machine.

Examining another file (I installed the free XhD hex editor) showed the XML data to be much nearer to the top of the file. There seems to be no rhyme nor reason for the placement of the metadata and there MUST be a pointer of some kind near the beginning of the file pointing to its location which is how the properties tab can display it so quickly. I couldn't figure out where that pointer was, however.

Feel free to use the code below to play with, but start with very SMALL images. It's not pretty because there was a lot of hacking and trial and error required to come up with it. I'm sure it could be written more efficiently, but it is what it is. Perhaps someone else can use it as a springboard to come up with something faster, but I've spent WAY too much time playing with it. Have to actually get some work done now. :crazy:

Code: Select all

#Requires AutoHotkey v1.1.33+

ImgFile := A_Desktop . "\MR-Test.mrimg"
Buffer := ""
BegTag := "<comment>"
BegTagLen := StrLen(BegTag)
EndTag := "</comment>"

oFile := FileOpen(ImgFile, "r")
While(!oFile.AtEOF) {
  VarSetCapacity(Buffer, 0)
  oFile.RawRead(Buffer, 4096)
  BuffLen := StrLen(Buffer) * 2
  NewBuff := ""
  
  Loop %BuffLen% {
    Char := NumGet(Buffer, A_Index, "UChar")
    If (Char = 0) {
      NewBuff .= " "
    } Else {
      NewBuff .= Chr(Char)
    }
  }
  BegOffset := InStr(NewBuff, BegTag)
  BegOffset += BegOffset = 0 ? 0 : oFile.Pos - 4096
  EndOffset := Instr(NewBuff, EndTag)
  EndOffset += EndOffset = 0 ? 0 : oFile.Pos - 4096
  If ((BegOffset > 0) && (EndOffset > 0))
    Break
}
oFile.Seek(BegOffset + BegTagLen, 0)
CommentText := oFile.Read(EndOffset - (BegOffset + BegTagLen))
oFile.Close()

MsgBox % "Comment: " . CommentText

ExitApp
Russ
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Pulling information from a mrimg file

22 May 2023, 18:52

Thanks, RussF for taking the time to look at this. I tried your code on one of my old images but gave up waiting for it to find the comments. I don't have the confidence to try to rework your script. For now I'll keep using my clunky method and see if I can get it working with a FileSelectFile dialogue. Thanks again.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Google [Bot], Rohwedder and 136 guests