flat assembler
Message board for the users of flat assembler.

Index > Windows > WAV file resource not found

Author
Thread Post new topic Reply to topic
alszla



Joined: 24 Sep 2016
Posts: 5
alszla 24 Sep 2016, 19:11
Hi. I am having trouble makin API PlaySound play a wav file defined in a resource file.

On one hand, I have this "external" RC script (to be compiled with MASM32's rc):

Code:
1000 WAVE sound.wav
    


and somewhere in my source code (to be compiled with FASM):

Code:
invoke PlaySound,1000,[wHInstance], SND_ASYNC + SND_RESOURCE
    


I also include the resource file like this:

Code:
section '.rsrc' data readable resource from 'woof.res'
    


And the resulting executable works as expected. It plays the wave file as soon as the window is displayed.

On the other hand, when I try to rewrite the resource section with FASM syntax:

Code:
section '.rsrc' data readable resource
        directory RT_RCDATA,sounds
                  
        resource sounds,\
                 1000,LANG_NEUTRAL,somename
        
        resdata somename
                file 'sound.wav'
        endres
    


The resulting executable doesn't seem to find the resource. Debugging it in OllyDbg shows "ERROR_RESOURCE_TYPE_NOT_FOUND" error in OllyDbg.

I found similar posts here at the forums describing this behaviour, but all of them give alternate solutions either by defining the file using 'file' directive in data section OR by using sndPlaySound API along with Lock/Free/Load/FindResource APIs.
I also tested all of those alternate solutions and while all of them worked, I couldn't find any code that uses just PlaySound API to play a sound from a resource defined in FASM syntax.
Microsoft reference states that PlaySound takes care of all the process of finding and loading the resource.
https://msdn.microsoft.com/en-us/library/dd743679%28v=vs.85%29.aspx
So there wouldn't be any need to use LoadResource, FindResource APIs.
I also find that using Playsound allows simpler and cleaner code. Just a personal preference.

Is there anything wrong with the resource definition in my source code? OR Is this some kind of limitation in the way how FASM manages resources??

Thanks for all the help you've all provided so far. These forums have been an extremely useful source of guidance and solutions for many of my programs.

If you find any mistake in the resource definition, please let me know.
Post 24 Sep 2016, 19:11
View user's profile Send private message Reply with quote
Walter



Joined: 26 Jan 2013
Posts: 155
Walter 27 Sep 2016, 10:59
alszla,

Quote:
Microsoft reference states that PlaySound takes care of all the process of finding and loading the resource.


Hadn't thought about that. While most coders here could debug the PlaySound API in an hour, it would take me three days. So instead, I looked to see what ReactOS does.

https://doxygen.reactos.org/db/ddf/playsound_8c_source.html

In proc_PlaySound you will see FindResource, LoadResource, LockResource, ...

It now makes sense to me why the examples on this board use these functions with RT_RCDATA and sndPlaySound.

Fasm does not support the named resource type (not what you wanted to hear).

Here is an example that sort of gets what you want. "FASM syntax" and it does use a resource. However, it would be uncomfortable to work with resources in this fashion.

Code:
;*****************
;* PlaySound.asm *
;*****************

format pe gui 4.0
entry start

include 'win32a.inc'

IDW_WAVE = 100
SND_SYNC = 0x0
SND_RESOURCE = 0x40004

section '.code' code readable executable

  start:

        invoke  GetModuleHandle,0
        invoke  PlaySound,IDW_WAVE,eax,SND_SYNC+SND_RESOURCE
        invoke  ExitProcess,0

section '.idata' import data readable

  library kernel32,'kernel32.dll',\
          winmm,'winmm.dll'

  import kernel32,\
         ExitProcess,'ExitProcess',\
         GetModuleHandle,'GetModuleHandleA'

  import winmm,\
         PlaySound,'PlaySoundA'

section '.rsrc' resource data readable

; IMAGE_RESOURCE_DIRECTORY

  dd 0x0         ; Characteristics.
  dd 0x57e9bcb3  ; TimeDateStamp (9/26/2016, 8:26:27 PM)
  dw 0x0         ; MajorVersion.
  dw 0x0         ; MinorVersion.
  dw 0x1         ; NumberOfNamedEntries.
  dw 0x0         ; NumberOfIdEntries.

; IMAGE_RESOURCE_DIRECTORY_ENTRY

  dd 0x80000048  ; Name.  High bit set, NameIsString true.  Lower 31 bits, is NameOffset. 
  dd 0x80000018  ; OffsetToData.  High bit set, DataIsDirectory true.  Lower 31 bits, is OffsetToDirectory.

; IMAGE_RESOURCE_DIRECTORY

  dd 0x0         ; Characteristics.
  dd 0x57e9bcb3  ; TimeDateStamp.
  dw 0x0         ; MajorVersion.
  dw 0x0         ; MinorVersion.
  dw 0x0         ; NumberOfNamedEntries.
  dw 0x1         ; NumberOfIdEntries.

; IMAGE_RESOURCE_DIRECTORY_ENTRY

  dd IDW_WAVE    ; ID.  WaveFile Id.
  dd 0x80000030  ; OffsetToData.  High bit set, DataIsDirectory true.  Lower 31 bits, is OffsetToDirectory.

; IMAGE_RESOURCE_DIRECTORY

  dd 0x0         ; Characteristics.
  dd 0x57e9bcb3  ; TimeDateStamp.
  dw 0x0         ; MajorVersion.
  dw 0x0         ; MinorVersion.
  dw 0x0         ; NumberOfNamedEntries.
  dw 0x1         ; NumberOfIdEntries.

; IMAGE_RESOURCE_DIRECTORY_ENTRY (Resource language)

  dd 0x00000409  ; Resource language 0x409 = 1033
  dd 0x00000054

; IMAGE_RESOURCE_DIR_STRING_U

  dw 4                    ; Length.  Length of resource type name.
  du 'W','A','V','E',0x0  ; NameString.

; IMAGE_RESOURCE_DATA_ENTRY

  dd RVA WaveFileStart            ; OffsetToData.  RVA of the actual data.
  dd WaveFileEnd - WaveFileStart  ; Size.  Size in bytes of WaveFile.
  dd 0x0                          ; CodePage.
  dd 0x0                          ; Reserved.

WaveFileStart:

  file 'Bdbnlck.wav'

WaveFileEnd:
    


Rename Bdbnlck.zip to Bdbnlck.wav if you decide to use this.


Description:
Download
Filename: Bdbnlck.zip
Filesize: 41.73 KB
Downloaded: 477 Time(s)

Post 27 Sep 2016, 10:59
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8354
Location: Kraków, Poland
Tomasz Grysztar 27 Sep 2016, 11:16
Walter wrote:
Fasm does not support the named resource type (not what you wanted to hear).

Here is an example that sort of gets what you want. "FASM syntax" and it does use a resource. However, it would be uncomfortable to work with resources in this fashion.
The standard resource construction macros that come with fasm are indeed very old, their syntax has not been updated since 2001 and they come from the times when fasm's macro facilities were much, much simpler than they are now.

Really, some new macros for resource should have been designed and written nowadays. I think it would be great to create some new syntax and then implement it as macro packages for both fasm and fasmg. I did not even port the old resource macros to fasmg, just because how outdated their design is. And in case of fasmg, I think that it could even be possible to create macros that could parse the standard resource script syntax.
Post 27 Sep 2016, 11:16
View user's profile Send private message Visit poster's website Reply with quote
alszla



Joined: 24 Sep 2016
Posts: 5
alszla 27 Sep 2016, 23:48
Walter,

I appreciate your taking the time to debug the issue and actually come up with a solution and a conclusive answer. As you mentioned, it might be uncomfortable using this notation, but in the end, it really shows PlaySound API reading a resource defined in the same source code.

I must also admit that I find the definition you proposed in the resource section very informative. As newbie as I am, it gives me an in-depth look at the nature of a resource. Correct me if I'm wrong: This notation is about using PE file format directly, right?

Once again, thanks for your effort ( and for the lesson Smile ).
Post 27 Sep 2016, 23:48
View user's profile Send private message Reply with quote
alszla



Joined: 24 Sep 2016
Posts: 5
alszla 29 Sep 2016, 05:40
As to using macros, I think that reading the resource.inc file (included in the fasm package) and the PE format specification are kind of mandatory in order for me to get a better understanding of how resources are constructed so I can try following a similar approach to the one used here: https://board.flatassembler.net/topic.php?t=2370 where a macro has been written in order to implement a stringtable in the resource section.
Post 29 Sep 2016, 05:40
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.