Using Memory Sources (Visual FoxPro 6.0)

The ltmmConvertCtrl and ltmmPlayCtrl objects allow the user to supply media data through memory instead of a file. The source can be defined by a global memory handle or by an array.

1.

Start Visual FoxPro 6.

2.

If you didn’t install LEAD Multi-Media controls before, install them.

On the Tools pull-down menu, choose Options… and go to the Controls tab. Click the ActiveX controls radio button. Mark the ltmmPlayCtrl Class and click the Set As Default button, then click OK.

3.

Create a new blank form. On the Form controls toolbar, press the View Classes button and select ActiveX controls. Add the ltmmPlayCtrl to your form and change its name to ltmmPlayCtrl1. Size and position the control as you want it to appear at run time.

4.

Add a command button to your form, and name it as follows.

 

Name

Caption

 

btnSetSourceHGlobal

Set SourceHGlobal Property

5.

Code the Form Destroy procedure as follows:

Declare Integer GlobalFree In kernel32 Integer hMem
&& Get the currrent SourceHGlobal property value. 
SourceHGlobal = ThisForm.ltmmPlayCtrl1.SourceHGlobal
&& Check if resources are already allocated. 
If SourceHGlobal <> 0 Then
   ThisForm.ltmmPlayCtrl1.ResetSource()
   GlobalFree(SourceHGlobal) 
EndIf

6.

Code the btnSetSourceHGlobal Click procedure as follows.

#define GMEM_MOVEABLE 2
Declare Integer GlobalAlloc In kernel32 Integer wFlags, Integer dwBytes
Declare Integer GlobalFree In kernel32 Integer hMem
Declare Integer GlobalLock In kernel32 Integer hMem
Declare Integer GlobalUnlock In kernel32 Integer hMem
Declare RtlMoveMemory In kernel32 As StrToMem Integer pMem, String @ FoxStr, Integer nBytes

&& Get the currrent SourceHGlobal property value. 
SourceHGlobal = ThisForm.ltmmPlayCtrl1.SourceHGlobal
&& Check if resources are already allocated. 
If SourceHGlobal <> 0 Then
   && Reset the resources to free it. 
   ThisForm.ltmmPlayCtrl1.ResetSource()
   && Free the resource. 
   GlobalFree(SourceHGlobal) 
   && assign it to 0. 
   ThisForm.ltmmPlayCtrl1.SourceHGlobal = -1
EndIf

hf = FOpen("C:\Source.avi")
If hf = -1 Then
   MessageBox("Error opening file!") 
   Return
EndIf

&& Get the total file size
Set Compatible On
uTotalFileSize = FSize("C:\Source.avi")

&& allocate same-sized global memory
SourceHGlobal = GlobalAlloc(GMEM_MOVEABLE, uTotalFileSize) 
&& There is not enough memory!. 
If SourceHGlobal = -1 Then
   FClose(hf) 
   MessageBox("Not enough memory to hold the file in memory!") 
   Return
EndIf

&& lock our global data. 
lpMem = GlobalLock(SourceHGlobal) 
If lpMem = 0 Then
   FClose(hf) 
   GlobalFree(SourceHGlobal) 
   MessageBox("Not enough memory to hold the file in memory!") 
   Return
EndIf


MemFile = FRead(hf, uTotalFileSize) 
If Len(MemFile) < uTotalFileSize Then
   FClose(hf) 
   GlobalUnlock(SourceHGlobal) 
   GlobalFree(SourceHGlobal) 
   MessageBox("Error reading file")
   Return
EndIf

StrToMem(lpMem, @MemFile, uTotalFileSize) 
&& close file
FClose(hf) 
&& Unlock our global data. 
GlobalUnlock(SourceHGlobal) 

&& Assign the SourceHGlobal Property to file data!. 
ThisForm.ltmmPlayCtrl1.SourceHGlobal = SourceHGlobal
&& Run this file
ThisForm.ltmmPlayCtrl1.Run

7.

Run your program to test it.