Jump to content

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

Crazy Scripting : Resource-Only DLL for Dummies - 36L / v0.7


  • Please log in to reply
71 replies to this topic
HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Works fine for me, try that:

FileRead,tada,*c C:\Windows\Media\tada.wav
PlaySound(tada)
PlaySound( ByRef Sound ) {
 Return DllCall( "winmm.dll\PlaySound" ( A_IsUnicode ? "W" : "A" ), UInt,&Sound, UInt,0
               , UInt, 0x6 ) ; SND_MEMORY := 0x4 | SND_NODEFAULT := 0x2
}


Skrell
  • Members
  • 384 posts
  • Last active: Jul 07 2016 05:03 PM
  • Joined: 23 Aug 2011

 

Works fine for me, try that:

FileRead,tada,*c C:\Windows\Media\tada.wav
PlaySound(tada)
PlaySound( ByRef Sound ) {
 Return DllCall( "winmm.dll\PlaySound" ( A_IsUnicode ? "W" : "A" ), UInt,&Sound, UInt,0
               , UInt, 0x6 ) ; SND_MEMORY := 0x4 | SND_NODEFAULT := 0x2
}

Ok so this now works for me.   I was using an older ahk2exe and apparently it didn't support this functionality or something?  

Anyway, i'm still stuck on my other problem where NO sounds will play in my hotkey section whether compiled or not.  Currently I put the ResRead function call for my waves ( i have 2) in the autoexec section and the i figured the References would be established and i could just call PlaySound in my hotkey section.  Assuming for some reason this was wrong i moved the ResRead function calls INTO my hotkey section and that still didn't work.  Any thoughts?



Skrell
  • Members
  • 384 posts
  • Last active: Jul 07 2016 05:03 PM
  • Joined: 23 Aug 2011

Here try this:

#NoEnv 
ResRead( tada, "tada.wav" )
return

#p::
PlaySound(tada)
return

PlaySound( ByRef Sound ) {
 Return DllCall( "winmm.dll\PlaySound" ( A_IsUnicode ? "W" : "A" ), UInt,&Sound, UInt,0
               , UInt, 0x6 ) ; SND_MEMORY := 0x4 | SND_NODEFAULT := 0x2
}


FileInstall, C:\Windows\Media\tada.wav, -
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", UInt,0 )
    If hRes := DllCall( "FindResource", UInt,hMod, Str,Key, UInt,10 )
      If hData := DllCall( "LoadResource", UInt,hMod, UInt,hRes )
        If pData := DllCall( "LockResource", UInt,hData )
  Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", UInt,hMod, UInt,hRes ) )
      ,  DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
Return 0    
}


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

You were very close :) ResRead( tada, "C:\Windows\Media\tada.wav")



Skrell
  • Members
  • 384 posts
  • Last active: Jul 07 2016 05:03 PM
  • Joined: 23 Aug 2011

You were very close :) ResRead( tada, "C:\Windows\Media\tada.wav")

Nope, still didn't work.  Did you try compiling the code i gave you with your change and running it?



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

I tried and it works fine for me.



Skrell
  • Members
  • 384 posts
  • Last active: Jul 07 2016 05:03 PM
  • Joined: 23 Aug 2011

I tried and it works fine for me.

sigh...what version of windows and what version of the ahk2exe compiler are you using ?  Is there some flag or something i need to set for the compiler?  

 

I've tried it now on both win7 and win8.1 and neither work for the compiled version.  There is definitely some small thing i'm missing here. 



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Check Resource with ResHacker or similar, also check what size ResRead returns.

MsgBox % ResRead( tada, "C:\Windows\Media\tada.wav") should be the same size as FileGetSize,sz,C:\Windows\Media\tada.wav.



Skrell
  • Members
  • 384 posts
  • Last active: Jul 07 2016 05:03 PM
  • Joined: 23 Aug 2011

Check Resource with ResHacker or similar, also check what size ResRead returns.

MsgBox % ResRead( tada, "C:\Windows\Media\tada.wav") should be the same size as FileGetSize,sz,C:\Windows\Media\tada.wav.

Thank you for the suggestion! Finally i'm getting somewhere.  The compiled exe says that the returned value from ResRead = 0 ??   So then i'd assume

If pData := DllCall( "LockResource", UInt,hData )

is coming back false and hence i'm skipping to the last return statement of Return 0;  Thoughts?

Also nsize is correct when called if i execute the non-compiled file.  (again the non-compiled version works fine)

 

*UPDATE*
upon more testing it looks like THE line that is failing is this one:

If hRes := DllCall( "FindResource", UInt,hMod, Str,Key, UInt,10 )

 

Hopefully you have a suggestion

btw all the system's i've tested on are 64 bit...could that be the problem?



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

In that case you need to adapt ResRead to 64-bit because returned values are 64-bit pointers  :)

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", UInt,0,PTR )
    If hRes := DllCall( "FindResource", UInt,hMod, Str,Key, UInt,10,PTR )
      If hData := DllCall( "LoadResource", UInt,hMod, UInt,hRes,PTR )
        If pData := DllCall( "LockResource", UInt,hData,PTR )
  Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", UInt,hMod, UInt,hRes,PTR ) )
      ,  DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
Return 0    
}


Skrell
  • Members
  • 384 posts
  • Last active: Jul 07 2016 05:03 PM
  • Joined: 23 Aug 2011

 

In that case you need to adapt ResRead to 64-bit because returned values are 64-bit pointers  :)

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", UInt,0,PTR )
    If hRes := DllCall( "FindResource", UInt,hMod, Str,Key, UInt,10,PTR )
      If hData := DllCall( "LoadResource", UInt,hMod, UInt,hRes,PTR )
        If pData := DllCall( "LockResource", UInt,hData,PTR )
  Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", UInt,hMod, UInt,hRes,PTR ) )
      ,  DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
Return 0    
}

AHHHH!!! If this fixes my problems i'm going to blow my brains out!!!

 

*UPDATE*
Yep that was it!!  Please note that this version works in BOTH 32bit and 64bit and should be the preferred code for all!  Out of curiosity what does adding the "PTR" parameter do exactly?



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Out of curiosity what does adding the "PTR" parameter do exactly?

It assumes that function returns a pointer which is 8 bytes on 64-bit and 4 bytes on 32 bit (default return type for DllCall is int = 4 bytes).