Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2913
  • Last Modified:

How do I close Adobe Reader from within VB.NET

I have a program that will allow the user to "stamp" a PDF document with comments.  Of course, if the user is viewing the file (using Adobe Reader) the the system cannot save the "stamped" version of the document because it is opened by another user.

The program needs to find the instance of Adobe Reader that is running with the needed doc and close it so it can "stamp" the PDF doc.  

Does anyone out there know how to do this?
0
mkacos
Asked:
mkacos
  • 4
  • 4
1 Solution
 
CSecurityCommented:
Add System.Diagnostic namespace

Then
Dim myProcesses() as Process

'single process variable
Dim myProcess As Process

'Get the list of processes
myProcesses = Process.GetProcesses()

For Each myProcess in myProcesses
if myProcess.ProcessName = "Acrobat.exe" Then myProcess.Terminate()
if myProcess.ProcessName = "AcroRd32.exe" Then myProcess.Terminate()
Next
0
 
planoczCommented:
Another way to write the code is..............
'Acrobat Reader is this name only   'AcroRd32'
'''ADJUST CODE TO FIT WHAT YOU WANT TO DO
  'Check to see if the Acrobat Reader is open
        If AcrobatInstance() = True Then
            Me.Text = "... Acrobat Reader is Running."
            MsgBox("Please Close Acrobat Reader")
            Exit Sub
       End If

  Public Function AcrobatInstance() As Boolean
        Dim sPros As Process
        For Each sPros In Diagnostics.Process.GetProcessesByName("AcroRd32")
            If sPros.ProcessName = "AcroRd32" Then
                Return True
              'OR Use this
              'sPros.kill
           Else
                Return False
            End If
        Next
    End Function
0
 
CSecurityCommented:
Code I provided works, also there is Acrobat Professional, Acrobat Professional versions exe file name is Acrobat.exe
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mkacosAuthor Commented:
Thanks guys.  This works as I described it but, as usual, there is scope creep.

Adobe Reader runs as a single process that opens multiple windows.  In task manager, it shows a single process but in the applications tab it shows each Window.  

I need to be able to select the appropriate window to close.  I have been trying to figure this out on my own to no avail.  Your further assitance is greatly appreciated.

0
 
CSecurityCommented:
It closes Notepad, replace Notepad text to Title of window you want to close, that's all
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);

DllImport("user32.dll")]
Public static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060; 

private void button1_Click(object sender, EventArgs e)
        {
            // retrieve the handler of the window
            int iHandle = FindWindow("Notepad", "Untitled - Notepad");
            if (iHandle > 0)
            {
                // close the window using API
                SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
            }
        }

Open in new window

0
 
mkacosAuthor Commented:
CSecurity

I suck at C.  Can you give me a vb.net example or at least point me in the right direction?
0
 
CSecurityCommented:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function


public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060; 

    Dim nWnd As IntPtr
    Dim ceroIntPtr As New IntPtr(0)
    Dim Wnd_name as String

    Wnd_name= "Some App Name"
    nWnd = FindWindow(Nothing, Wnd_name)
    SendMessage(nWnd, WM_SYSCOMMAND, SC_CLOSE, 0)

Open in new window

0
 
mkacosAuthor Commented:
Perfect.  I was almost there when you sent the update so thanks a ton.  I did have to make a few adjustments to the code.  This is what I ended up with and it seems to work.  Only more testing will tell.

----------------------------------------------------------------------------------------------------------------------------
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
 <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Int32, ByVal lParam As Integer) As Int32
    End Function

    Private Sub myCloseIt(ByVal where As Integer)
        Dim nWnd As IntPtr
        Dim ceroIntPtr As New IntPtr(0)
        Dim WM_SYSCOMMAND As UInteger = Convert.ToUInt32("0112", 16)
        Dim SC_CLOSE As Integer = Convert.ToInt32("F060", 16)

        nWnd = FindWindow("Notepad", "test.txt - Notepad")

        If CInt(nWnd) > 0 Then
            SendMessage(nWnd, WM_SYSCOMMAND, SC_CLOSE, 0)
        End If
  End Sub
0
 
mkacosAuthor Commented:
I had to make a couple of minor adjustments to the definitions but it worked .  Thanks
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now