Bool loadmydomainfile (cstring fname, lppicture * lppi)
{
Handle hfile = createfile (fname, generic_read, 0, null, open_existing, 0, null );
If (hfile = invalid_handle_value)
{
Cstring STR;
Str. Format (_ T ("% s cannot be opened"), fname );
MessageBox (STR );
Return false;
}
// Obtain the file size
DWORD dwfilesize = getfilesize (hfile, null );
If (DWORD)-1 = dwfilesize)
{
Closehandle (hfile );
MessageBox (_ T ("image file is empty "));
Return false;
}
// Read image files
Lpvoid pvdata;
// Allocate memory by file size
Hglobal = globalalloc (gmem_moveable, dwfilesize );
If (null = hglobal)
{
Closehandle (hfile );
MessageBox (_ T ("insufficient memory, insufficient memory allocation "));
Return false;
}
Pvdata = globallock (hglobal );
If (null = pvdata)
{
Globalunlock (hglobal );
Closehandle (hfile );
MessageBox (_ T ("memory cannot be locked "));
Return false;
}
DWORD dwfileread = 0;
Bool bread = readfile (hfile, pvdata, dwfilesize, & dwfileread, null );
Globalunlock (hglobal );
Closehandle (hfile );
If (false = bread)
{
MessageBox (_ T ("file read error "));
Return false;
}
Lpstream PSTM = NULL;
// Generate an istream stream from the allocated memory
Hresult hR = createstreamonhglobal (hglobal, true, & PSTM );
If (! Succeeded (HR ))
{
MessageBox (_ T ("failed to generate stream operation "));
If (PSTM! = NULL)
PSTM-> release ();
Return false;
}
Else if (PSTM = NULL)
{
MessageBox (_ T ("failed to generate stream operation "));
Return false;
}
If (! * Lppi)
(* Lppi)-> release ();
HR = oleloadpicture (PSTM, dwfilesize, false, iid_ipicture, (lpvoid *) & (* lppi ));
PSTM-> release ();
If (! Succeeded (HR ))
{
MessageBox (_ T ("loading failed "));
Return false;
}
Else if (* lppi = NULL)
{
MessageBox (_ T ("loading failed "));
Return false;
}
Return true;
}