Question [fonction] Récupérer les librairies de type (TLB)

Plus d'informations
il y a 11 ans 5 mois #12898 par Laurent Dardenne
En rapport avec ce post , voici une fonction récupérant toutes les informations des librairie de type.
Celle permet de récupérer le chemin d'une librairie de type afin de la tranformer en un fichier Interop (wrapper dotNET d'objet COM).

Le public visé est celui des développeurs, voir des scripteurs avancés.

[code:1]
function Get-TLBComInformation {
#Lit la registry HKCR:\TypeLib et crée un objet pour chaque librairie de type (TLB«») trouvée.

function New-TLBObject{
#Crée un objet personnalisé contenant des informations sur une librairie de type
Param (
[GUID] $LibID,
[string] $Name,
[String] $Version,
[string] $Helpdir
)

#Certaines clés peuvent ne pas être correctement renseignées
# lors de l'installation de la TLB:
# [HKEY_CLASSES_ROOT\TypeLib\{ED6BD1F1-0A6B-41C1-A57D-646C63F5A361}\4.0\0\win32]
# @=\"Path\"
$object=New-Object PSObject -Property @{
#HKEY_CLASSES_ROOT\Typelib\<LibID>\
#Toutes les TLBs ont un GUID, mais certaines n'ont pas de nom.
LibID=$LibID;

#KEY_CLASSES_ROOT\Typelib\<LibID>\<major version>.<minor version>\
#Ne peut ne pas être du type [System.Version],
#car certains éditeurs code le numéro de version en hexa : f.3 ou 1a.0
Version=$Version;

#HKEY_CLASSES_ROOT\Typelib\<LibID>\<major version>.<minor version>\
#Default Value: <friendly name for the library> Again, not really required, but nice for oleview
Name=$Name;

#HKEY_CLASSES_ROOT\Typelib\<LibID>\<major version>.<minor version>\HELPDIR
#Default Value: <Directory that contains the help file for the type library>
#Commun à toutes les versions et pour tous les pays.
HelpDir=$Helpdir;

#HKEY_CLASSES_ROOT\Typelib\<LibID>\<major version>.<minor version>\<LCID for library>
#0 = neutral (NLS LANG_SYSTEM_DEFAULT), 9=en , 409 =en-US
LCID=$null;

#HKEY_CLASSES_ROOT\Typelib\<LibID>\<major version>.<minor version>\<LCID>\<Platform>
#Cette version ne gére pas les TLB 16 bits mais uniquement celles 32 et 64 bits.
is64Bit=$false;

#HKEY_CLASSES_ROOT\Typelib\<LibID>\<major version>.<minor version>\<LCID>\<Platform>.(Default)
#Chemin complet du fichier TLB, peut être .tlb,.exe,.ocx
#Certains Path peuvent contenir des informations supplémentaires en fin de fichier
#Exemple : C:\Program Files\Microsoft Lync\communicator.exe\3
Path=$null

#Dans ce contexte, on ne gére pas la clé FLAGS : la pluspart du temps 0
#cf. msdn.microsoft.com/en-us/library/windows...221149(v=vs.85).aspx
}
$object.PsObject.TypeNames[0] = \"TypeLibInformation\"
Return $Object
} #New-TLBObject

Dir Microsoft.PowerShell.Core\Registry::HKEY_CLASSES_ROOT\TypeLib|
Foreach-Object {
$TLBLibID=$_.PSChildName
Write-Debug \"Read TLB : $TLBLibID\"
Dir $_.PSPath|
Foreach-Object {
$TLBName=(Get-ItemProperty -Path $_.Pspath).'(Default)'
if ($TLBName -eq $null) { Write-Verbose \"Pas de nom pour la librairie : $TLBLibID\"}
$TLBVersion=$_.PSChildName
Write-Debug \"Version $TLBVersion\"
if (Test-Path \"$($_.Pspath)\HelpDir\"«»)
{$TLBHelpDir=(Get-ItemProperty -Path \"$($_.Pspath)\HelpDir\"«»).'(Default)'}
#Sinon pas de fichier d'aide pour la librairie

Dir $_.PSPath |
Foreach-Object {
$CurrentItem=$_
$CurrentVersion = New-TLBObject $TLBLibID $TLBName $TLBVersion $TLBHelpDir
Switch ($CurrentItem.PSChildName)
{
{$_ -match '^Flags$|^HelpDir$'} { continue }
default { # C'est une clé LCID
$CurrentVersion.LCID=$CurrentItem.PSChildName
$path=$CurrentItem.Pspath+'\Win32'

if (Test-Path $Path)
{
$CurrentVersion.is64Bit=$False
$CurrentVersion.Path=(Get-ItemProperty -Path $Path).'(Default)'
Write-Debug \"`tEmet l'objet TLB 32 bits. Lcid: $($CurrentVersion.LCID)\"
#Write-Debug \"`t$Currentversion\"
$CurrentVersion
}

$path=$CurrentItem.Pspath+'\Win64'
if (Test-Path $Path)
{
#Clone sans modifier la liste TypeNames
$NewTLBInfo= New-Object PsObject
$CurrentVersion.PSobject.Properties | % {
$NewTLBInfo | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
}
#Remplace System.Management.Automation.PSCustomObject
$NewTLBInfo.PsObject.TypeNames[0] = \"TypeLibInformation\"
$NewTLBInfo.is64Bit=$True
$NewTLBInfo.Path=(Get-ItemProperty -Path $Path).'(Default)'
Write-Debug \"`tEmet l'objet TLB 64 bits. Lcid: $($NewTLBInfo.LCID)\"
#Write-Debug \"`t$NewTLBInfo\"
$NewTLBInfo
}
}#Default
}#Switch
}#Foreach platform
}#Foreach Version
}#Foreach TLB
} #Get-TLBComInformation
[/code:1]
Un exemple d'utilisation :
[code:1]
$TLBs=Get-TLBComInformation

$IE=$TLBs|? {$_.LibID -eq 'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B'}
$IE=$TLBs|? {$_.Name -eq 'Microsoft Internet Controls'}
#Microsoft Internet Controls
$IE
# LCID : 0
# Version : 1.1
# Path : C:\Windows\System32\ieframe.dll
# is64Bit : False
# HelpDir : C:\Windows\System32
# Name : Microsoft Internet Controls
# LibID : eab22ac0-30c1-11cf-a7eb-0000c05bae0b

$ActiveDS=$TLBs|Where {$_.path -match 'Activeds'}
$ActiveDS
# LCID : 0
# Version : 1.0
# Path : C:\Windows\system32\activeds.tlb
# is64Bit : False
# HelpDir : C:\Program Files\Microsoft Office\Office14\
# Name : Active DS Type Library
# LibID : 97d25db0-0363-11cf-abc4-02608c9e7553
[/code:1]
Une recherche rapide d'après les informations de guid, de version, de langue et d'OS.
[code:1]
Function Search-TLBComInformation {
#Renvoi le nom complet du fichier contant la description d'une TLB
param(
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true,Position=1)]
[GUID]$GUID,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true,Position=2)]
[String]$Version,
[ValidateNotNullOrEmpty()]
[Parameter(Position=3)]
[String]$LCID='0',
[Switch]$Win64
)

$Platforms=@('Win32','Win64')
(Get-ItemProperty -Path \"Microsoft.PowerShell.Core\Registry::HKEY_CLASSES_ROOT\typelib\{$GUID}\$Version\$LCID\$($Platforms[$Win64.isPresent])\"«»).'(Default)'
} #Search-TLBComInformation

Search-TLBComInformation \"4fb2d46f-efc8-4643-bcd0-6e5bfa6a174c\" '2.0'

Search-TLBComInformation \"bed7f4ea-1a96-11d2-8f08-00a0c9a6186d\" '2.0' -Win64

Search-TLBComInformation '000c1092-0000-0000-c000-000000000046' '1.0' '409'
[/code:1]
Reste à construire le fichier interop à l'aide de l'utilitaire tlbImp.exe du SDK.
La suite ici .<br><br>Message édité par: Laurent Dardenne, à: 29/01/13 10:37

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.067 secondes
Propulsé par Kunena