Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# PSAPI_VERSION. and sevral other question [modified] [Partialy Solved]

C# PSAPI_VERSION. and sevral other question [modified] [Partialy Solved]

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studiosysadminwindows-admin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saksida Bojan
    wrote on last edited by
    #1

    In MSDN I found this for dll import used with PInvoke: Kernel32.dll on Windows 7 and Windows Server 2008 R2; Psapi.dll if PSAPI_VERSION=1 on Windows 7 and Windows Server 2008 R2; Psapi.dll on Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000 So how do i do programaticly find PSAPI_VERSION? I already Can determent windows version. But i am missing from the List Windwos 2008 and R2. I am doing the folowing way:

    public static EnumWinVer GetWindowsVersion()
    {

    OperatingSystem os = Environment.OSVersion;
    Version vs = os.Version;
    
    // This switch is pre-NT version
    if (os.Platform == PlatformID.Win32Windows)
    {
        switch (vs.Minor)
        {
            case 0:
                return EnumWinVer.Win95;
            case 10:
                if (vs.Revision.ToString() == "2222A")
                    return EnumWinVer.Win98SE;
                else
                    return EnumWinVer.Win98;
            case 90:
                return EnumWinVer.WinME;
        }
    }
    else if (os.Platform == PlatformID.Win32NT)
    {
        switch (vs.Major)
        {
            case 3:
                return EnumWinVer.WinNT3\_51;
            case 4:
                return EnumWinVer.WinNT4\_0;
            case 5:
                if (vs.Minor == 0)
                    return EnumWinVer.Win2000;
                else
                    return EnumWinVer.WinXP;
            case 6:
                if (vs.Minor == 0)
                    return EnumWinVer.WinVista;
                else
                    return EnumWinVer.Win7;
            default:
                break;
        }
    }
    
    // This is supposed to be unreachable area.
    return EnumWinVer.Undefined;
    

    }

    I am looking into GetModuleFileNameEx. QueryFullProcessImageName also can do the job. But minimum OS is Vista. So what would happen when Dllimport calls function that doesn't exsist on current OS. So how do i handle that. Thanks in advance for any answers to those question. Have a nice day edit:

    [DllImport("kernel32.dll", EntryPoint = "GetModuleFileNameEx")]
    static extern int K32GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, StringBuilder lpFilename, int nSize);

    [DllImport("psapi.dll", EntryPoint = "GetModuleFileNameEx")]
    static extern int PSAPIGetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, StringBuilder lpFilename, int nSize);

    After this i will Handle System.EntryPointNotFoundExpection. No

    B 1 Reply Last reply
    0
    • S Saksida Bojan

      In MSDN I found this for dll import used with PInvoke: Kernel32.dll on Windows 7 and Windows Server 2008 R2; Psapi.dll if PSAPI_VERSION=1 on Windows 7 and Windows Server 2008 R2; Psapi.dll on Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000 So how do i do programaticly find PSAPI_VERSION? I already Can determent windows version. But i am missing from the List Windwos 2008 and R2. I am doing the folowing way:

      public static EnumWinVer GetWindowsVersion()
      {

      OperatingSystem os = Environment.OSVersion;
      Version vs = os.Version;
      
      // This switch is pre-NT version
      if (os.Platform == PlatformID.Win32Windows)
      {
          switch (vs.Minor)
          {
              case 0:
                  return EnumWinVer.Win95;
              case 10:
                  if (vs.Revision.ToString() == "2222A")
                      return EnumWinVer.Win98SE;
                  else
                      return EnumWinVer.Win98;
              case 90:
                  return EnumWinVer.WinME;
          }
      }
      else if (os.Platform == PlatformID.Win32NT)
      {
          switch (vs.Major)
          {
              case 3:
                  return EnumWinVer.WinNT3\_51;
              case 4:
                  return EnumWinVer.WinNT4\_0;
              case 5:
                  if (vs.Minor == 0)
                      return EnumWinVer.Win2000;
                  else
                      return EnumWinVer.WinXP;
              case 6:
                  if (vs.Minor == 0)
                      return EnumWinVer.WinVista;
                  else
                      return EnumWinVer.Win7;
              default:
                  break;
          }
      }
      
      // This is supposed to be unreachable area.
      return EnumWinVer.Undefined;
      

      }

      I am looking into GetModuleFileNameEx. QueryFullProcessImageName also can do the job. But minimum OS is Vista. So what would happen when Dllimport calls function that doesn't exsist on current OS. So how do i handle that. Thanks in advance for any answers to those question. Have a nice day edit:

      [DllImport("kernel32.dll", EntryPoint = "GetModuleFileNameEx")]
      static extern int K32GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, StringBuilder lpFilename, int nSize);

      [DllImport("psapi.dll", EntryPoint = "GetModuleFileNameEx")]
      static extern int PSAPIGetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, StringBuilder lpFilename, int nSize);

      After this i will Handle System.EntryPointNotFoundExpection. No

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Why are you using unmanaged code? It seems that Process.MainModule.FileName will get this information without worrying about this sort of thing (which is the entire purpose of using a framework like .Net in the first place).

      S 1 Reply Last reply
      0
      • B BobJanova

        Why are you using unmanaged code? It seems that Process.MainModule.FileName will get this information without worrying about this sort of thing (which is the entire purpose of using a framework like .Net in the first place).

        S Offline
        S Offline
        Saksida Bojan
        wrote on last edited by
        #3

        Because I am gonna use WriteProcessMemory and ReadProcessMemory. Those calls does not support in .NET. I am creating a Memory Scanner. My personal project for hobby. I am creating similar to CheatEngine. The second thing is I want to learn PInvoke, even if it is harder.

        B 1 Reply Last reply
        0
        • S Saksida Bojan

          Because I am gonna use WriteProcessMemory and ReadProcessMemory. Those calls does not support in .NET. I am creating a Memory Scanner. My personal project for hobby. I am creating similar to CheatEngine. The second thing is I want to learn PInvoke, even if it is harder.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          Looks like you can get the parameters you need for Read/WriteProcessMemory (which is resident in one place) via Process.Handle and Process.MainModule.BaseAddress.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups