I am looking for ways to open a PDF document using the 'CreateFileMoniker' API call. I have observed using certain API monitoring tools that when we have a file path (such as a PDF) in a Microsoft Excel document mentioned as a hyperlink, the click action calls the 'CreateFileMoniker' which makes many other calls and finally opens the PDF file.
I have not seen Process.Start or ShellExecute being present directly in those calls and I would like to replicate the same for one of my application using a VB.NET code. This is the code I have, but it throws a
Failed. HRESULT=800401EA error message. Took help from ChatGPT and other sources but to no avail.
HTML Code:
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.ComTypes
Public Class Form1
<DllImport("ole32.dll", CharSet:=CharSet.Unicode)>
Private Shared Function CreateFileMoniker(
ByVal lpszPathName As String,
ByRef ppmk As IMoniker
) As Integer
End Function
<DllImport("ole32.dll", CharSet:=CharSet.Unicode, PreserveSig:=True)>
Private Shared Function CoGetObject(
ByVal pszName As String,
ByVal pBindOptions As IntPtr,
<[In]()> ByRef riid As Guid,
<Out(), MarshalAs(UnmanagedType.Interface)> ByRef ppv As Object
) As Integer
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Dim pdfPath As String = "C:\temp\file.pdf"
' Create the IUnknown GUID
Dim IID_IUnknown As New Guid("00000000-0000-0000-C000-000000000046")
Dim unk As Object = Nothing
Dim hr As Integer = CoGetObject("file://" & pdfPath, IntPtr.Zero, IID_IUnknown, unk)
If hr = 0 AndAlso unk IsNot Nothing Then
MessageBox.Show("PDF opened successfully via OLE/COM.")
Else
MessageBox.Show("Failed. HRESULT=" & hr.ToString("X"))
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
The API sequence ( not exhaustive ) drafted by observing the process used by MS Excel to open the PDF hyperlink file is as below:
1.When PDF file is opened from hyperlink through excel, the HLINK.dll calls the API "CreateFileMoniker"
2.The KERNELBASE.dll calls the API wcschr
3.urlmon.dll calls the API wcschr
4.Finally, the HLINK.dll calls the "GetFileAttributesW" API
5.This then inititates "RtlDosPathNameToNtPathName_U_WithStatus"
6.Acrobat reader comes into picture
7.Call to "RtlDosPathNameToRelativeNtPathName_U_WithStatus"
Which then initiates "ExtTextOutW''
How to modify my code to achieve the same in VB. NET?
The method tried is mentioned above
************
EDIT 09-09-2025
The actual purpose of our question is to look for a best method to open a file using it's default application in Windows using VB.NET application. We are presently using ShellExecute, but the process gets interfered by Antivirus software present in the PC. When tried by pasting it as a file link into a cell of MS Excel, and opened using Hyperlink, there was no antivirus interference. We posted about CreateFileMoniker as we observed this during the API calls as stated above. Any other such foolproof method to avoid AV interference is also ok
************