1

I have 2 WPF Windows classes. Lets name them Class A and Class B

Both A and B have a WebBrowser Control

Both A and B are calling a common method from a C++/CLI DLL to print custom header and footer from the link below: https://www.limilabs.com/blog/printing-in-webbrowser-control-custom-header-and-footer

Both A and B executes the same code printFromWebBrowser(), however the method of calling is different

Class A works well on both x86 and x64, but the trigger comes from a button, the printout comes out successfully without crashing

Class B works well on x86 but crashes on x64

In class B upon window open, there is a background worker that will retrieve data and load information on the webbrowser control, I checked and the data loaded successfully and the background worker have completed the job before starting the timer.

After the webbrowser completed loading, the timer will start and will call the common method after x seconds

I know there is a difference between IntPtr in x86 and x64 machines, however I am not sure why it works well and doesnt crash on class A when i invoked the same method if it was indeed an IntPtr issue, except that the trigger is now automated instead of user triggered

To Add On, the C# project's target platform is x86, C++/CLI project is set to Win32

Any ideas to move forward will be appreciated

Thanks

Code snippet below

Common code

private void printFromWebBrowser()
{
    try
    {
        IntPtr ptr = Marshal.GetComInterfaceForObject(
             webBrowser1.axIWebBrowser2,
             typeof(IWebBrowser2));


        string header = " Custom header ";
        string footer = " Custom Footer";

        PrintHelper.Print(ptr, header, footer);
    }
    catch (Exception ex)
    {
        System.Windows.MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + ex.InnerException.Message + Environment.NewLine + ex.InnerException.StackTrace);
    }
 }

Class B worker complete thread

private void printWorker_RunWorkerCompleted(object s, RunWorkerCompletedEventArgs args)
{
    if (canPrint)
    {
        webBrowser1.Navigate(docURL);
    }
    else
    {
        System.Windows.MessageBox.Show(errorMsg, "Error Printing Document", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        this.Close();
    }
}

Class B Web browser document complete, only then start timer

private void WB_OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (this.webBrowser1.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
        return;
    else
    {
        timer1.Start();
    }
}

Class B Timer Tick

void timer1_Tick(object sender, EventArgs e)
{
     counter++;//some counter to wait for number of ticks
     if (counter >= x) //x is a global class variable defined 
     {
          timer1.Stop();
          printFromWebBrowser();
          this.Close(); //close after printing
     }
} 
Allanckw
  • 641
  • 1
  • 6
  • 17
  • Are you trying to do this from a Windows Service or from ASP.NET? Because [such scenarios are not officially supported](https://msdn.microsoft.com/en-us/library/system.drawing.printing(v=vs.110).aspx) and it's very common to see crashes like this on them. – yms Jul 18 '17 at 16:02
  • If you are not printing from a service, then try to [shift the call to your printing methods to the main (ui) thread of your app](https://stackoverflow.com/a/11625264/501196) – yms Jul 18 '17 at 16:08

0 Answers0