andres descalzo
andres descalzo

Reputation: 14967

How to know if there is any PDF reader on the client PC

I need to know if there is any way to detect in a client PC (Windows) there any PDF reader (Adobe Reader, Foxit Reader, ...), using VB6 and. NET (C #).

I can not do by reading the Windows registry, because the user may not have permissions to read it.

Thank you.

Upvotes: 0

Views: 719

Answers (4)

ProKiner
ProKiner

Reputation: 707

I know the question was for VB, but found a decent implementation of FindExecutable in C# here:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace PaytonByrd {
public class Win32API
{
  [DllImport("shell32.dll", EntryPoint="FindExecutable")] 
  public static extern long FindExecutableA(
    string lpFile, string lpDirectory, StringBuilder lpResult);

  public static string FindExecutable(
    string pv_strFilename)
  {
    StringBuilder objResultBuffer = 
      new StringBuilder(1024);
    long lngResult = 0;

    lngResult = 
      FindExecutableA(pv_strFilename, 
        string.Empty, objResultBuffer);

    if(lngResult >= 32)
    {
        return objResultBuffer.ToString();
    }

    return string.Format(
      "Error: ({0})", lngResult);
  }
}}

Here's a usage example:

using System;
using System.Diagnostics;
using System.IO;
using PaytonByrd;

namespace CSharpFindExecutableTester
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      foreach(string strFile in 
        Directory.GetFiles("c:\\"))
      {
        string strOutput = 
          string.Format(
            "{0} - Application: {1}",
            strFile, 
            Win32API.FindExecutable(
              strFile));

        Debug.WriteLine(strOutput);
      }
    }
  }
}

Upvotes: 0

Dirk Vollmar
Dirk Vollmar

Reputation: 176169

A sample based on Jerry Coffin's suggestion to use FindExecutable (based on this article):

Private Declare Function FindExecutable Lib "shell32.dll" _
  Alias "FindExecutableA"  ( _
  ByVal lpFile As String, _
  ByVal lpDirectory As String, _
  ByVal lpResult As String) As Long 

Private Declare Function lstrlen Lib "kernel32.dll" _
  Alias "lstrlenA" ( _
  ByVal lpString As Any) As Long

' FindExecutable Constants
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&

Private Sub Command1_Click()
  Dim Retval As Long, buffer As String

  ' init buffer 
  buffer = Space(256)

  Retval = FindExecutable("c:\windows\media\tada.wav", "", buffer)

  Select Case Retval
    Case 0
      Debug.Print "Not enough memory to execute this method." 
    Case 31
      Debug.Print "No file association found."
    Case ERROR_FILE_NOT_FOUND
      Debug.Print "File not found."
    Case ERROR_PATH_NOT_FOUND
      Debug.Print "Path not found."
    Case ERROR_BAD_FORMAT
      Debug.Print "The associated application is not a valid Win32 executable."
    Case Else
      Debug.Print "Associated application: " & Left$(buffer, lstrlen(buffer))
  End Select
End Sub

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490138

Create a temporary file with an extension of ".pdf", and then use FindExectuable for your temporary file. Note that even though only the extension really matters, FindExecutable still requires a real file, not just a name with the right extension.

Upvotes: 2

jac
jac

Reputation: 9726

This VB routine will get the default executable for a file extension. If there is no executable then the user does not have a program configured to handle the extension.

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long

Function GetAssociation(ByVal Path As String, ByVal FileName As String) As String
   Dim Result As String
   Dim x As Long
   Dim lngLenBuff

   lngLenBuff = 256
   Result = String(lngLenBuff, vbNullChar)
   x = FindExecutable(FileName, Path, Result)
   GetAssociation = Left$(Result, InStr(Result, Chr$(0)) - 1) 'get string to left of the null'
End Function

Upvotes: 1

Related Questions