Microsoft.mshtml throws exception in platform targert x64 but works fine in x86 in windows 10 machine

Dilip kumar Pal 1 Reputation point
2022-03-10T12:19:48.193+00:00

I was trying to read the html elements using Microsoft.mshtml.dll and this is working fine when the project platform target is x86. Its able to extract the html elements, but when I tried to change the platform target as x64 its getting me exception as shown below

Message: Unable to cast COM object of type 'System.__ComObject' to class type 'mshtml.HTMLTableClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject type. Instances of this type cannot be cast to any other class; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
Source: VoyagerDocuments
StackTrace: at
DataCollection.Forms.Viewers.VoyagerHtmlDocumentViewer.buildMappingsTable(IHTMLElement tab, IHTMLElement ourCell, String tableName, String src, HtmlDocument CTAUTO_htmlDocument, String auto_scaling, String table_text) in C:\Dilip\DataCollection\VoyagerDocuments\Forms\Viewers\VoyagerHtmlDocumentViewer.cs:line 1371
at DataCollection.Forms.Viewers.VoyagerHtmlDocumentViewer.ReadSpecificTable(String tablename) in C:\Dilip\DataCollection\VoyagerDocuments\Forms\Viewers\VoyagerHtmlDocumentViewer.cs:line 1207
at DataCollection.Forms.Viewers.VoyagerDocumentViewer.toolStripMenuItemBSDifferentiated_Click(Object sender, EventArgs e) in C:\Dilip\DataCollection\VoyagerDocuments\Forms\Viewers\VoyagerDocumentViewer.cs:line 6020
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at TRF.Collection.App.Main()
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

In the current project we have 64 bit dll to read pdf file. Need help on how can I cast this html elements in x64 target platform using 32 bit Microsoft.mshtml.dll
Can you let us know is there any 64 bit microsoft.mshtml.dll.

Code we are trying to cast is as below

HtmlTableElementInfo _tableSelectedElement;
var tableEntry = (IHTMLElement) _tableSelectedElement.tableElement;
IHTMLElement el = tableEntry;
IHTMLElement tab=el;
IHTMLElementCollection Rows = ((HTMLTableClass) tab).rows;
temp_tablearray = new IHTMLElement[Rows.length,100];

Thanks,
Dilip

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,918 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
337 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,166 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,621 Reputation points
    2022-03-11T16:16:29.08+00:00

    a 64 bit app can not call a 32 bit dll, nor can a 32 bit app call a 64bit dll.

    the main workaround, is to create a separate process, that runs the other fitness, and use some process inter communication between processes.

    see:

    https://blog.mattmags.com/2007/06/30/accessing-32-bit-dlls-from-64-bit-code/

    your other option is to upgrade to Edge WebView2 rather than Mshtml.

    0 comments No comments

  2. Castorix31 85,806 Reputation points
    2022-03-12T17:20:21.54+00:00

    Can you let us know is there any 64 bit microsoft.mshtml.dll.

    C:\Windows\System32\mshtml.dll is 64-bit

    C:\Windows\SysWOW64\mshtml.dll is 32-bit

    I tested MSHTML in x64, on Windows 10 21H1, just by parsing links from Google and it works fine, loading C:\Windows\System32\mshtml.dll...

    0 comments No comments

  3. Tanya Solyanik 80 Reputation points Microsoft Employee
    2023-02-16T21:15:44.2533333+00:00

    Hi Dilip,

    Sounds like you should distribute the PIA file with your 64 bit application. You can generate the PIA from the TLB file located in the system directory on 64 bit Windows - C:\Windows\System32\mshtml.tlb using the SDK tool - How to: Generate Primary Interop Assemblies Using Tlbimp.exe - .NET Framework | Microsoft Learn

    Additionally, register the PIA assembly as a COM server in the registry, for example by creating a .reg file and running it as an administrator. Each class that you reference in the code, must be registered. The registration can be copied from the WoW6432Nodes on a 64 bit machine, or from a 32 bit machine. For example:

    Windows Registry Editor Version 5.00

     

    [HKEY_CLASSES_ROOT\CLSID{3050F26B-98B5-11CF-BB82-00AA00BDCE0B}\InprocServer32]

    "Assembly"="Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

    "Class"="mshtml.HTMLTableClass"

    "RuntimeVersion"="v1.0.3705"

     

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID{3050F26B-98B5-11CF-BB82-00AA00BDCE0B}\InprocServer32]

    "Assembly"="Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

    "Class"="mshtml.HTMLTableClass"

    "RuntimeVersion"="v1.0.3705"

    Hope this helps,

    Tanya

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.