Vous êtes sur la page 1sur 5

td_win32asm_205.

asm
;==============================================================================
;
Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE's
205
;==============================================================================
;==============================================================================
; ==> Part 205 : How to include a midi file into the exe via resource script.
;-----------------------------------------------------------------------------; High,
; thanks to Stefan for the idea to write this example.
; RCDATA statement in the resource script includes the midi file to the exe.
; The included midi datas are not changed, so also the FileHeader is included !
; I get a pointer to the first byte in the resource via API's "FindResource",
; "LoadResource" and "LockResource".
; Remember, the first byte is the FileHeader !
; I also get the size of the resource via API "SizeofResource".
; This is critical because WIN32.HLP told me the returned size could be wrong.
; I try several ways ( midiOutOpen, midiStreamOut, mmioopen ... ) to transfer
; this datas to a midi device, but it always failed ...
; Switching on my brain (always a good idea) I found a solution :
; On runtime I save the datas to the HDD (temp.mid) via API's "CreateFile",
; "WriteFile" and now I call API "mciSendCommandA" to open the midi sequencer
; using the temporary file as the source.
; If you know a better solution please give me a note.
;
; Test Department
td@crahkob.com
;==============================================================================
; Assembler directives
;-----------------------------------------------------------------------------.386
; specifies the processor our program want run on
.Model Flat ,StdCall
; always the same for Win95 (32 Bit)
option casemap:none
; case sensitive !!!
;==============================================================================
; Include all files where API functins resist you want use
; You must set the correct path to the include and library files
;-----------------------------------------------------------------------------include D:\Masm32\include\windows.inc
includelib kernel32.lib
includelib user32.lib
includelib winmm.lib
;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
;-----------------------------------------------------------------------------GetModuleHandleA
PROTO :DWORD
FindResourceA
PROTO :DWORD,:DWORD,:DWORD
SizeofResource
PROTO :DWORD,:DWORD
LoadResource
PROTO :DWORD,:DWORD
LockResource
PROTO :DWORD
CreateFileA
PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
WriteFile
PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
CloseHandle
PROTO :DWORD
Page 1

mciSendCommandA
MessageBoxA
ExitProcess

td_win32asm_205.asm
PROTO :DWORD,:DWORD,:DWORD,:DWORD
PROTO :DWORD,:DWORD,:DWORD,:DWORD
PROTO :DWORD

;==============================================================================
; .Data = the data area starts here, datas are defined but not fixed
;-----------------------------------------------------------------------------.Data
SoundName
db "TDSound",0
;sound name in resource
MB1Titel
db "Program Error",0
;message box name
MB1Text
db "Bad work ...",0
;message box text
MB2Titel
db "End of Program",0
;message box name
MB2Text
db "Do you here the sound ?",0 ;message box text
FileName
db "temp.mid",0
;temporary filename
;==============================================================================
; .Data? = the data? area starts here, not defined and not fixed
;-----------------------------------------------------------------------------.data?
hInstance
dd ?
handleResource
dd ?
sizeResource
dd ?
pointerResource
dd ?
handleFile
returnFile

dd ?
dd ?

align 4
; - MCI_OPEN_PARMS Structure ( API=mciSendCommand ) open_dwCallback
dd ?
open_wDeviceID
dd ?
open_lpstrDeviceType
dd ?
open_lpstrElementName
dd ?
open_lpstrAlias
dd ?
align 4
; - MCI_GENERIC_PARMS Structure ( API=mciSendCommand ) generic_dwCallback
dd ?
align 4
; - MCI_PLAY_PARMS Structure ( API=mciSendCommand ) play_dwCallback
dd ?
play_dwFrom
dd ?
play_dwTo
dd ?
;==============================================================================
; .CODE = our code area starts here
Main = label of our program code
;-----------------------------------------------------------------------------.Code
Main:
;==============================================================================
; Always get your program ID first (API=GetModuleHandleA)
Page 2

td_win32asm_205.asm
;-----------------------------------------------------------------------------push
0h
;lpModuleHandle, 0=get program handle
call
GetModuleHandleA
;- API Function mov
hInstance,eax
;return value in eax=handle of program
;==============================================================================
; API "FindResource" determines the location of a resource with the specified
; type and name in the specified module
;-----------------------------------------------------------------------------push
10
;lpType,address of resource type, RT_RCDATA
push
OFFSET SoundName
;lpName, address of resource name
push
hInstance
;hModule, resource-module handle
call
FindResourceA
;- API Function cmp
eax,0h
;
je
ErrorPrg
;
mov
handleResource,eax
;
;-----------------------------------------------------------------------------; API "SizeofResource" returns the size, in bytes, of the specified resource.
;-----------------------------------------------------------------------------push
handleResource
;hrsrc, resource handle
push
hInstance
;hModule, resource-module handle
call
SizeofResource
;- API Function cmp
eax,0h
;
je
ErrorPrg
;
mov
sizeResource,eax
;returned value is critical ( WIN32.HLP )
;-----------------------------------------------------------------------------; API "LoadResource" loads the specified resource into global memory.
;-----------------------------------------------------------------------------push
handleResource
;hResInfo, resource handle
push
hInstance
;hModule, resource-module handle
call
LoadResource
;- API Function cmp
eax,0h
;
je
ErrorPrg
;
;-----------------------------------------------------------------------------; API "LockResource" locks the specified resource in memory.
;-----------------------------------------------------------------------------push
eax
;hResData, handle to resource to lock
call
LockResource
;- API Function cmp
eax,0h
;
je
ErrorPrg
;
mov
pointerResource,eax
;pointer to the first byte of the resource
;==============================================================================
; API "CreateFileA" creates or opens a file, returns a handle to access object.
;-----------------------------------------------------------------------------push
0h
;hTemplateFile,
push
80h
;dwFlagsAndAttributes, 80h=normal
push
2h
;dwCreationDistribution, CREATE_ALWAYS
push
0h
;lpSecurityAttributes,
push
0h
;dwShareMode,
push
40000000h
;dwDesiredAccess,GENERIC_WRITE
push
OFFSET FileName
;lpFileName,pointer to filename
call
CreateFileA
;- API Function Page 3

