带有遗留 .NET 3DEngine 的 C# 屏幕保护程序 - 构建配置

C# screensaver with legacy .NET 3DEngine - build configuration

我编写了一个 C# 屏幕保护程序,它可以在预览(安装)模式、配置甚至测试模式下运行。但是,当到达 windows 计时器启动它时,屏幕变黑,我看到鼠标加载图标持续 2-3 秒,然后屏幕恢复到桌面。

我在 main() 中添加了一个日志文件条目作为代码的第一行,当 windows 启动时,这段代码似乎从来没有 运行。

使用 Visual studio 2017 年 Windows 10.

由于我使用的是旧的 3D 引擎,因此我确保对 app.config 进行了修改:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <startup useLegacyV2RuntimeActivationPolicy="true"> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
      <supportedRuntime version="v1.1.4322"/>
    </startup>
</configuration>

我将 Screensaver.exe 重命名为 Screensaver.scr,同时将 app.config 重命名为 Screensaver.scr.config。将这些与我的引擎 dll 一起复制到 SysWOW64 文件夹中。

构建平台目标 = x86。

我尝试了调试和发布版本... 我使用相同的代码结构来做一个显示文本的屏幕保护程序的简单示例并且它有效因此我真的认为问题来自 3D 引擎 dll 的使用。

你们有什么建议吗?适用于 .scr 的配置中是否有一些特殊性? 在任何地方都找不到任何线索,我不知道....

这里是主要代码,如果有帮助的话:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TV3D;

namespace ScreenSaver
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            LogMessageToFile("Hello, World");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            CLTV3D tv3d = new CLTV3D();

            if (args.Length > 0)
            {
                string firstArgument = args[0].ToLower().Trim();
                string secondArgument = null;

                // Handle cases where arguments are separated by colon.
                // Examples: /c:1234567 or /P:1234567
                if (firstArgument.Length > 2)
                {
                    secondArgument = firstArgument.Substring(3).Trim();
                    firstArgument = firstArgument.Substring(0, 2);
                }
                else if (args.Length > 1)
                    secondArgument = args[1];

                if (firstArgument == "/c")           // Configuration mode
                {
                    Application.Run(new ScreenSaverSettingsForm());
                }
                else if (firstArgument == "/p")      // Preview mode
                {
                    if (secondArgument == null)
                    {
                        MessageBox.Show("Sorry, but the expected window handle was not provided.",
                            "ScreenSaver", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    IntPtr previewWndHandle = new IntPtr(long.Parse(secondArgument));
                    Application.Run(new TVForm(previewWndHandle, tv3d));


                }
                else if (firstArgument == "/s")      // Full-screen mode
                {
                    tv3d.TV.AddToLog("full screen mode argument detected");

                    foreach (Screen screen in Screen.AllScreens)
                    {
                        TVForm tv = new TVForm(screen.Bounds, screen.DeviceName, tv3d);
                        tv.Show();
                    }
                    Application.Run();
                }
                else    // Undefined argument
                {
                    MessageBox.Show("Sorry, but the command line argument \"" + firstArgument +
                        "\" is not valid.", "ScreenSaver",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else    // No arguments - treat like /c
            {
                Application.Run(new ScreenSaverSettingsForm());
            }
        }



        static public string GetTempPath()
        {
            string path = System.Environment.GetEnvironmentVariable("TEMP");
            if (!path.EndsWith("\")) path += "\";
            return path;
        }

        static public void LogMessageToFile(string msg)
        {
            System.IO.StreamWriter sw = System.IO.File.AppendText(
                GetTempPath() + "My Log File.txt");
            try
            {
                string logLine = System.String.Format(
                    "{0:G}: {1}.", System.DateTime.Now, msg);
                sw.WriteLine(logLine);
            }
            finally
            {
                sw.Close();
            }
        }


    }
}

看来您已将范围缩小到 3d 组件。

如果没有日志文件,您可以确定应用程序无法启动,如果没有错误消息,则很难诊断原因。以下是一些故障排除步骤。

尝试:

  • 线索的事件日志,
  • 到 'late bind' CLTV3D 在 Try/Catch、
  • 中使用 Assembly.Load
  • 运行 ProcessMonitor 查看失败的原因。

如果以上方法不起作用,请设置 DebugDiag(或带有 WinDbg 和 SOS 的 AdPlus)并分析故障转储。

否则,.Net 1.1 就像 15 岁一样!!!帮自己一个忙,使用最新的库会容易得多。