首页 > 解决方案 > 带有进程类的点网核心 C# 打开照片查看器

问题描述

我将使用 .net core 打开照片查看器,这是我的代码

using System.Diagnostics;
namespace TestProcessForOpenPhoto
{
    class Program
    {
        static void Main(string[] args)
        {
            var photoViewer = new Process();
            photoViewer.StartInfo.FileName = @"C:\Program Files\Windows Photo Viewer\PhotoViewer.dll";
            photoViewer.StartInfo.Arguments = @" C:\Users\XXXXX\Desktop\TestImage\abc.jpg";
            photoViewer.StartInfo.UseShellExecute = false;
            photoViewer.Start();
        }
    }
}

我收到了这个错误信息

System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'

谁能帮我解决这个错误,谢谢

标签: c#.net-core

解决方案


在对此进行研究后,我注意到人们使用 Microsoft Photo Viewer 应用程序rundll32.exe执行导出PhotoViewer.dll以显示图片。所以我认为这就是 OP 试图做的,他们只是忘记使用该rundll32.exe应用程序。

所以我想我会对此有所了解,而不是使用,rundll32.exe而只是直接调用导出。我用 x86dbg 对其进行了调试,发现它传入了 4 个参数:指针、指针、指针 (to wchar_t*)、int。我不知道参数是做什么的,所以我只是将它们设置为 NULL 并确保将图片的路径作为第三个传递,它似乎可以工作。

所以这会做你想做的事。我知道硬编码系统路径是不好的做法,但也许有更多时间的人可以让它更有活力。

private static class WindowsPhotoViewer
{
    private const string FilePath32 = @"c:\program files (x86)\Windows Photo Viewer\PhotoViewer.dll";
    private const string FilePath64 = @"c:\program files\Windows Photo Viewer\PhotoViewer.dll";

    [DllImport(FilePath32, CharSet = CharSet.Unicode, EntryPoint = "ImageView_FullscreenW")]
    private static extern void ImageView_Fullscreen32(
        IntPtr unknown1, IntPtr unknown2, string path, int unknown3);

    [DllImport(FilePath64, CharSet = CharSet.Unicode, EntryPoint = "ImageView_FullscreenW")]
    private static extern void ImageView_Fullscreen64(
        IntPtr unknown1, IntPtr unknown2, string path, int unknown3);

    public static bool ShowImage(FileInfo imageFile)
    {
        if ((IntPtr.Size == 8) && File.Exists(FilePath64) && imageFile.Exists)
        {
            ImageView_Fullscreen64(IntPtr.Zero, IntPtr.Zero, imageFile.FullName, 0);
            return true;
        }
        else if ((IntPtr.Size == 4) && File.Exists(FilePath32) && imageFile.Exists)
        {
            ImageView_Fullscreen32(IntPtr.Zero, IntPtr.Zero, imageFile.FullName, 0);
            return true;
        }
        return false;
    }
}

然后你可以这样称呼它:

if(!WindowsPhotoViewer.ShowImage(new FileInfo(@"c:\users\andy\desktop\test.jpg")))
{
    Console.WriteLine("Failed to show image");
}

推荐阅读