Preview of a file

User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Preview of a file

Post by Silvio.Falconi »

Can I show the preview of a file with Image class ?
sample I have namedoc.doc or noname.pdf can I show the preview on dialog ?
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
AntoninoP
Posts: 347
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: Preview of a file

Post by AntoninoP »

Hello,
It is not an easy task, I quickly made this example of use of IPreviewHandler. But It looks that work only with pdf file :oops:

Code: Select all

#include <fivewin.ch>

memvar oPreview

function main()

   local oDlg
   public oPreview
   // IMPORTANT!!
   CoInitialize()
   
   DEFINE WINDOW oDlg FROM  0,0 TO 650,650 PIXEL TITLE "Test"
   oPreview := AddPreview( oDlg:hWnd, {10,10,610,610}, "C:\Users\aperricone\Downloads\grid.pdf")
   ACTIVATE WINDOW oDlg CENTERED

return nil


#pragma BEGINDUMP
#define CINTERFACE
#define COBJMACROS
#include <windows.h>
#include <hbapi.h>
#include <CommCtrl.h>
#include <ShObjIdl.h>
#include <Shlwapi.h>
#include <ShlGuid.h>

LPWSTR     AnsiToWide( LPSTR cAnsi );

IPreviewHandler* GetIPreviewHandler(const wchar_t* cExtension)
{
   CLSID cls;
   wchar_t clsStr[250];
   wchar_t cKey[250];
   LONG nLen=250;
   IPreviewHandler* pRet=0;
   HRESULT hr;
   
   if(cExtension == 0 ) return 0;
   wcscpy_s(cKey,250, cExtension);
   wcscat_s(cKey,250, L"\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
   if( ERROR_SUCCESS != RegQueryValueW(HKEY_CLASSES_ROOT, cKey, clsStr, &nLen) )
   {
      RegQueryValueW(HKEY_CLASSES_ROOT, cExtension, cKey, &nLen);
      wcscat_s(cKey,250, L"\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
      MessageBoxW(0,cKey,cExtension,MB_OK);
      if( ERROR_SUCCESS != RegQueryValueW(HKEY_CLASSES_ROOT, cKey, clsStr, &nLen) )
         return 0;
   }
      
   CLSIDFromString( clsStr,&cls);
    
#if defined(__cplusplus)
   if((hr = CoCreateInstance( cls, 0, CLSCTX_ALL, IID_IPreviewHandler, ( void ** ) &pRet ) )==S_OK)
#else
   if((hr = CoCreateInstance( &cls, 0, CLSCTX_ALL, &IID_IPreviewHandler, ( void ** ) &pRet ) )==S_OK)
#endif
   {
      return pRet;
   }
   swprintf(cKey,250,L"%i",hr);
   MessageBoxW(0,cKey,cExtension,MB_OK);
  return 0;
}
HB_FUNC( ADDPREVIEW ) // hWnd, {nTop, nLeft, nBottom, nRight}, cFileName
{
#ifndef _WIN64
   HWND hWnd = ( HWND ) hb_parnl( 1 );
   #define RET hb_retnl( (long) pPreview );
#else   
   HWND hWnd = ( HWND ) hb_parnll( 1 );
   #define RET hb_retnll( (long long) pPreview );
#endif   
   RECT rectPreview;
   LPWSTR cFileName = AnsiToWide(( char * ) hb_parc( 3 ));
   LPCWSTR cExtension = wcsrchr(cFileName,L'.');
   IPreviewHandler* pPreview = GetIPreviewHandler(cExtension);
   IInitializeWithFile* pFile;
   IInitializeWithStream* pIStream;
   
   if(pPreview==0)
   {
      MessageBoxW(0,L"no pPreview",cExtension,MB_OK);
      RET
      return;
   }
   IPreviewHandler_QueryInterface(pPreview, &IID_IInitializeWithFile,(void**)&pFile );
   IPreviewHandler_QueryInterface(pPreview, &IID_IInitializeWithStream,(void**)&pIStream );
   
   if(pFile==0 && pIStream==0)
   {
      MessageBoxW(0,L"no pfile no pStream",cExtension,MB_OK);
      IPreviewHandler_Unload(pPreview);
      pPreview = 0;
      RET
      return;
   }
   if(pFile!=0)
   {
      IInitializeWithFile_Initialize(pFile, cFileName, STGM_READ);
   } else
   { //pIStream!=0
      HANDLE hFile = CreateFileW(cFileName,FILE_READ_DATA,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL );
     if( INVALID_HANDLE_VALUE != hFile )
     {
         DWORD dwSize = GetFileSize( hFile, NULL );
         HGLOBAL hGlobal= GlobalAlloc(GPTR, dwSize );
         BYTE * pByte = (BYTE *)GlobalLock(hGlobal);

         if( pByte )
         {
            LPSTREAM pStream;
            ReadFile(hFile,pByte,dwSize,&dwSize,NULL);  
            GlobalUnlock(hGlobal);

             CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);    
             IInitializeWithStream_Initialize(pIStream, pStream, STGM_READ);
         }

         CloseHandle( hFile );
     }
   }
   hb_xfree( cFileName );
   
   rectPreview.top    = hb_parvni(2,1);
   rectPreview.left   = hb_parvni(2,2);
   rectPreview.bottom = hb_parvni(2,3);
   rectPreview.right  = hb_parvni(2,4);
     
   IPreviewHandler_SetWindow( pPreview, hWnd, &rectPreview );
   IPreviewHandler_DoPreview( pPreview );

   RET
}

HB_FUNC( UnloadPreview ) //preview
{
#ifndef _WIN64
   IPreviewHandler* pPreview = ( IPreviewHandler* ) hb_parnl( 1 );
#else   
   IPreviewHandler* pPreview = ( IPreviewHandler* ) hb_parnll( 1 );
#endif
   if( pPreview )
   {
      IPreviewHandler_Unload(pPreview);
   }
}

#pragma ENDDUMP
Regards,
Antonino
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Preview of a file

Post by Silvio.Falconi »

error in compilation

line 31
IPreviewHandler* GetIPreviewHandler(const wchar_t* cExtension)


Progetto: test, Ambiente: xFive_Pelles:
[1]:Harbour.Exe test.prg /m /n0 /gc1 /es2 /iC:\Work\fwh\include /ic:\work\xHarbour\Include /jC:\Work\Errori\PREVIE~1\I18n\Main.hil /iinclude;c:\work\fwh\include;c:\work\xHarbour\include /oObj\test.c
xHarbour 1.2.3 Intl. (SimpLex) (Build 20140725)
Copyright 1999-2014, http://www.xharbour.org http://www.harbour-project.org/
Compiling 'test.prg'...
Generating international list to 'C:\Work\Errori\PREVIE~1\I18n\Main.hil'...
Generating C source output to 'Obj\test.c'...
Done.
Lines 18, Functions/Procedures 1, pCodes 100
[1]:Bcc32.Exe -M -c -O2 -tW -v- -X -DHB_FM_STATISTICS_OFF -DHB_NO_DEFAULT_API_MACROS -DHB_NO_DEFAULT_STACK_MACROS -DHB_OS_WIN_32 -IC:\Work\fwh\include -IC:\Work\bcc582\Include;c:\work\xHarbour\Include -nC:\Work\Errori\PREVIE~1\Obj test.c
Borland C++ 5.82 for Win32 Copyright (c) 1993, 2005 Borland
test.c:
Error E2141 test.prg 31: Declaration syntax error
*** 1 errors in Compile ***
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Preview of a file

Post by Silvio.Falconi »

Antonio,
any solution with borland bc582 ?
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
mastintin
Posts: 1502
Joined: Thu May 27, 2010 2:06 pm

Re: Preview of a file

Post by mastintin »

Silvio.Falconi wrote:Antonio,
any solution with borland bc582 ?
in Bcc693 is ok.
Silvio in bcc582 is not posible :-(
IPreviewHandler declaration is in ShObjodl.h header file in bcc693.
In bcc582 ShObjodl.h header not exist IPreviewHandler declaration .
AntoninoP
Posts: 347
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: Preview of a file

Post by AntoninoP »

User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Preview of a file

Post by nageswaragunupudi »

working with bcc7.
I have a slightly different code but we need bcc7 for handling the latest interfaces.
Regards

G. N. Rao.
Hyderabad, India
User avatar
mastintin
Posts: 1502
Joined: Thu May 27, 2010 2:06 pm

Re: Preview of a file

Post by mastintin »

I spent part of a c ++ code and it works with xls and doc files. :-)
When you have finished public code
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Preview of a file

Post by nageswaragunupudi »

mastintin wrote:I spent part of a c ++ code and it works with xls and doc files. :-)
When you have finished public code
Yes, for me too.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Preview of a file