td_win32asm_205.asm
cmp
eax,-1
;error ? INVALID_HANDLE_VALUE = -1
je
ErrorPrg
;
mov
handleFile,eax
;store handle in variable
;-----------------------------------------------------------------------------; API "WriteFile" writes data to a file
;-----------------------------------------------------------------------------push
0h
;lpOverlapped, structure overlapped I/O
push
OFFSET returnFile
;lpNumberOfBytesWritten,
push
sizeResource
;nNumberOfBytesToWrite, bytes to write
push
pointerResource
;lpBuffer, address data write to file
push
handleFile
;hFile, handle of file to write to
call
WriteFile
;- API Function cmp
eax,0h
;check for error
je
ErrorPrg
;
;-----------------------------------------------------------------------------; API "CloseHandle" closes an open object handle.
;-----------------------------------------------------------------------------push
handleFile
;hObject, handle of object to close
call
CloseHandle
;- API Function cmp
eax,0h
;check for error
je
ErrorPrg
;
;==============================================================================
; API "mciSendCommandA" here opens the device
;-----------------------------------------------------------------------------mov
open_lpstrDeviceType,0h
;fill MCI_OPEN_PARMS structure
mov
open_lpstrElementName,OFFSET FileName
;
push
OFFSET open_dwCallback
;dwParam, MCI_OPEN_PARMS struc.
push
0200h
;fdwCommand, MCI_OPEN_ELEMENT
push
0803h
;uMsg, command msg., MCI_OPEN
push
0h
;IDDevice, here not used
call
mciSendCommandA
;- API Function cmp
eax,0h
;check for error
jne
ErrorPrg
;
;-----------------------------------------------------------------------------; API "mciSendCommandA", MCI_PLAY command begins transmitting output data.
;-----------------------------------------------------------------------------push
OFFSET play_dwCallback
;dwParam, MCI_PLAY_PARMS struc.
push
0h
;fdwCommand,
push
0806h
;uMsg, command message, MCI_PLAY
push
open_wDeviceID
;IDDevice, given from MCI_OPEN
call
mciSendCommandA
;- API Function cmp
eax,0h
;check for error
jne
ErrorPrg
;
;-----------------------------------------------------------------------------; API "MessageBoxA" creates a message box.
;-----------------------------------------------------------------------------push
0h
;uType, style, 0=MB_OK Button
push
OFFSET MB2Titel
;lpCaption,pointer to title text
push
OFFSET MB2Text
;lpText,pointer to text message box
push
0h
;handle of owner window 0=no owner
call
MessageBoxA
;- API Function
;-----------------------------------------------------------------------------Page 4

td_win32asm_205.asm
; API "mciSendCommandA" here closes the device
;-----------------------------------------------------------------------------push
OFFSET generic_dwCallback
;dwParam, MCI_GENERIC_PARMS structure
push
0h
;fdwCommand,
push
804h
;uMsg, command message, MCI_CLOSE
push
open_wDeviceID
;IDDevice, given from MCI_OPEN
call
mciSendCommandA
;- API Function cmp
eax,0h
;check for error
jne
ErrorPrg
;
;==============================================================================
; Next we terminate our program (API=ExitProcess)
;-----------------------------------------------------------------------------ExitPrg:
push
hInstance
;push our programm handle to exit
call
ExitProcess
;- API Function ErrorPrg:
;==============================================================================
; API "MessageBoxA" creates a message box.
;-----------------------------------------------------------------------------push
0h
;uType, style, 0=MB_OK Button
push
OFFSET MB1Titel
;lpCaption,pointer to title text
push
OFFSET MB1Text
;lpText,pointer to text message box
push
0h
;handle of owner window 0=no owner
call
MessageBoxA
;- API Function jmp
ExitPrg
;==============================================================================
; end Main = end of our program code
;-----------------------------------------------------------------------------end Main
;end of our program code, entry point
;==============================================================================
; To create the exe file use this commands with your Microsoft Assembler/Linker
;-----------------------------------------------------------------------------; ml.exe /c /coff td_win32asm_205.asm
;asm command
; rc.exe /v rsrc.rc
;rc command
; cvtres.exe /machine:ix86 rsrc.res
; link.exe /subsystem:windows td_win32asm_205.obj rsrc.obj
;link command
;==============================================================================

Page 5

Vous aimerez peut-être aussi