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. p/invoke _stat

p/invoke _stat

Scheduled Pinned Locked Moved C#
csharptutorialquestion
14 Posts 3 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.
  • D Offline
    D Offline
    DanielWehrle
    wrote on last edited by
    #1

    Hi, does anyone know how to call _stat with p/invoke from C#? it is posible to use File.Get* instead, but I need the direct call to investigate something on the file system. Thanks

    L 1 Reply Last reply
    0
    • D DanielWehrle

      Hi, does anyone know how to call _stat with p/invoke from C#? it is posible to use File.Get* instead, but I need the direct call to investigate something on the file system. Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Although this link[^] is not for _stat() it should help you figure it out.

      I must get a clever new signature for 2011.

      D 1 Reply Last reply
      0
      • L Lost User

        Although this link[^] is not for _stat() it should help you figure it out.

        I must get a clever new signature for 2011.

        D Offline
        D Offline
        DanielWehrle
        wrote on last edited by
        #3

        No sorry it is only fclose on the linkt, I check pinvoke.net before my post and did not find anything.

        L 1 Reply Last reply
        0
        • D DanielWehrle

          No sorry it is only fclose on the linkt, I check pinvoke.net before my post and did not find anything.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          DanielWehrle wrote:

          it is only fclose on the link

          Yes, I know that, but if you look at the sample you should be able to figure out how to code the call for the _stat() function.

          I must get a clever new signature for 2011.

          D 2 Replies Last reply
          0
          • L Lost User

            DanielWehrle wrote:

            it is only fclose on the link

            Yes, I know that, but if you look at the sample you should be able to figure out how to code the call for the _stat() function.

            I must get a clever new signature for 2011.

            D Offline
            D Offline
            DanielWehrle
            wrote on last edited by
            #5

            The pure _stat call is not my reral problem, but I have no idear how to define the struct containing the _stat result

            1 Reply Last reply
            0
            • L Lost User

              DanielWehrle wrote:

              it is only fclose on the link

              Yes, I know that, but if you look at the sample you should be able to figure out how to code the call for the _stat() function.

              I must get a clever new signature for 2011.

              D Offline
              D Offline
              DanielWehrle
              wrote on last edited by
              #6

              I try to do itz like: [StructLayout(LayoutKind.Sequential)] public struct STAT { public uint st_dev; public ushort st_ino; public ushort st_mode; public short st_nlink; public short st_uid; public short st_gid; public uint st_rdev; public long st_size; public long st_atime; public long st_mtime; public long st_ctime; } [DllImport("msvcrt.dll", SetLastError = true)] static extern int _stat(string file, ref STAT buf); But recive an PInvoke Imbalance Exception.

              T L 2 Replies Last reply
              0
              • D DanielWehrle

                I try to do itz like: [StructLayout(LayoutKind.Sequential)] public struct STAT { public uint st_dev; public ushort st_ino; public ushort st_mode; public short st_nlink; public short st_uid; public short st_gid; public uint st_rdev; public long st_size; public long st_atime; public long st_mtime; public long st_ctime; } [DllImport("msvcrt.dll", SetLastError = true)] static extern int _stat(string file, ref STAT buf); But recive an PInvoke Imbalance Exception.

                T Offline
                T Offline
                Tony Richards
                wrote on last edited by
                #7

                Have you checked the calling convention? The last time I got an error like this, this was the reason. Oh, and please use <pre></pre> to make your code clearer

                D 1 Reply Last reply
                0
                • D DanielWehrle

                  I try to do itz like: [StructLayout(LayoutKind.Sequential)] public struct STAT { public uint st_dev; public ushort st_ino; public ushort st_mode; public short st_nlink; public short st_uid; public short st_gid; public uint st_rdev; public long st_size; public long st_atime; public long st_mtime; public long st_ctime; } [DllImport("msvcrt.dll", SetLastError = true)] static extern int _stat(string file, ref STAT buf); But recive an PInvoke Imbalance Exception.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  DanielWehrle wrote:

                  But recive an PInvoke Imbalance Exception.

                  If you have an exception, it really is better if you post the exact text that you receive, rather than your interpretation of it. Also the line(s) of code that caused the exception; always included within <pre></pre> tags for readability.

                  I must get a clever new signature for 2011.

                  D 1 Reply Last reply
                  0
                  • T Tony Richards

                    Have you checked the calling convention? The last time I got an error like this, this was the reason. Oh, and please use <pre></pre> to make your code clearer

                    D Offline
                    D Offline
                    DanielWehrle
                    wrote on last edited by
                    #9

                    Yes but I'm not sure what the calling conventions for the _stat call are. All I know is from: http://msdn.microsoft.com/en-us/library/14h5k7ff%28vs.71%29.aspx

                    1 Reply Last reply
                    0
                    • L Lost User

                      DanielWehrle wrote:

                      But recive an PInvoke Imbalance Exception.

                      If you have an exception, it really is better if you post the exact text that you receive, rather than your interpretation of it. Also the line(s) of code that caused the exception; always included within <pre></pre> tags for readability.

                      I must get a clever new signature for 2011.

                      D Offline
                      D Offline
                      DanielWehrle
                      wrote on last edited by
                      #10

                      PInvokeStackImbalance
                      Message: A call to PInvoke function 'SharpFastStatWrite!SharpFastStatWrite.Writer::_stat' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

                      Thanks

                      L 2 Replies Last reply
                      0
                      • D DanielWehrle

                        PInvokeStackImbalance
                        Message: A call to PInvoke function 'SharpFastStatWrite!SharpFastStatWrite.Writer::_stat' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

                        Thanks

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Well this took me some time to figure out but I finally noticed that the function prototype for _stat is

                        int __cdecl _stat(_In_z_ const char * _Name, _Out_ struct _stat32 * _Stat);

                        and that __cdecl is not the standard calling convention. So I changed the DLLImport as follows

                        \[DllImport("msvcrt.dll", SetLastError = true, CallingConvention=CallingConvention.Cdecl)\]
                        

                        and it now works. Give it a try.

                        I must get a clever new signature for 2011.

                        1 Reply Last reply
                        0
                        • D DanielWehrle

                          PInvokeStackImbalance
                          Message: A call to PInvoke function 'SharpFastStatWrite!SharpFastStatWrite.Writer::_stat' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

                          Thanks

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Further information, the STAT structure should be as follows:

                          public struct STAT
                          {
                          public uint st_dev;
                          public ushort st_ino;
                          public ushort st_mode;
                          public short st_nlink;
                          public short st_uid;
                          public short st_gid;
                          public uint st_rdev;
                          public uint st_size;
                          public uint st_atime;
                          public uint st_mtime;
                          public uint st_ctime;
                          }

                          Note the last four entries are uint rather than long values.

                          I must get a clever new signature for 2011.

                          D 1 Reply Last reply
                          0
                          • L Lost User

                            Further information, the STAT structure should be as follows:

                            public struct STAT
                            {
                            public uint st_dev;
                            public ushort st_ino;
                            public ushort st_mode;
                            public short st_nlink;
                            public short st_uid;
                            public short st_gid;
                            public uint st_rdev;
                            public uint st_size;
                            public uint st_atime;
                            public uint st_mtime;
                            public uint st_ctime;
                            }

                            Note the last four entries are uint rather than long values.

                            I must get a clever new signature for 2011.

                            D Offline
                            D Offline
                            DanielWehrle
                            wrote on last edited by
                            #13

                            Thanks, now it work.

                            L 1 Reply Last reply
                            0
                            • D DanielWehrle

                              Thanks, now it work.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              You're welcome; perhaps you would be so kind as to vote my answer (the one with the solution)?

                              I must get a clever new signature for 2011.

                              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