【问题标题】:Using Visual Studio WPF application and 16-bit command.com使用 Visual Studio WPF 应用程序和 16 位 command.com
【发布时间】:2023-03-21 01:25:02
【问题描述】:

从较旧的 16 位 MSBasic 程序调用用 VS2013 vb.net 编写的程序时出现错误,但如果我从基于 WPF 的库更改为基于 GDI+ 的库,它可以工作。

先介绍一下背景:

http://www.pdfsharp.net 的 PDFSharp 库可以使用 GDI 或 WPF

来自 PDF 夏普网站

PDFsharp 是一个用于处理 PDF 文件的 .NET 库。您创建 PDF 使用 GDI+ 中已知的绘图例程的页面。几乎任何可以 使用 GDI+ 完成也适用于 PDFsharp。只有基本的文本布局 受 PDFsharp 支持,不创建分页符 自动地。相同的绘图例程可用于屏幕、PDF、 或元文件。

PDFSharp 提供的库使用来自 GDI+ 或 WPF 的绘图例程。

问题:

我正在创建的程序是用 VS2013 VB.NET 编写的。我从一个旧的 Microsoft Basic 程序调用这个程序(.EXE 文件),该程序使用command.com 来启动程序。使用command /c pdftest.exe 启动程序时出现错误。但是,使用命令cmd /c pdftest.exe 使用cmd.exe 时,我没有收到错误消息。此外,在使用相同程序但使用 PDFSharp.dll 的 GDI+ 库版本时,我也没有收到错误。

我知道command.com 是 16 位程序,cmd.exe 是 32 位程序。我的理解是command.com使用cmd.exe调用任何程序,那么有什么不同呢? PDFSharp.dll 的 GDI+ 版本也很好。我的测试代码只是一个带有一个按钮的表单,它调用以下代码:

    ' Create a new PDF document
    Dim document As PdfDocument = New PdfDocument
    document.Info.Title = "Created with PDFsharp"

    ' Create an empty page
    Dim page As PdfPage = document.AddPage

    ' Get an XGraphics object for drawing
    Dim gfx As XGraphics = XGraphics.FromPdfPage(page)

    ' Draw crossing lines
    Dim pen As XPen = New XPen(XColor.FromArgb(255, 0, 0))

    ' Create a font
    Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)

    ' Draw the text
    gfx.DrawString("Hello, World!", font, XBrushes.Black, _
    New XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.Center)

    ' Save the document...
    Dim filename As String = "HelloWorld.pdf"
    document.Save(filename)

这是错误

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.TypeInitializationException: The type initializer for 'System.Windows.Media.FontFamily' threw an exception. ---> System.TypeInitializationException: The type initializer for 'MS.Internal.FontCache.Util' threw an exception. ---> System.UriFormatException: Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString, UriKind uriKind)
at MS.Internal.FontCache.Util..cctor()
--- End of inner exception stack trace ---
at System.Windows.Media.FontFamily..cctor()
--- End of inner exception stack trace ---
at System.Windows.Media.Typeface..ctor(FontFamily fontFamily, FontStyle style, FontWeight weight, FontStretch stretch)
at PdfSharp.Drawing.FontHelper.CreateTypeface(FontFamily family, XFontStyle style)
at PdfSharp.Drawing.XFont.Initialize()
at PdfSharp.Drawing.XFont..ctor(String familyName, Double emSize, XFontStyle style)
at PDFSharpTestWin.PDFSharpTestWin.cmdBasicTest_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我也在尝试在 PDFSharp 论坛上发帖。但是他们的注册系统有问题。我从来没有收到他们的验证电子邮件。所以虽然我也会在这里尝试。

【问题讨论】:

    标签: wpf vb.net windows gdi+ 16-bit


    【解决方案1】:

    我找到了问题和解决方案。问题不是真正的 PDFSharp。它通常是任何 .NET WPF 应用程序。使用 Visual Studio 创建的 WPF 应用程序需要将环境变量 WINDIR 设置为 Windows 目录。使用 16 位 command.com 时,它没有 WINDIR 环境变量。在运行 .NET WPF 应用程序之前添加此环境变量允许应用程序运行。

    在互联网上对此进行了研究,许多网站认为这是一个错误。我会同意的。 Visual Studio 应用程序需要更优雅地退出并显示错误消息,或者更好地使用 SYSTEMROOT 代替。基于 winForms 的 .NET 应用似乎没有这个要求。

    要使此更改在 Windows 工作站上永久生效,请编辑 C:\Windows\System32 或 %SYSTEMROOT%\System32 中的文件 autoexec.nt 添加行

    set windir=%systemroot%
    

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/b54f4d59-e62f-467d-9a51-daff5e7947a2/what-is-best-to-do-if-windir-environment-variable-does-not-exist?forum=wpf

    http://nerdynotes.blogspot.com/2010/05/wpf-program-crashes-when-started-from.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 2010-10-26
      • 2018-05-10
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      相关资源
      最近更新 更多