Reputation: 141
I get an error when calling a program written in VS2013 vb.net from an older 16 bit MSBasic program but it works if I change from the WPF based library to GDI+ based library.
First some background:
The PDFSharp library from http://www.pdfsharp.net can use GDI or WPF
FROM PDF SHARP WEB SITE
PDFsharp is a .NET library for processing PDF file. You create PDF pages using drawing routines known from GDI+. Almost anything that can be done with GDI+ will also work with PDFsharp. Only basic text layout is supported by PDFsharp, and page breaks are not created automatically. The same drawing routines can be used for screen, PDF, or meta files.
The libraries provided by PDFSharp use drawing routines from GDI+ or WPF.
The problem:
The program I am creating is written in VS2013 VB.NET. I am calling this program (.EXE file) from an old Microsoft Basic program which uses command.com
to launch programs. I get an error when using command /c pdftest.exe
to launch the program. I however do not get the error when using cmd.exe
using the command cmd /c pdftest.exe
. Also I do not get the error when using the same program but using the GDI+ library version of the PDFSharp.dll
.
I know that command.com
is a 16 bit program and cmd.exe
is 32 bit. My understanding is that command.com
uses cmd.exe
to call any programs, so what is different? Also the GDI+ version of PDFSharp.dll
is fine. My test code is just a form with one button that calls the following code:
' 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)
Here is the error
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)
I am trying to post on PDFSharp forum too. But having trouble with their registration system. I never get their verification eMails. So though I would try here too.
Upvotes: 0
Views: 186
Reputation: 141
I have found the problem and solution. The problem is not really PDFSharp. It is any .NET WPF application in general. A WPF application created with Visual Studio requires the environment variable WINDIR be set to the Windows directory. When using the 16-bit command.com
it does not have the WINDIR environment variable. Adding this environment variable before running the .NET WPF application allows the application to run.
Researching this on the internet, many sites consider this a bug. I would agree. The visual studio applications need to either more gracefully exit with an error message or better yet use SYSTEMROOT instead. It appears that .NET applications that are based on winForms do not have this requirement.
To make this change permanent on a windows workstation, edit the file autoexec.nt in C:\Windows\System32 or %SYSTEMROOT%\System32 adding the line
set windir=%systemroot%
http://nerdynotes.blogspot.com/2010/05/wpf-program-crashes-when-started-from.html
Upvotes: 1