Flash Player: Loading from memory

user cucao wants to know how to play a flash file from application resource. The user wants to use a dongle to protect unauthorized viewing of the flash file so can’t just put the swf file in plain sight.

Adobe’s Flash Player, being an ActiveX with supports for many OLE containers such as Internet Explorer and Microsoft Word, implements many standard OLE interfaces (for details open the ActiveX in oleview). One of them is IPersistStreamInit, a standard interface for ActiveX controls who want to distinguish new page load vs refresh, but can be used for the user to load the swf file from memory.

The data source passed into IPersistStreamInit::Load could be a home-brew COM server that implements IStream, but since Windows already has such an implementation (CreateStreamOnHGlobal), it saves a lot of work to just load the resource into global memory (GlobalAlloc/GlobalLock/memcpy/GlobalUnlock), and creates a stream off there for loading.

//MFC and ATL instead of Windows API for readability

void LoadFlash(LPUNKNOWN player, LPCVOID data, UINT size) {
COleStreamFile streamFile; if(!streamFile.CreateMemoryStream() return;
CArchive ar (&streamFile, CArchive::store );
ar.Write(data,size); ar.Close();
CComQIPtr<IPersistStreamInit> playerStreamLoader(player);
if(playerStreamLoader==NULL) return;

This only works for self-contained swf files, however. To set a base url for relative url search in action scripts, a home-brew COM server that implements both IStream and IMoniker is needed.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.