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. Other Discussions
  3. Clever Code
  4. A P/Invoke mystery [modified]

A P/Invoke mystery [modified]

Scheduled Pinned Locked Moved Clever Code
csharpc++cssdata-structuresperformance
9 Posts 6 Posters 22 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.
  • T Offline
    T Offline
    Tsuda Kageyu
    wrote on last edited by
    #1

    A few days ago, I looked through a C# program that didn't run well on x64 system. Finally I fixed it, but faced a mysterious fact. First, I had suspected that its P/Invoke declarations are wrong, and found the one like this.

    // MEMORY_BASIC_INFORMATION is defined separately.
    [DllImport("kernel32.dll")]
    static extern int VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);

    I thought it was surely incorrect. The native definition of the function is like this...

    SIZE_T WINAPI VirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);

    ...and SIZE_T is platform dependent, so that its corresponding P/Invoke declaration might be like this.

    [DllImport("kernel32.dll")]
    static extern IntPtr VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, IntPtr dwLength);

    But the thing confused me is that the function itseif seemd to run well even on x64 system. I think that 'incorrect' one runs well because dwLength is passed with a register (not stack) on x64 system. If the parameter is 64bit or less, it would be passed apparently. Does somebody have an idea about it?

    modified on Monday, August 2, 2010 10:07 PM

    E N C 3 Replies Last reply
    0
    • T Tsuda Kageyu

      A few days ago, I looked through a C# program that didn't run well on x64 system. Finally I fixed it, but faced a mysterious fact. First, I had suspected that its P/Invoke declarations are wrong, and found the one like this.

      // MEMORY_BASIC_INFORMATION is defined separately.
      [DllImport("kernel32.dll")]
      static extern int VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);

      I thought it was surely incorrect. The native definition of the function is like this...

      SIZE_T WINAPI VirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);

      ...and SIZE_T is platform dependent, so that its corresponding P/Invoke declaration might be like this.

      [DllImport("kernel32.dll")]
      static extern IntPtr VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, IntPtr dwLength);

      But the thing confused me is that the function itseif seemd to run well even on x64 system. I think that 'incorrect' one runs well because dwLength is passed with a register (not stack) on x64 system. If the parameter is 64bit or less, it would be passed apparently. Does somebody have an idea about it?

      modified on Monday, August 2, 2010 10:07 PM

      E Offline
      E Offline
      Electron Shepherd
      wrote on last edited by
      #2

      Tsuda Kageyu wrote:

      because dwLength is passed with a register (not stack) on x64 system.

      That contradicts the WINAPI calling convention in the declaration, so I'd be surprised if that was the case.

      Server and Network Monitoring

      T 1 Reply Last reply
      0
      • E Electron Shepherd

        Tsuda Kageyu wrote:

        because dwLength is passed with a register (not stack) on x64 system.

        That contradicts the WINAPI calling convention in the declaration, so I'd be surprised if that was the case.

        Server and Network Monitoring

        T Offline
        T Offline
        Tsuda Kageyu
        wrote on last edited by
        #3

        Electron Shepherd wrote:

        That contradicts the WINAPI calling convention

        I think it's not a contradiction. The calling convention on x64 system is different from the ones on x86 system. Here is the overview about it. http://msdn.microsoft.com/en-us/library/ms235286%28VS.80%29.aspx[^]

        E R 2 Replies Last reply
        0
        • T Tsuda Kageyu

          Electron Shepherd wrote:

          That contradicts the WINAPI calling convention

          I think it's not a contradiction. The calling convention on x64 system is different from the ones on x86 system. Here is the overview about it. http://msdn.microsoft.com/en-us/library/ms235286%28VS.80%29.aspx[^]

          E Offline
          E Offline
          Electron Shepherd
          wrote on last edited by
          #4

          Tsuda Kageyu wrote:

          The calling convention on x64 system is different from the ones on x86 system.

          Interesting. I didn't know that - thanks.

          Server and Network Monitoring

          1 Reply Last reply
          0
          • T Tsuda Kageyu

            Electron Shepherd wrote:

            That contradicts the WINAPI calling convention

            I think it's not a contradiction. The calling convention on x64 system is different from the ones on x86 system. Here is the overview about it. http://msdn.microsoft.com/en-us/library/ms235286%28VS.80%29.aspx[^]

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            Good piece of information. Not doing anything with 64-bits yet, but I'll tuck this away in memory for when that day comes.

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

            1 Reply Last reply
            0
            • T Tsuda Kageyu

              A few days ago, I looked through a C# program that didn't run well on x64 system. Finally I fixed it, but faced a mysterious fact. First, I had suspected that its P/Invoke declarations are wrong, and found the one like this.

              // MEMORY_BASIC_INFORMATION is defined separately.
              [DllImport("kernel32.dll")]
              static extern int VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);

              I thought it was surely incorrect. The native definition of the function is like this...

              SIZE_T WINAPI VirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);

              ...and SIZE_T is platform dependent, so that its corresponding P/Invoke declaration might be like this.

              [DllImport("kernel32.dll")]
              static extern IntPtr VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, IntPtr dwLength);

              But the thing confused me is that the function itseif seemd to run well even on x64 system. I think that 'incorrect' one runs well because dwLength is passed with a register (not stack) on x64 system. If the parameter is 64bit or less, it would be passed apparently. Does somebody have an idea about it?

              modified on Monday, August 2, 2010 10:07 PM

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              Was the C# caller compiled as 32 bit by any chance?

              Regards, Nish


              Blog: blog.voidnish.com Most recent article: An MVVM friendly approach to adding system menu entries in a WPF application

              T 1 Reply Last reply
              0
              • N Nish Nishant

                Was the C# caller compiled as 32 bit by any chance?

                Regards, Nish


                Blog: blog.voidnish.com Most recent article: An MVVM friendly approach to adding system menu entries in a WPF application

                T Offline
                T Offline
                Tsuda Kageyu
                wrote on last edited by
                #7

                No. I made doubly sure that the process was running in x64 mode. By the way, your articles have helped me much for years. Thanks :-D

                1 Reply Last reply
                0
                • T Tsuda Kageyu

                  A few days ago, I looked through a C# program that didn't run well on x64 system. Finally I fixed it, but faced a mysterious fact. First, I had suspected that its P/Invoke declarations are wrong, and found the one like this.

                  // MEMORY_BASIC_INFORMATION is defined separately.
                  [DllImport("kernel32.dll")]
                  static extern int VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);

                  I thought it was surely incorrect. The native definition of the function is like this...

                  SIZE_T WINAPI VirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);

                  ...and SIZE_T is platform dependent, so that its corresponding P/Invoke declaration might be like this.

                  [DllImport("kernel32.dll")]
                  static extern IntPtr VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, IntPtr dwLength);

                  But the thing confused me is that the function itseif seemd to run well even on x64 system. I think that 'incorrect' one runs well because dwLength is passed with a register (not stack) on x64 system. If the parameter is 64bit or less, it would be passed apparently. Does somebody have an idea about it?

                  modified on Monday, August 2, 2010 10:07 PM

                  C Offline
                  C Offline
                  Code Master38
                  wrote on last edited by
                  #8

                  Yeah really weird

                  P 1 Reply Last reply
                  0
                  • C Code Master38

                    Yeah really weird

                    P Offline
                    P Offline
                    Paw Jershauge
                    wrote on last edited by
                    #9

                    Always use System.Runtime.InteropServices.Marshal.SizeOf method to ensure platform independency.

                    With great code, comes great complexity, so keep it simple stupid...:-\ :-\

                    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