nickname
2008-01-20 18:21:00 UTC
I have found some sample code for how to do this on the site:
http://blogs.microsoft.co.il/blogs/eyal/
This seems to work for the Word preview handler but the adobe one fails to
load with exception:
"Creating an instance of the COM component with CLSID
{DC6EFB56-9CFA-464D-8880-44885D7DC193} from the IClassFactory failed due to
the following error: 80040154."
Could somebody explain why this is?
Source Code Below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices.ComTypes;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
//http://blogs.microsoft.co.il/blogs/eyal/
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public Rectangle ToRectangle() { return Rectangle.FromLTRB(left,
top, right, bottom); }
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("fc4801a3-2ba9-11cf-a229-00aa003d7352")]
public interface IObjectWithSite
{
void SetSite([In, MarshalAs(UnmanagedType.IUnknown)] object pUnkSite);
void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out
object ppvSite);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("8895b1c6-b41f-4c1c-a562-0d564250836f")]
interface IPreviewHandler
{
void SetWindow(IntPtr hwnd, ref RECT rect);
void SetRect(ref RECT rect);
void DoPreview();
void Unload();
void SetFocus();
void QueryFocus(out IntPtr phwnd);
[PreserveSig]
uint TranslateAccelerator(ref MSG pmsg);
}
[StructLayout(LayoutKind.Sequential)]
internal struct MSG
{
public IntPtr hwnd;
public int message;
public IntPtr wParam;
public IntPtr lParam;
public int time;
public int pt_x;
public int pt_y;
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b7d14566-0509-4cce-a71f-0a554233bd9b")]
interface IInitializeWithFile
{
void Initialize([MarshalAs(UnmanagedType.LPWStr)] string
pszFilePath, uint grfMode);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b824b49d-22ac-4161-ac8a-9916e8fa3f7f")]
interface IInitializeWithStream
{
void Initialize(IStream pstream, uint grfMode);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void PreView(Guid guid, string file)
{
//get type by clsid
Type previewerType = Type.GetTypeFromCLSID(guid);
//create instance
object instance = Activator.CreateInstance(previewerType);
//now we need to querry interface for the required interfaces.
IObjectWithSite ows = (IObjectWithSite)instance;
IPreviewHandler ph = (IPreviewHandler)instance;
IInitializeWithFile iwf = instance as IInitializeWithFile;
IInitializeWithStream iws = instance as IInitializeWithStream;
//now we need to initialize the object
if (iwf != null)
{
iwf.Initialize(file, 0);
}
else
{
if (iws != null)
{
StreamWrapper stream = new StreamWrapper(File.Open(file,
FileMode.Open));
iws.Initialize(stream, 0);
}
}
//set the current form (implements IPreviewHandlerFrame)
ows.SetSite(this);
//set size
RECT rect = new RECT();
rect.left = 0;
rect.top = 0;
rect.bottom = this.Height;
rect.right = this.Width;
ph.SetWindow(this.Handle, ref rect);
ph.SetRect(ref rect);
//perform the preview
ph.DoPreview();
ph.SetFocus();
}
private void Form1_Load(object sender, EventArgs e)
{
//Word (Works Fine)
//PreView(new Guid("{84F66100-FF7C-4fb4-B0C0-02CD7FB668FE}"),
"c:\\test.docx");
//PDF (Does not work)
PreView(new Guid("{DC6EFB56-9CFA-464D-8880-44885D7DC193}"),
"c:\\test.pdf");
}
}
}
internal class StreamWrapper : IStream
{
~StreamWrapper()
{
if (stream != null)
{
stream.Close();
stream.Dispose();
stream = null;
}
}
Stream stream = null;
public StreamWrapper(Stream stream)
{
this.stream = stream;
}
#region IStream Members
public void Clone(out IStream ppstm)
{
ppstm = null;
}
public void Commit(int grfCommitFlags)
{
}
public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr
pcbWritten)
{
}
public void LockRegion(long libOffset, long cb, int dwLockType)
{
}
public void Read(byte[] pv, int cb, IntPtr pcbRead)
{
int actual = stream.Read(pv, 0, cb);
Marshal.WriteInt32(pcbRead, actual);
}
public void Revert()
{
}
public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
{
long actual = stream.Seek(dlibMove, (SeekOrigin)dwOrigin);
}
public void SetSize(long libNewSize)
{
}
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG
pstatstg, int grfStatFlag)
{
pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG();
}
public void UnlockRegion(long libOffset, long cb, int dwLockType)
{
}
public void Write(byte[] pv, int cb, IntPtr pcbWritten)
{
}
#endregion
}
http://blogs.microsoft.co.il/blogs/eyal/
This seems to work for the Word preview handler but the adobe one fails to
load with exception:
"Creating an instance of the COM component with CLSID
{DC6EFB56-9CFA-464D-8880-44885D7DC193} from the IClassFactory failed due to
the following error: 80040154."
Could somebody explain why this is?
Source Code Below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices.ComTypes;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
//http://blogs.microsoft.co.il/blogs/eyal/
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public Rectangle ToRectangle() { return Rectangle.FromLTRB(left,
top, right, bottom); }
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("fc4801a3-2ba9-11cf-a229-00aa003d7352")]
public interface IObjectWithSite
{
void SetSite([In, MarshalAs(UnmanagedType.IUnknown)] object pUnkSite);
void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out
object ppvSite);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("8895b1c6-b41f-4c1c-a562-0d564250836f")]
interface IPreviewHandler
{
void SetWindow(IntPtr hwnd, ref RECT rect);
void SetRect(ref RECT rect);
void DoPreview();
void Unload();
void SetFocus();
void QueryFocus(out IntPtr phwnd);
[PreserveSig]
uint TranslateAccelerator(ref MSG pmsg);
}
[StructLayout(LayoutKind.Sequential)]
internal struct MSG
{
public IntPtr hwnd;
public int message;
public IntPtr wParam;
public IntPtr lParam;
public int time;
public int pt_x;
public int pt_y;
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b7d14566-0509-4cce-a71f-0a554233bd9b")]
interface IInitializeWithFile
{
void Initialize([MarshalAs(UnmanagedType.LPWStr)] string
pszFilePath, uint grfMode);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b824b49d-22ac-4161-ac8a-9916e8fa3f7f")]
interface IInitializeWithStream
{
void Initialize(IStream pstream, uint grfMode);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void PreView(Guid guid, string file)
{
//get type by clsid
Type previewerType = Type.GetTypeFromCLSID(guid);
//create instance
object instance = Activator.CreateInstance(previewerType);
//now we need to querry interface for the required interfaces.
IObjectWithSite ows = (IObjectWithSite)instance;
IPreviewHandler ph = (IPreviewHandler)instance;
IInitializeWithFile iwf = instance as IInitializeWithFile;
IInitializeWithStream iws = instance as IInitializeWithStream;
//now we need to initialize the object
if (iwf != null)
{
iwf.Initialize(file, 0);
}
else
{
if (iws != null)
{
StreamWrapper stream = new StreamWrapper(File.Open(file,
FileMode.Open));
iws.Initialize(stream, 0);
}
}
//set the current form (implements IPreviewHandlerFrame)
ows.SetSite(this);
//set size
RECT rect = new RECT();
rect.left = 0;
rect.top = 0;
rect.bottom = this.Height;
rect.right = this.Width;
ph.SetWindow(this.Handle, ref rect);
ph.SetRect(ref rect);
//perform the preview
ph.DoPreview();
ph.SetFocus();
}
private void Form1_Load(object sender, EventArgs e)
{
//Word (Works Fine)
//PreView(new Guid("{84F66100-FF7C-4fb4-B0C0-02CD7FB668FE}"),
"c:\\test.docx");
//PDF (Does not work)
PreView(new Guid("{DC6EFB56-9CFA-464D-8880-44885D7DC193}"),
"c:\\test.pdf");
}
}
}
internal class StreamWrapper : IStream
{
~StreamWrapper()
{
if (stream != null)
{
stream.Close();
stream.Dispose();
stream = null;
}
}
Stream stream = null;
public StreamWrapper(Stream stream)
{
this.stream = stream;
}
#region IStream Members
public void Clone(out IStream ppstm)
{
ppstm = null;
}
public void Commit(int grfCommitFlags)
{
}
public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr
pcbWritten)
{
}
public void LockRegion(long libOffset, long cb, int dwLockType)
{
}
public void Read(byte[] pv, int cb, IntPtr pcbRead)
{
int actual = stream.Read(pv, 0, cb);
Marshal.WriteInt32(pcbRead, actual);
}
public void Revert()
{
}
public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
{
long actual = stream.Seek(dlibMove, (SeekOrigin)dwOrigin);
}
public void SetSize(long libNewSize)
{
}
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG
pstatstg, int grfStatFlag)
{
pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG();
}
public void UnlockRegion(long libOffset, long cb, int dwLockType)
{
}
public void Write(byte[] pv, int cb, IntPtr pcbWritten)
{
}
#endregion
}