p/invoke _stat
-
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
-
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
-
No sorry it is only fclose on the linkt, I check pinvoke.net before my post and did not find anything.
-
No sorry it is only fclose on the linkt, I check pinvoke.net before my post and did not find anything.
-
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.
The pure _stat call is not my reral problem, but I have no idear how to define the struct containing the _stat result
-
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.
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.
-
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.
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
-
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.
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.
-
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
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
-
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.
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
-
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
Well this took me some time to figure out but I finally noticed that the function prototype for
_stat
isint __cdecl _stat(_In_z_ const char * _Name, _Out_ struct _stat32 * _Stat);
and that
__cdecl
is not the standard calling convention. So I changed theDLLImport
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.
-
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
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 thanlong
values.I must get a clever new signature for 2011.
-
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 thanlong
values.I must get a clever new signature for 2011.
Thanks, now it work.
-
Thanks, now it work.