title: ASP.NET and COM interop
body: <p>I am trying to use a COM component in my asp.net web application. In this Component I am passing the <strong>path of an image file</strong> stored in my server directory and this component is giving me an IPicture datatype in return.</p>
  
  <p>Following are the steps that I am going through, I post my questions at each steps so that it remains specific.</p>
  
  <ol>
  <li>I am referencing the DLL in my solution.</li>
  <li>Early binding the DLL and instantiating the specific class and the interface.</li>
  </ol>
  
  <p><strong>Question 1.</strong> First up, I am not able to see all the methods and properties visible in the IL DASM in the VS intelisence. Only a few are available. why?</p>
  
  <pre><code>ViewerClass cview = null; // ViewerClass is the class from the COM Component
  Viewer Icsview = null; // Viewer is one of the interfaces that ViewerClass has.
  cview = new ViewerClass();
  
  if (cview is Viewer)
  {
      Icsview = (Viewer)cview;
      Icsview.Open(filePath, 0, 0); // filePath is a string, passing the path of the file present in my local directory.
  </code></pre>
  
  <p><strong>Question 2:</strong> This is where the error occures: - when the code comes to this line, I receive an InvalidCast exception:</p>
  
  <p><img src="https://i.stack.imgur.com/zRvxJ.jpg" alt="Screenshot of the error"></p>
  
  <ol start="3">
  <li>Calling the method that is supposed to work on the file and convert into an IPicture type variable:</li>
  </ol>
  
  <p>In current scenario, this code does not execute.</p>
  
  <p>I have tried in different ways to talk to this Component. I have tried latebinding.</p>
  
  <p>following is the code which I am using for latebinding:</p>
  
  <pre><code>object objCSViewerLateBound;
  Type objTypeCSViewer;
  object[] arrayInput = new object[3];
  arrayInput[0] = filePath;
  arrayInput[1] = 0;
  arrayInput[2] = 0;
  objTypeCSViewer = Type.GetTypeFromCLSID(new Guid("{89251546-3F1C-430D-BA77-F86572FA4EF6}"));
  objCSViewerLateBound = Activator.CreateInstance(objTypeCSViewer);
  objTypeCSViewer.InvokeMember("Open", BindingFlags.Default | BindingFlags.InvokeMethod, null, objCSViewerLateBound, arrayInput);
  // getting the values from the properties
  double viewMaxX = (double)objTypeCSViewer.InvokeMember("ViewMaxX", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
  double viewMaxY = (double)objTypeCSViewer.InvokeMember("ViewMaxY", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
  double viewMinX = (double)objTypeCSViewer.InvokeMember("ViewMinX", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
  double viewMinY = (double)objTypeCSViewer.InvokeMember("ViewMinY", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
  // geting the image height and width
  int imagewidth, imageheight, pictype;
  imagewidth = 1280; imageheight = 800; pictype = 1;
  object[] previewDetails = new object[7];
  previewDetails[0] = viewMinX;
  previewDetails[1] = viewMinY;
  previewDetails[2] = viewMaxX;
  previewDetails[3] = viewMaxY;
  previewDetails[4] = imagewidth;
  previewDetails[5] = imageheight;
  previewDetails[6] = pictype;
  IPicture pict = (IPicture)objTypeCSViewer.InvokeMember("Preview", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, previewDetails); 
  </code></pre>
  
  <p>latebinding works, but only for a few steps, I can pass through the Open method, I can get the values from the properties, but in the last line, I receive an error that TargetInvocationException was unhandled by user code. I dont know what it means.</p>
  
  <ol start="3">
  <li>Why is this error occuring?</li>
  </ol>
  
  <p><strong>Please note:</strong> I have tried using the component through Javascript block. It works fine.</p>
  
  <p><strong>Question 4:</strong> I have tried the same code snippets posted here in a winforms application. It works without any problem. this gets me thinking there is something wrong in my approach.  Is there any special process needed to talk to a COM Component in an ASP.NET app?</p>