473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(
[MarshalAs(UnmanagedType.LPTStr)] string lpDirectoryName,
[MarshalAs(UnmanagedType.U8)] long lpFreeBytesAvailable,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfBytes,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfFreeBytes);
}

protected void GetFreeSpace()
{
long lFreeBytes = 0;
long lTotBytes = 0;
long lTotFreeBytes = 0;
string sDir = "C:\\";
bool bOk = WinApi.GetDiskFreeSpaceEx(sDir, lFreeBytes, lTotBytes,
lTotFreeBytes);
if (bOk)
Console.Write(lFreeBytes.ToString());
}
Nov 15 '05 #1
3 1449
John,

Import the System.ComponentModel namespace and then do this:

if (bOk)
Console.Write(lFreeBytes.ToString());
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]
- ni**************@exisconsulting.com

"John Cantley" <jc*******@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP12.phx.gbl...
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("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(
[MarshalAs(UnmanagedType.LPTStr)] string lpDirectoryName,
[MarshalAs(UnmanagedType.U8)] long lpFreeBytesAvailable,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfBytes,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfFreeBytes);
}

protected void GetFreeSpace()
{
long lFreeBytes = 0;
long lTotBytes = 0;
long lTotFreeBytes = 0;
string sDir = "C:\\";
bool bOk = WinApi.GetDiskFreeSpaceEx(sDir, lFreeBytes, lTotBytes,
lTotFreeBytes);
if (bOk)
Console.Write(lFreeBytes.ToString());
}

Nov 15 '05 #2
John,

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

[MarshalAs(UnmanagedType.U8)] ref long lpFreeBytesAvailable,
....

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

Willy.

"John Cantley" <jc*******@hotmail.com> wrote in message news:e0**************@TK2MSFTNGP12.phx.gbl...
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("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(
[MarshalAs(UnmanagedType.LPTStr)] string lpDirectoryName,
[MarshalAs(UnmanagedType.U8)] long lpFreeBytesAvailable,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfBytes,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfFreeBytes);
}

protected void GetFreeSpace()
{
long lFreeBytes = 0;
long lTotBytes = 0;
long lTotFreeBytes = 0;
string sDir = "C:\\";
bool bOk = WinApi.GetDiskFreeSpaceEx(sDir, lFreeBytes, lTotBytes,
lTotFreeBytes);
if (bOk)
Console.Write(lFreeBytes.ToString());
}

Nov 15 '05 #3
Thanks

jc
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: Tom | last post by:
Hi Everybod I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
14
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
23
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.