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

Advertisements
This entry was posted in PowerShell 2.0. Bookmark the permalink.

2 Responses to PowerShell – Find-PInvoke.ps1

  1. huy says:

    Find-Pinvoke link is broken (((((

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s