2015-04-24 34 views
0

我從一個較舊的16位MSBasic程序調用在VS2013 vb.net中編寫的程序時出現錯誤,但是如果我從基於WPF的庫更改爲基於GDI +圖書館。使用Visual Studio WPF應用程序和16位command.com

首先是一些背景:

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

從PDF SHARP WEB SITE

PDFsharp是用於處理PDF文件.NET庫。您可以使用GDI +中的繪圖程序創建PDF 頁面。幾乎所有可以用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來調用任何程序,所以有什麼不同? GDI +版本PDFSharp.dll也沒問題。我的測試代碼只是一個按鈕調用下面的代碼形式:

' 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過錯誤。但是他們的註冊系統有問題。我從來沒有得到他們的驗證電子郵件。所以儘管我也會在這裏嘗試。

回答

1

我發現問題和解決方案。問題不是真正的PDFSharp。它是一般的任何.NET WPF應用程序。使用Visual Studio創建的WPF應用程序需要將環境變量WINDIR設置爲Windows目錄。當使用16位command.com時,它不具有WINDIR環境變量。在運行.NET WPF應用程序之前添加此環境變量允許應用程序運行。

在互聯網上研究這一點,許多網站認爲這是一個錯誤。我同意。 Visual Studio應用程序需要更正常地退出並顯示錯誤消息,或者更好地使用SYSTEMROOT。看來基於winForms的.NET應用程序沒有這個要求。

要使此更改在Windows工作站上永久保存,請編輯文件autoexec。NT在C:\ Windows \ System32或%SYSTEMROOT%\ System32下加入線

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

相關問題