How to access string added with AHK2EXE AddResource directive Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Randy31416
Posts: 58
Joined: 15 Jan 2014, 19:09

How to access string added with AHK2EXE AddResource directive

Post by Randy31416 » 27 Sep 2022, 18:06

The AHK2EXE compiler directive AddResource allows a file to be incorporated into a compiled module. I want to put a small text file into the module and then access the text using code within the module. I've tried things like

Code: Select all

 ;Ahk2Exe-AddResource *6 TestTextFile.txt, 33  
which should add resource item 33 as an RT_STRING. (I've also tried *10 for an RT_RCDATA item.) But I have found no way, after lots of searching and fiddling with LoadString DLL calls, actually to fetch the AddResourced data so I can manipulate it in code. LoadString wants a resource identifier; hence the 33. Any pointers?

Randy31416
Posts: 58
Joined: 15 Jan 2014, 19:09

Re: How to access string added with AHK2EXE AddResource directive  Topic is solved

Post by Randy31416 » 28 Sep 2022, 10:19

Finally I found some code by SKAN and after a small adaptation it will do what I want.

Code: Select all

;@Ahk2Exe-AddResource *6 G:\testfile.txt, testdata
Size := ResRead(Data, "testdata")
MsgBox, [%Size%] [%Data%]
ExitApp

; ResRead() By SKAN, from http://www.autohotkey.com/board/topic/57631-crazy-scripting-resource-only-dll-for-dummies-36l-v07/?p=609282
;   adapted to change Uint to Ptr in pointer arguments
;   adapted to use 6 for RC_STRING, rather than 10 for RC_DATA (either will work) in call to FindResource
;   adapted to return a translated string

ResRead( ByRef Var, Key )
{
  VarSetCapacity( Var, 128 ), VarSetCapacity( Var, 0 )
  If ! ( A_IsCompiled )
  {
    FileGetSize, nSize, %Key%
    FileRead, Var, *c %Key%
    Return nSize
  }
  If hMod := DllCall( "GetModuleHandle", UPtr,0 , Ptr)
    If hRes := DllCall( "FindResource", Uptr,hMod, Str,Key, UInt,6 , Ptr)
      If hData := DllCall( "LoadResource", UInt,hMod, UPtr,hRes , Ptr)
        If pData := DllCall( "LockResource", UInt,hData, Ptr )
        {
          VarSetCapacity( Var, nSize := DllCall( "SizeofResource", UInt,hMod, UInt,hRes ) )
          DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
          Var := StrGet(&Var, nsize, "UTF-8")
          Var := StrReplace(strreplace(Var, "`n",""), "`r", "")
          Return, StrLen(Var)
        }
  Return 0
}

Post Reply

Return to “Ask for Help (v1)”