views:

108

answers:

5

Hi everyone, 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.

A: 

You could use this method to verify if the printer exist on the system:

string printerName = "Foxit Reader";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();

If the coll object contain an element, it means that the printer is installed. I didn't test the code, took it from http://stackoverflow.com/questions/296182/how-to-get-printer-info-in-c-net. Tell me if it works, otherwise i'll remove my post!

Pierre-Luc Champigny
+2  A: 

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.

Jerry Coffin
@divo:Thanks for adding the link.
Jerry Coffin
+1  A: 

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
Beaner
+2  A: 

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
0xA3
A: 

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);
      }
    }
  }
}
ProKiner