How to properly initialise a COM/ActiveX component in C#?
-
I'm trying to do something that might actually be a bit silly. I'm accessing what is essentially a windows form from an aspx page, to do some printing from the server side for me. I have the following code:
public class axForm : System.Windows.Forms.Form
{
public AxDHTMLEDLib.AxDHTMLEdit axd;
public axForm()
{
Thread.CurrentThread.ApartmentState=ApartmentState.STA;
axd=new AxDHTMLEDLib.AxDHTMLEdit();
((System.ComponentModel.ISupportInitialize)(axd)).BeginInit();
this.Controls.Add(axd);
axd.Enabled = true;
axd.Name = "axd";
((System.ComponentModel.ISupportInitialize)(axd)).EndInit();
}public void printDoc(string sHtml, bool bPrompt) { axd.DocumentHTML=sHtml; object filename=@"c:\\fileouttest.txt"; for(;axd.Busy!=false;) { System.Windows.Forms.Application.DoEvents(); } axd.SaveDocument(ref filename); object opt=null; if (bPrompt) opt="1"; axd.PrintDocument(ref opt); }
}
Basically I create an instance of that class in my aspx code behind, then call printDoc with the HTML I want to print and an indicator as to whether or not I want to see the print dialog (I don't but the code I based this on had the boolean in there and I thought it might be useful for testing). The problem is I'm getting a HRESULT error message and I don't know what to do about it. I'm guessing, since the error is fired just after the .endInit() line that it's something to do with how I'm setting the thing up in the first place, but I don't know enough about it to be able to debug it properly. The error is:
'System.Runtime.InteropServices.COMException' occurred in system.windows.forms.dll - Additional information: Exception from HRESULT: 0x80040200.
Can anyone help me figure this out? It seems like I'm really close to getting it working. If I tone down the amount of debug information I want, it actually runs without giving any errors at all, and the line that saves the document to disk actually works regardless, so I'm a bit confused. Any help would be greatly appreciated.