Keith's profileKeith Hill's BlogPhotosBlogListsMore Tools Help

Blog


    October 30

    PowerShell - Find-PInvoke.ps1

    I had a need to find all PInvokes in an assembly.  At first I thought about searching the source code since I had it but ran into various problems:

    • Commented out PInvokes
    • DLL names specified via constants

    [Updated: 11-02-2009] I decided to go with the "reflection only load" approach to extract the PInvoke (DllImport) information.  The script file has been updated to reflect (pun intended) this new and better approach.  In fact, more data is gathered for each PInvoke including all of the values for the associated DllImportAttribute.  Now to see if I can get this included in the next version of PSCX.   It seems that the reflection only load is sensitive to assembly load failures.  Once an assembly fails to load you pretty much have to start a new PowerShell session to try again.  I'll keep looking into this.  For what appears to be a really neat capability (ReflectionOnlyLoad) it has some unfortunate short-comings.

    Here is some sample output when run on the PSCX snapin dlls.

    PS> gci C:\Users\Keith\Pscx\Trunk\Src\Pscx\bin\Debug\Pscx*.dll | .\Find-PInvoke |
            Sort DllName,AssemblyName,MethodName | ft AssemblyName,MethodName,TypeName -groupby DllName -auto

       DllImport: advapi32.dll

    Assembly      MethodName            TypeName                       
    --------      ----------            --------                       
    Pscx.Core.dll AdjustTokenPrivileges Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetTokenInformation   Pscx.Interop.UnsafeNativeMethods
    Pscx.Core.dll LogonUser             Pscx.Interop.NativeMethods     
    Pscx.Core.dll LookupPrivilegeName   Pscx.Interop.NativeMethods     
    Pscx.Core.dll LookupPrivilegeValue  Pscx.Interop.NativeMethods     
    Pscx.Core.dll OpenProcessToken      Pscx.Interop.NativeMethods     
    Pscx.Core.dll OpenThreadToken       Pscx.Interop.NativeMethods     

       DllImport: Fusion.dll

    Assembly      MethodName                 TypeName                 
    --------      ----------                 --------                 
    Pscx.Core.dll CreateAssemblyCache        Pscx.Interop.NativeMethods
    Pscx.Core.dll CreateAssemblyEnum         Pscx.Interop.NativeMethods
    Pscx.Core.dll CreateAssemblyNameObject   Pscx.Interop.NativeMethods
    Pscx.Core.dll CreateInstallReferenceEnum Pscx.Interop.NativeMethods
    Pscx.Core.dll GetCachePath               Pscx.Interop.NativeMethods

       DllImport: kernel32.dll

    Assembly      MethodName                       TypeName                       
    --------      ----------                       --------                       
    Pscx.Core.dll CloseHandle                      Pscx.Interop.NativeMethods     
    Pscx.Core.dll CreateConsoleScreenBuffer        Pscx.Interop.NativeMethods     
    Pscx.Core.dll CreateFile                       Pscx.Interop.NativeMethods     
    Pscx.Core.dll CreateHardLink                   Pscx.Interop.NativeMethods     
    Pscx.Core.dll CreateSymbolicLink               Pscx.Interop.NativeMethods     
    Pscx.Core.dll DeleteFile                       Pscx.Interop.NativeMethods     
    Pscx.Core.dll DeleteVolumeMountPoint           Pscx.Interop.NativeMethods     
    Pscx.Core.dll DeviceIoControl                  Pscx.Interop.UnsafeNativeMethods
    Pscx.Core.dll FindFirstVolumeMountPoint        Pscx.Interop.NativeMethods     
    Pscx.Core.dll FindNextVolumeMountPoint         Pscx.Interop.NativeMethods     
    Pscx.Core.dll FindVolumeMountPointClose        Pscx.Interop.NativeMethods     
    Pscx.Core.dll FreeLibrary                      Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetConsoleFontSize               Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetConsoleInputMode              Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetConsoleOutputMode             Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetConsoleWindow                 Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetCurrentConsoleFont            Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetCurrentProcess                Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetModuleHandle                  Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetProcAddress                   Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetProcAddress                   Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetShortPathName                 Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetStdHandle                     Pscx.Interop.NativeMethods     
    Pscx.Core.dll GetVolumeNameForVolumeMountPoint Pscx.Interop.NativeMethods     
    Pscx.Core.dll IsWow64Process                   Pscx.Interop.NativeMethods     
    Pscx.Core.dll LoadLibrary                      Pscx.Interop.NativeMethods     
    Pscx.Core.dll ReadConsoleInput                 Pscx.Interop.NativeMethods     
    Pscx.Core.dll ReadFile                         Pscx.Interop.NativeMethods     
    Pscx.Core.dll ReadFile                         Pscx.Interop.NativeMethods     
    Pscx.Core.dll RemoveDirectory                  Pscx.Interop.NativeMethods     
    Pscx.Core.dll SetConsoleActiveScreenBuffer     Pscx.Interop.NativeMethods     
    Pscx.dll      __SetVolumeLabel                 Pscx.Commands.IO.SetVolumeLabel

    October 29

    PowerShell 2.0 – Accessing Different Profiles

    Just ran across a new feature of PowerShell 2.0 that is convenient.  Need the path to the various profile scripts on a system?  Try this:

    PS> $profile.psextended | Format-List

    AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
    AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
    CurrentUserAllHosts    : C:\Users\Keith\Documents\WindowsPowerShell\profile.ps1
    CurrentUserCurrentHost : C:\Users\Keith\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

    To access individually you can use $profile.AllUsersAllHosts.

    psmdtag:variable Profile