Figure 1 FASTBOOT.CPP
#include <windows.h>
#include <ole2.h>
DEFINE_GUID( Fastboot_CLSID, 0xFEF72D70, 0x6DEB, 0x11CE,
0xaa, 0x0e, 0x00, 0x80, 0x29, 0x00, 0x3c, 0x66 );
WinMain( HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow )
{
HWND hWndDragDrop;
DWORD dwRegister; // Token returned by RegisterClassObject [ESP+1Ch]
DWORD dwRegister2; // Token returned by IRunningObjectTable::Register
LPSTORAGE pIStorage;
LPSTREAM pIStream;
LPMONIKER pIMoniker;
LPRUNNINGOBJECTTABLE pIRunningObjectTable;
LPDROPTARGET pIDropTarget;
HRESULTS hResult;
if ( 1 != RegisterClassAndCreateWindow( &hWndDragDrop ) )
exit( 1 );
hResult = OleInitialize( 0 );
if ( S_OK == hResult )
{
// Interesting hardcoding of the file name
hResult = StgCreateDocFile( TEXT("c:\temp\~oleapp.doc"),
STGM_TRANSACTED | STGM_CREATE |
STGM_SHARE_DENY_WRITE | STGM_READWRITE,
0, &pIStorage )
if ( S_OK == hResult )
{
hResult = pIStorage->CreateStream( TEXT("DATA"),
STGM_CREATE | STGM_SHARE_EXLUSIVE STGM_READWRITE,
0, 0, &pIStream );
if ( S_OK == hResult )
{
hResult = CreateFileMoniker( TEXT("c:\temp\~oleapp.doc"),
&pIMoniker );
if ( S_OK == hResult )
{
hResult = GetRunningObjectTable(0, &pIRunningObjectTable);
if ( S_OK == hResult )
{
pIDropTarget = 0;
pIDropTarget = new DropTarget;
hResult = IRunningObjectTable->Register(
0, pIDropTarget, pIMoniker, &dwRegister2);
if ( S_OK == hResult )
{
// Interesting how an IDropTarget is passed,
// rather than an IClassFactory
hResult = CoRegisterClassObject( Fastboot_CLSID,
pIDropTarget, CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE, &dwRegister );
if ( S_OK == hResult )
{
hResult = RegisterDragDrop( hWndDragDrop,
pIDropTarget );
if ( S_OK == hResult )
{
// Get rid of the IDropTarget
hResult = RevokeDragDrop( hWndDragDrop );
if ( S_OK != hResult )
exit( 1 );
}
if (S_OK != CoRevokeClassObject( dwRegister ))
exit( 1 );
}
hResult=pIRunningObjectTable->Revoke(dwRegister2);
if ( S_OK != hResult )
exit( 1 );
}
PIRunningObjectTable->Release();
}
pIMoniker->Release();
}
pIStream->Release();
}
pIStorage->Release();
}
OleUninitialize();
}
DestroyWindowAndUnregister( hWndDragDrop );
return 0;
}
//
// A very minimal IDropTarget interface implementation
//
IDropTarget::QueryInterface( REFIID refiid,
void** ppvObject )
{
*ppvObject = 0;
if ( !IsEqualIID( IID_IUnknown, refiid ) )
if ( !IsEqualIID( IID_IUnknown, refiid ) ) // Hey! Identical test!
return E_NOINTERFACE;
// If we get here, IID_IUnknown was asked for...
this->AddRef();
*ppvObject = this;
return S_OK;
}
IDropTarget::AddRef(void)
{
InterlockedIncrement( &this->m_cRef );
return this->m_cRef;
}
IDropTarget::Release(void)
{
if ( 0 == InterlockedDecrement( &this->m_cRef ) )
{
// If we got here, the reference count dropped to zero...
if ( this )
delete this;
return S_OK;
}
return this->m_cRef;
}
IDropTarget::DragEnter( IDataObject * pDataObject,
DWORD grfKeyState,
POINTL pt,
DWORD * pdwEffect )
{
*pdwEffect = 0;
return S_OK;
}
IDropTarget::DragOver( DWORD grfKeyState,
POINTL pt,
DWORD * pdwEffect )
{
pdwEffect = 0;
return S_OK;
}
IDropTarget::DragLeave(void)
{
return S_OK;
}
IDropTarget::Drop( IDataObject * pDataObject,
DWORD grfKeyState,
POINTL pt,
DWORD * pdwEffect )
{
pdwEffect = 0;
return E_FAIL;
}
//
// Functions to register/unregister the OleInit window class, and
// create/destroy a window of that class. Called by WinMain.
//
RegisterClassAndCreateWindow( HWND * pHWnd )
{
HMODULE hModule;
WNDCLASS wndClass;
HWND hWndRet;
hModule = GetModuleHandle( 0 );
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = 0;
wndClass.hIcon = 0;
wndClass.hCursor = 0;
wndClass.lpszMenuName = "OleInit";
wndClass.lpszClassName = "OleInit";
wndClass.style = CS_VREDRAW | CS_HREDRAW;
wndClass.lpfnWndProc = FastBootWndProc;
wndClass.hbrbackground = COLOR_WINDOW + 1; // that is, 6
if ( 0 == RegisterClass( &wndClass ) )
return 0;
hWndRet =
CreateWindowEx( 0, "OleInit", "OleInit", WS_OVERLAPPEDWINDOW,
0x80000000, 0, // X, Y
0x80000000, 0, // nWidth, nHeight
0, 0, hModule, 0 ); // parent, menu, hModule, lParam
if ( hWndRet )
pHWnd = hWndRet;
return hWndRet ? TRUE : FALSE;
}
DestroyWindowAndUnregister( HWND hWnd )
{
HMODULE hModule = GetModuleHandle( 0 );
DestroyWindow( hWnd );
UnregisterClass( "OleInit", hModule );
}
FastbootWndProc( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam )
{
switch ( uMsg )
{
default:
return DefWindowProc( hWnd, uMsg, wParam, lParam );
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_COMMAND:
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
}