---------
追記です。
教えて頂いた情報をもとにコードを修正しました。
それにより、ParseDisplayName()
、BindToObject()
がうまく動く様になったようです。しかし、GetUIObjectOf()
の最後の引数に結果が帰って来ず、nullのままです。
GetUIObjectOf()
の引数の取り方に誤りがあるでしょうか?もしくは何か不足があるでしょうか。
度々すみませんがおねがいいたしますm(__)m
☆Program.cs (未変更)
[STAThread]についてはそのままです。
static class Program
{
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new formMain());
}
}
☆メインフォーム
public partial class formMain : Form
{
public formMain()
{
InitializeComponent();
}
private void formMain_Load(object sender, EventArgs e)
{
pictureBox1.Image = Libs.Acrobat.AcrobatControl.GetThumbnailImage(@"D:Data\20150123-01.pdf");
}
☆IShellFolder定義と呼び出し
static Guid IID_IShellFolder = new Guid("{000214E6-0000-0000-C000-000000000046}");
[ComImportAttribute()]
[GuidAttribute("000214E6-0000-0000-C000-000000000046")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellFolder
{
void ParseDisplayName(IntPtr hwndOwner, IntPtr pbc, [MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName,
[In, Out] ref IntPtr pchEaten, out IntPtr pidl, [In, Out] ref UInt32 pdwAttributes);
IEnumIDList EnumObjects(IntPtr hwndOwner, [MarshalAs(UnmanagedType.U4)] SHCONTF grfFlags);
//[return: MarshalAs(UnmanagedType.Interface)]
void BindToObject(IntPtr pidl, IntPtr pbc, [In] ref Guid riid, out IntPtr ppv);
//[return: MarshalAs(UnmanagedType.Interface)]
object BindToStorage(IntPtr pidl, IntPtr pbc, [In] ref Guid riid, out IntPtr ppv);
[PreserveSig()]
UInt32 CompareIDs(Int32 lparam, IntPtr pidl1, IntPtr pidl2);
[return: MarshalAs(UnmanagedType.Interface)]
object CreateViewObject(IntPtr hwndOwner, [In] ref Guid riid);
//[PreserveSig()]
//UInt32 GetAttributesOf(UInt32 cidl, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
// IntPtr[] apidl, ref SFGAO rgfInOut);
[PreserveSig()]
UInt32 GetAttributesOf(UInt32 cidl, IntPtr apidl, [In, Out] [MarshalAs(UnmanagedType.U4)] ref SFGAO rgfInOut);
//[PreserveSig()]
//UInt32 GetUIObjectOf(IntPtr hwndOwner, UInt32 cidl, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
// IntPtr[] apidl, [In] ref Guid riid, ref UInt32 rgfReserved, out IntPtr ppv);
[PreserveSig()]
UInt32 GetUIObjectOf(IntPtr hwndOwner, UInt32 cidl, ref IntPtr apidl, [In] ref Guid riid, ref UInt32 rgfReserved, ref IntPtr ppv); //←☆☆これがまだちゃんと動かない
void GetDisplayNameOf(IntPtr pidl, SHGDNF uFlags, out STRRET pName);
[PreserveSig()]
void SetNameOf(IntPtr hwndOwner, IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] string pszName,
SHGDNF uFlags, out IntPtr ppidlOut);
}
class ShellApi
{
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public static extern int SHGetDesktopFolder(out IShellFolder ppshf);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern UInt32 SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] String pszName,
IntPtr pbc, out IntPtr ppidl, UInt32 sfgaoIn, out UInt32 psfgaoOut);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern Int32 SHBindToObject(IShellFolder shell, IntPtr pidl, IntPtr pbc, ref Guid riid,
out IntPtr ppvOut);
}
public static Image GetThumbnailImage(string strFilePath)
{
IntPtr peaten = IntPtr.Zero;
IntPtr ppidl = IntPtr.Zero;
UInt32 iattr = 0;
IntPtr pbmp = IntPtr.Zero;
string strDir = strFilePath.Substring(0, strFilePath.LastIndexOf('\\')) + '\\';
//string strFile = Path.GetFileName(strFilePath);
IShellFolder shell = null;
try
{
ShellApi.SHGetDesktopFolder(out shell);
}
catch (Exception e)
{
}
shell.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, strFilePath, ref peaten, out ppidl, ref iattr);
//ShellApi.SHParseDisplayName(strFilePath, IntPtr.Zero, out ppidl, 0, out iattr);
IExtractImage ieimg = null;
peaten = IntPtr.Zero;
if (ppidl != IntPtr.Zero)
{
UInt32 rgfRes = 0;
IntPtr res = IntPtr.Zero;
shell.GetUIObjectOf(IntPtr.Zero, 1, ref ppidl, ref IID_IExtractImage, ref rgfRes, ref res); //☆☆←どうしてもresの結果がnullのまま、うまくいかない
ieimg = (IExtractImage)Marshal.GetTypedObjectForIUnknown(res, typeof(IExtractImage));
if (ieimg != null)
{
StringBuilder sb = new StringBuilder(1024);
SIZE size = new SIZE();
size.cx = 64;
size.cy = 64;
UInt32 priority = 0;
IEIFLAG flag = IEIFLAG.IEIFLAG_ORIGSIZE | IEIFLAG.IEIFLAG_SCREEN | IEIFLAG.IEIFLAG_ASPECT;
ieimg.GetLocation(sb, (UInt32)sb.Capacity, out priority, ref size, 32, ref flag);
ieimg.Extract(out pbmp);
Marshal.FreeCoTaskMem(ppidl);
}
}
Image image = null;
if (pbmp != null)
{
image = Image.FromHbitmap(pbmp);
}
if (shell != null)
{
Marshal.ReleaseComObject(shell);
}
if (ieimg != null)
{
Marshal.ReleaseComObject(ieimg);
}
AcroPDFLib.AcroPDF pdf = new AcroPDF();
pdf.gotoNextPage();
return image;
}
}