2012-08-14 71 views
0

越来越DLL路径名我有被安装的软件时,它被安装在动态地从注册表

HKEY_LOCAL_MACHINE\\SOFTWARE for 32-bit OS

HKEY_CURRENT_USER\\SOFTWARE for 64-bit OS. 

如何检测基于注册表键的dll路径的dll?

我不想像这样硬编码。

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software//NodeName") 

我可以尝试检测32位或64位操作系统,并完成类似

如果32-bit

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software//NodeName") 

如果64-bit

RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software//NodeName") 

但有什么办法我可以直接检测dll的路径,例如,如果指定了dll名称,并且h我是否做整个注册表检查并找到它的路径名?

+3

'HKEY_LOCAL_MACHINE'和'HKEY_CURRENT_USER'有不同的含义。为什么你使用一个位置为32位而另一个位置为64位。这似乎是错误的。 – 2012-08-14 17:14:17

回答

1

HKEY_CLASSES_ROOT \ CLSID包含系统上所有已注册COM文件(.ocx,.dll,.ax)的列表,无论是32位还是64位都无关紧要。

CLSID下的密钥由每个COM的生成的GUID来表示。

而且finaly进GUID键,有InprocServer32的子键,在它的默认值包含COM文件到SISTEM的路径,这样你可以找到它,如下所示:

VB代码:

''' <summary> 
    ''' Search and Find Registry Function 
    ''' </summary> 
    Public Shared Function SearchRegistry(ByVal dllName As String) As String 

     'Open the HKEY_CLASSES_ROOT\CLSID which contains the list of all registered COM files (.ocx,.dll, .ax) 
     'on the system no matters if is 32 or 64 bits. 
     Dim t_clsidKey As RegistryKey = Registry.ClassesRoot.OpenSubKey("CLSID") 

     'Get all the sub keys it contains, wich are the generated GUID of each COM. 
     For Each subKey In t_clsidKey.GetSubKeyNames.ToList 

      'For each CLSID\GUID key we get the InProcServer32 sub-key . 
      Dim t_clsidSubKey As RegistryKey = Registry.ClassesRoot.OpenSubKey("CLSID\" & subKey & "\InProcServer32") 

      If Not t_clsidSubKey Is Nothing Then 

       'in the case InProcServer32 exist we get the default value wich contains the path of the COM file. 
       Dim t_valueName As String = (From value In t_clsidSubKey.GetValueNames() Where value = "")(0).ToString 

       'Now gets the value. 
       Dim t_value As String = t_clsidSubKey.GetValue(t_valueName).ToString 


       'And finaly if the value ends with the name of the dll (include .dll) we return it 
       If t_value.EndsWith(dllName) Then 

        Return t_value 

       End If 

      End If 

     Next 

     'if not exist, return nothing 
     Return Nothing 

    End Function 

C#代码:

/// <summary> 
    /// Search and Find Registry Function 
    /// </summary> 
    public static string SearchRegistry(string dllName) 
    { 

     //Open the HKEY_CLASSES_ROOT\CLSID which contains the list of all registered COM files (.ocx,.dll, .ax) 
     //on the system no matters if is 32 or 64 bits. 
     RegistryKey t_clsidKey = Registry.ClassesRoot.OpenSubKey("CLSID"); 

     //Get all the sub keys it contains, wich are the generated GUID of each COM. 

     foreach (object subKey_loopVariable in t_clsidKey.GetSubKeyNames.ToList) { 
      subKey = subKey_loopVariable; 
      //For each CLSID\GUID key we get the InProcServer32 sub-key . 
      RegistryKey t_clsidSubKey = Registry.ClassesRoot.OpenSubKey("CLSID\\" + subKey + "\\InProcServer32"); 


      if ((t_clsidSubKey != null)) { 
       //in the case InProcServer32 exist we get the default value wich contains the path of the COM file. 
       string t_valueName = (from value in t_clsidSubKey.GetValueNames()where string.IsNullOrEmpty(value))(0).ToString; 

       //Now gets the value. 
       string t_value = t_clsidSubKey.GetValue(t_valueName).ToString; 


       //And finaly if the value ends with the name of the dll (include .dll) we return it 

       if (t_value.EndsWith(dllName)) { 
        return t_value; 

       } 

      } 

     } 

     //if not exist, return nothing 
     return null; 

    } 
+0

正如Liaqat Fayyaz在wow64文件夹中说的64位东西。当我为我的OPOS设备注册SO时就是这种情况。如果您在搜索时知道驱动程序的GUID,它可能会稍微加快速度。 – 2013-01-04 04:02:24

+0

它不工作。在CLSID找不到DLL的路径 – sky91 2017-08-02 02:28:29