|
-
Sep 6th, 2025, 07:25 AM
#1
Thread Starter
Lively Member
Open a PDF file using the 'CreateFileMoniker' API in VB.NET
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
************
Last edited by IT_Researcher; Sep 9th, 2025 at 05:15 AM.
-
Sep 8th, 2025, 07:26 AM
#2
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
You know this isn't VB6, where you have to resort to this kind of arcane wizardry to do simple things like opening a PDF. I am 100% certain you can very easily find a dozen Nuget packages that deal with PDFs.
-
Sep 9th, 2025, 05:16 AM
#3
Thread Starter
Lively Member
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
@Niya Please check today's EDIT in the question for our actual requirement.
-
Sep 9th, 2025, 12:09 PM
#4
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
There is no COM class to represent a PDF as far as I know so Monikers won't work here.
-
Sep 10th, 2025, 01:01 AM
#5
Thread Starter
Lively Member
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
@Niya . Sure, but could you please suggest us which of the following methods are preferable to open any file programmatically using VB .NET, and which has less chances of getting interferred by Antivirus software or Security software ? 'Process.Start with UseShellExecute as True' v/s ShellExecute . Or if any better method that is also fine.
-
Sep 10th, 2025, 08:22 AM
#6
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
Ok, but first, let's be very clear about what you are looking for. Do you want to open and display the entire PDF? Do you want to write to it, or do you want to extract something specific from the PDF, like an image or piece of text?
-
Sep 10th, 2025, 08:44 AM
#7
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
In the meantime you can try the "open" verb which may be less suspicious to AVs:-
Code:
Dim pi As New ProcessStartInfo
pi.Verb = "open"
pi.FileName = "c:\temp temp\test.pdf"
pi.UseShellExecute = True
Process.Start(pi)
-
Sep 11th, 2025, 05:45 AM
#8
Thread Starter
Lively Member
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
I want to open PDF, Excel, and Word (.docx) files with their default programs fully, just like if the user double-clicked them. I do not want to make them read-only or hide any part of the file.
-
Sep 11th, 2025, 05:47 AM
#9
Thread Starter
Lively Member
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
 Originally Posted by Niya
Ok, but first, let's be very clear about what you are looking for. Do you want to open and display the entire PDF? Do you want to write to it, or do you want to extract something specific from the PDF, like an image or piece of text?
I want to open PDF, Excel, and Word (.docx) files with their default programs fully, just like if the user double-clicked them. I do not want to make them read-only or hide any part of the file.
-
Sep 11th, 2025, 06:11 AM
#10
Re: Open a PDF file using the 'CreateFileMoniker' API in VB.NET
You should be able to do this directly with the Process object. something like the following should do the trick (not tested however)
Code:
Dim myProcess As New Process
myProcess.StartInfo.FileName = "<path to the file you want to open>"
myProcess.StartInfo.UseShellExecute = True
myProcess.StartInfo.RedirectStandardOutput = False
myProcess.Start()
myProcess.Dispose()
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|