P/Invoke ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Cantley

    #1

    P/Invoke ?

    Here is what I have, I am not getting back any values all are returned as
    zero. But it seems to me that the call is not running at all. what am i
    doing wrong or missing?

    public class WinApi
    {
    [DllImport("kern el32.dll", SetLastError=tr ue, CharSet=CharSet .Auto)]
    public static extern bool GetDiskFreeSpac eEx(
    [MarshalAs(Unman agedType.LPTStr )] string lpDirectoryName ,
    [MarshalAs(Unman agedType.U8)] long lpFreeBytesAvai lable,
    [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf Bytes,
    [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf FreeBytes);
    }

    protected void GetFreeSpace()
    {
    long lFreeBytes = 0;
    long lTotBytes = 0;
    long lTotFreeBytes = 0;
    string sDir = "C:\\";
    bool bOk = WinApi.GetDiskF reeSpaceEx(sDir , lFreeBytes, lTotBytes,
    lTotFreeBytes);
    if (bOk)
    Console.Write(l FreeBytes.ToStr ing());
    }


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: P/Invoke ?

    John,

    Import the System.Componen tModel namespace and then do this:

    if (bOk)
    Console.Write(l FreeBytes.ToStr ing());
    else
    throw new Win32Exception( );

    This will throw an exception with the information why the call failed
    (if it did).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "John Cantley" <jcstrider@hotm ail.com> wrote in message
    news:e0gnLW6eDH A.2348@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Here is what I have, I am not getting back any values all are returned as
    > zero. But it seems to me that the call is not running at all. what am i
    > doing wrong or missing?
    >
    > public class WinApi
    > {
    > [DllImport("kern el32.dll", SetLastError=tr ue, CharSet=CharSet .Auto)]
    > public static extern bool GetDiskFreeSpac eEx(
    > [MarshalAs(Unman agedType.LPTStr )] string lpDirectoryName ,
    > [MarshalAs(Unman agedType.U8)] long lpFreeBytesAvai lable,
    > [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf Bytes,
    > [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf FreeBytes);
    > }
    >
    > protected void GetFreeSpace()
    > {
    > long lFreeBytes = 0;
    > long lTotBytes = 0;
    > long lTotFreeBytes = 0;
    > string sDir = "C:\\";
    > bool bOk = WinApi.GetDiskF reeSpaceEx(sDir , lFreeBytes, lTotBytes,
    > lTotFreeBytes);
    > if (bOk)
    > Console.Write(l FreeBytes.ToStr ing());
    > }
    >
    >[/color]


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: P/Invoke ?

      John,

      You should pass a "pointer to a long" for the out arguments in both, the declaration and the call.

      [MarshalAs(Unman agedType.U8)] ref long lpFreeBytesAvai lable,
      ....

      bool bOk = WinApi.GetDiskF reeSpaceEx(sDir , ref lFreeBytes, ref lTotBytes,
      ref lTotFreeBytes);

      Willy.

      "John Cantley" <jcstrider@hotm ail.com> wrote in message news:e0gnLW6eDH A.2348@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Here is what I have, I am not getting back any values all are returned as
      > zero. But it seems to me that the call is not running at all. what am i
      > doing wrong or missing?
      >
      > public class WinApi
      > {
      > [DllImport("kern el32.dll", SetLastError=tr ue, CharSet=CharSet .Auto)]
      > public static extern bool GetDiskFreeSpac eEx(
      > [MarshalAs(Unman agedType.LPTStr )] string lpDirectoryName ,
      > [MarshalAs(Unman agedType.U8)] long lpFreeBytesAvai lable,
      > [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf Bytes,
      > [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf FreeBytes);
      > }
      >
      > protected void GetFreeSpace()
      > {
      > long lFreeBytes = 0;
      > long lTotBytes = 0;
      > long lTotFreeBytes = 0;
      > string sDir = "C:\\";
      > bool bOk = WinApi.GetDiskF reeSpaceEx(sDir , lFreeBytes, lTotBytes,
      > lTotFreeBytes);
      > if (bOk)
      > Console.Write(l FreeBytes.ToStr ing());
      > }
      >
      >[/color]


      Comment

      • John Cantley

        #4
        Re: P/Invoke ?

        Thanks

        jc


        Comment

        Working...