Post by nageswaragunupudi »

Silvio.Falconi wrote:Can I show the preview of a file with Image class ?
sample I have namedoc.doc or noname.pdf can I show the preview on dialog ?
As of now, ximage can display previews of docx, xlsx and pptx if they are saved with thumbnails.
Regards

G. N. Rao.
Hyderabad, India
User avatar
mastintin
Posts: 1502
Joined: Thu May 27, 2010 2:06 pm

Re: Preview of a file

Post by mastintin »

the code for IPreviewHandler ...
It has a big problem that I can not solve ,suggestions are welcome.
if we load Excel or Word documents, the process is not killed and excel process is in memory .
I've tried several ways but does not work. not dispose excel process. :-(

c++ code ...

Code: Select all


#include <hbapi.h>
#include <windows.h>
#include <shobjidl.h>

HB_FUNC( CREATEHPREVIEW ) //preview
{

#ifndef _WIN64
  #define RET hb_retnl( (long) pPreview );
#else   
  #define RET hb_retnll( (long long) pPreview );
#endif   
 LPWSTR cFileName = UTF8toUTF16( hb_parc( 1 ) );
  LPCWSTR cExtension = wcsrchr(cFileName,L'.');
 
 CLSID cls;
   wchar_t clsStr[250];
   wchar_t cKey[250];
   LONG nLen=250;
   IPreviewHandler* pPreview=0;
   HRESULT hr;
         
   if(cExtension == 0 ) {
      pPreview = 0;
      RET
      return;
   }
   wcscpy_s(cKey,250, cExtension);
   wcscat_s(cKey,250, L"\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
   if( ERROR_SUCCESS != RegQueryValueW(HKEY_CLASSES_ROOT, cKey, clsStr, &nLen) )
   {
      RegQueryValueW(HKEY_CLASSES_ROOT, cExtension, cKey, &nLen);
      wcscat_s(cKey,250, L"\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
      // MessageBoxW(0,cKey,cExtension,MB_OK);
      if( ERROR_SUCCESS != RegQueryValueW(HKEY_CLASSES_ROOT, cKey, clsStr, &nLen) )
      {
       pPreview = 0;
       RET
       return;
      }
    }
    
    CLSIDFromString( clsStr,&cls);
    
   if( S_OK == CoCreateInstance(cls, NULL, CLSCTX_INPROC_SERVER |CLSCTX_LOCAL_SERVER, __uuidof(IPreviewHandler),(LPVOID*)&pPreview) )
   {
      RET 
      return;      
   }
   
   MessageBoxW(0,cKey,cExtension,MB_OK);
   pPreview = 0;
   RET
return;
}


HB_FUNC( ADDPREVIEWH ) // hWnd, {nTop, nLeft, nBottom, nRight}, cFileName, pPreview
{
#ifndef _WIN64
   HWND hWnd = ( HWND ) hb_parnl( 1 );
    IPreviewHandler* pPreview = ( IPreviewHandler* ) hb_parnl( 4 );  
#else   
   HWND hWnd = ( HWND ) hb_parnll( 1 );   
   IPreviewHandler* pPreview = ( IPreviewHandler* ) hb_parnll( 4 );
#endif   

   RECT rectPreview;
 
   LPWSTR cFileName = UTF8toUTF16( hb_parc( 3 ) );
   LPCWSTR cExtension = wcsrchr(cFileName,L'.');

   IInitializeWithFile* pFile;
   IInitializeWithStream* pIStream;
   
   if(pPreview==0)
   {
      MessageBoxW(0,L"no pPreview",cExtension,MB_OK);
      RET
      return;
   }

   pPreview->QueryInterface(__uuidof( IInitializeWithFile ), (LPVOID*)&pFile ); 
   pPreview->QueryInterface(__uuidof( IInitializeWithStream ), (LPVOID*)&pIStream ); 
      
   if(pFile==0 && pIStream==0)
   {
      MessageBoxW(0,L"no pfile no pStream",cExtension,MB_OK);
      pPreview->Unload();
      pPreview = 0;
      RET
      return;
   }
   if(pFile!=0)
   {
      pFile->Initialize( cFileName, STGM_READ);
   } else
   { //pIStream!=0
      HANDLE hFile = CreateFileW(cFileName,FILE_READ_DATA,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL );
     if( INVALID_HANDLE_VALUE != hFile )
     {
         DWORD dwSize = GetFileSize( hFile, NULL );
         HGLOBAL hGlobal= GlobalAlloc(GPTR, dwSize );
         BYTE * pByte = (BYTE *)GlobalLock(hGlobal);

         if( pByte )
         {
            LPSTREAM pStream;
            ReadFile(hFile,pByte,dwSize,&dwSize,NULL);  
            GlobalUnlock(hGlobal);

             CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);    
             pIStream->Initialize( pStream, STGM_READ);
         }

         CloseHandle( hFile );
     }
   }
   hb_xfree( cFileName );
   
   rectPreview.top    = hb_parvni(2,1);
   rectPreview.left   = hb_parvni(2,2);
   rectPreview.bottom = hb_parvni(2,3);
   rectPreview.right  = hb_parvni(2,4);
     
   pPreview->SetWindow( hWnd, &rectPreview );
   pPreview->DoPreview();
   
 //  pPreview->Unload();
   delete pIStream ;
    
/*
#ifndef _WIN64
  hb_retnl( (long) pIStream );
#else   
  hb_retnll( (long long) pIStream );
#endif   
  */ 
}

HB_FUNC( UNLOADHPREVIEW ) //preview
{
#ifndef _WIN64
   IPreviewHandler* pPreview = ( IPreviewHandler* ) hb_parnl( 1 );
  #else   
   IPreviewHandler* pPreview = ( IPreviewHandler* ) hb_parnll( 1 );
  #endif
   pPreview->Unload();
       
 }

 
The sample ...

Code: Select all

#include <fivewin.ch>

memvar oPreview

function main()

   local oDlg
   local hStream
   local cFile
   
   public oPreview
   // IMPORTANT!!
   cFile:= cGetfile("coge","*.*")
   if !empty( cfile )   
     CoInitialize()
  
   DEFINE WINDOW oDlg FROM  0,0 TO 650,650 PIXEL TITLE "Test"
   
   
       oPreview := CreateHPreview( cFile )
       AddPreviewH( oDlg:hWnd, {10,10,610,610}, cFile , oPreview )
   
      
   ACTIVATE WINDOW oDlg CENTERED
   
   
 endif     

return nil
 
AntoninoP
Posts: 347
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: Preview of a file

Post by AntoninoP »

Warning, you use C++ interface, might not compile in C mode!
you should add (i forgot) after

Code: Select all

pPreview->Unload();
the

Code: Select all

pPreview->Release();
Necessary to destroy all COM object.

As we already said in http://forums.fivetechsupport.com/viewt ... =3&t=31014
you can remove all #ifdef WIN64 using hb_parptr and hb_retptr, using the pointer vartype that is an Harbour extension.
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Preview of a file

Post by nageswaragunupudi »

Glad you made it working for bcc582
Is it working for you for pdf (most important) and others like image files, text files, etc?

Excel process. I think we can leave it. If you insist on closing it ...

First open excel with ole and also ascertain if we started it and it was started earlier by the user.
After this program is over, if we started excel, then quit the app with ole otherwise leave it (user was using it before)
Regards

G. N. Rao.
Hyderabad, India
User avatar
mastintin
Posts: 1502
Joined: Thu May 27, 2010 2:06 pm

Re: Preview of a file

Post by mastintin »

Nages, bcc582 is not supported.
txt, bat file not work properly yet :-( ( I've looked )
AntoninoP , thank you very much for your remarks :-) . I've placed the code in gdiplus.cpp for now
User avatar
mastintin
Posts: 1502
Joined: Thu May 27, 2010 2:06 pm

Re: Preview of a file

Post by mastintin »

Bcc582 compiled and pdf work :-)
I had to make some changes in the code to compile in bcc582 .
I added the definitions missing in shobjidl.h header file in an extra h file .... Shextra.h
Post Reply