 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Mon Jun 08, 2009 6:55 pm Post subject: [C] Viewing hard drive space |
|
|
This should read the remaining free & total space in your hard drive in terms of bytes. Also, I've 'adopted' a few new coding practices because in my professional (not so much) opinion they look cool.
| Code: | #include <windows.h>
#include <tchar.h>
#include <stdio.h>
int __cdecl _tmain(__in int argc, __in_ecount_z(argc) TCHAR* argv[], __in_z TCHAR* envp[])
{
DWORD dwAvailableBytes = 0;
DWORD dwTotalBytes = 0;
DWORD dwFreeBytes = 0;
if(!GetDiskFreeSpaceEx(_T("C:\\"), (PULARGE_INTEGER)&AvailableBytes,
(PULARGE_INTEGER)&TotalBytes, (PULARGE_INTEGER)&FreeBytes))
{
DWORD dwSectorsPerCluster = 0;
DWORD dwBytesPerSector = 0;
DWORD dwFreeClusters = 0;
DWORD dwTotalClusters = 0;
if(!GetDiskFreeSpace(_T("C:\\"), &dwSectorsPerCluster, BytesPerSector,
&FreeClusters, &TotalClusters))
{
_tprintf(_T("Couldn't get free disk space.."));
return EXIT_FAILURE;
}
dwFreeBytes = dwFreeClusters * dwSectorsPerCluster * dwBytesPerSector;
dwTotalBytes = dwTotalClusters * dwSectorsPerCluster * dwBytesPerSector;
}
_tprintf(_T("You have %d out of %d bytes of free space remaining in your hard drive."), dwFreeBytes, dwTotalBytes);
return EXIT_SUCCESS;
} |
Sorry, wrong section. Mods, move please?
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Jun 08, 2009 7:27 pm Post subject: |
|
|
Moved
also Hungarian notation is gross
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Jun 08, 2009 7:55 pm Post subject: |
|
|
Not so much C, as in C you have to define all variables in a function before any instruction is executed, also you could shorten all those definitions by using commas.
Oh and also i'm pretty sure stdio.h isn't needed here...
And slovach, lets not hate on Hungarian notation, I like it.
PS. You should check to see if C:\ is their main partition first Maybe enumerate all the partitions?
_________________
|
|
| Back to top |
|
 |
dnsi0 I post too much
Reputation: 0
Joined: 04 Jan 2007 Posts: 2674
|
Posted: Mon Jun 08, 2009 8:05 pm Post subject: |
|
|
| lurc wrote: | Not so much C, as in C you have to define all variables in a function before any instruction is executed, also you could shorten all those definitions by using commas.
Oh and also i'm pretty sure stdio.h isn't needed here...
And slovach, lets not hate on Hungarian notation, I like it.
PS. You should check to see if C:\ is their main partition first Maybe enumerate all the partitions? |
Um... what Hungarian notations?
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Mon Jun 08, 2009 9:00 pm Post subject: |
|
|
SAL totally makes you look cooler.
Either you ripped someone's source out or you didn't compile your source. Look at it.
| Code: | if(!GetDiskFreeSpaceEx(_T("C:\\"), (PULARGE_INTEGER)&AvailableBytes,
(PULARGE_INTEGER)&TotalBytes, (PULARGE_INTEGER)&FreeBytes))
|
| Code: | DWORD dwAvailableBytes = 0;
DWORD dwTotalBytes = 0;
DWORD dwFreeBytes = 0;
|
There were some more errors in your code(misspelling variables & some other stuff).
Here's the C version, that I've coded.
| Code: | #include <stdio.h>
#include <tchar.h>
#include <windows.h>
int main( )
{
DWORD dwAvailableBytes, dwTotalBytes, dwFreeBytes;
if(!GetDiskFreeSpaceEx(_T("C:\\"), (PULARGE_INTEGER)&dwAvailableBytes, (PULARGE_INTEGER)&dwTotalBytes, (PULARGE_INTEGER)&dwFreeBytes))
{
DWORD dwSectorPerCluster, dwBytesPerSector, dwFreeClusters, dwTotalClusters;
if(!GetDiskFreeSpace(_T("C:\\"), &dwSectorPerCluster, &dwBytesPerSector, &dwFreeClusters, &dwTotalClusters))
return puts("GetDiskFreeSpace returned false!");
dwFreeBytes = dwFreeClusters * dwSectorPerCluster * dwBytesPerSector;
dwTotalBytes = dwTotalClusters * dwSectorPerCluster * dwBytesPerSector;
}
printf("[C:/] HD: %d Bytes / %d Bytes\n", (int)dwFreeBytes, (int)dwTotalBytes);
fflush(stdout);
fflush(stdin);
return getchar();
} |
Could I ask you what's the point of using _tprintf instead of printf?
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Jun 08, 2009 9:51 pm Post subject: |
|
|
| unicode
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Tue Jun 09, 2009 3:14 pm Post subject: |
|
|
| : wrote: | | SAL totally makes you look cooler. |
No they make the code more understandable.
| : wrote: | Here's the C version, that I've coded.
| Code: | #include <stdio.h>
#include <tchar.h>
#include <windows.h>
int main( )
{
DWORD dwAvailableBytes, dwTotalBytes, dwFreeBytes;
if(!GetDiskFreeSpaceEx(_T("C:\\"), (PULARGE_INTEGER)&dwAvailableBytes, (PULARGE_INTEGER)&dwTotalBytes, (PULARGE_INTEGER)&dwFreeBytes))
{
DWORD dwSectorPerCluster, dwBytesPerSector, dwFreeClusters, dwTotalClusters;
if(!GetDiskFreeSpace(_T("C:\\"), &dwSectorPerCluster, &dwBytesPerSector, &dwFreeClusters, &dwTotalClusters))
return puts("GetDiskFreeSpace returned false!");
dwFreeBytes = dwFreeClusters * dwSectorPerCluster * dwBytesPerSector;
dwTotalBytes = dwTotalClusters * dwSectorPerCluster * dwBytesPerSector;
}
printf("[C:/] HD: %d Bytes / %d Bytes\n", (int)dwFreeBytes, (int)dwTotalBytes);
fflush(stdout);
fflush(stdin);
return getchar();
} |
|
Still won't compile as C Code...
You have seemed to completely miss what i wrote above..
Also to both of you, for GetDiskFreeSpaceEx you cannot just cast the PULARGE_INTEGER parameters as DWORD's. MSDN specifically states this:
| MSDN wrote: | Remarks
The values obtained by this function are of the type ULARGE_INTEGER. Do not truncate these values to 32 bits. |
It seems neither of you know true C.
Here is what it should look like:
| Code: | #include <Windows.h>
#include <Tchar.h>
#define MEGABYTE 1048576 // 1024 x 1024
#define GIGABYTE 1073741824 // 1024 x 1024 x 1024
_TINT _tmain(__in _TINT argc, __in_ecount_z(argc) LPTSTR argv[], __in_z LPTSTR envp[])
{
BOOL bSUCCESS;
LPTSTR lpszMessages;
PULARGE_INTEGER lpuliAvailable, lpuliTotal, lpuliFree;
lpuliAvailable = (PULARGE_INTEGER)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ULARGE_INTEGER));
if (lpuliAvailable != NULL)
{
lpuliTotal = (PULARGE_INTEGER)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ULARGE_INTEGER));
if (lpuliTotal != NULL)
{
lpuliFree = (PULARGE_INTEGER)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ULARGE_INTEGER));
if (lpuliFree != NULL)
{
bSUCCESS = GetDiskFreeSpaceEx(NULL, lpuliAvailable, lpuliTotal, lpuliFree);
if (bSUCCESS)
{
lpszMessages = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(TCHAR)*512);
if (lpszMessages != NULL)
{
if (wsprintf(lpszMessages, _T("Total Size:\t\t\t%u GB\r\nTotal Available To Caller:\t%u GB\r\nTotal Free:\t\t\t%u GB\r\n\r\nSizes are rounded to the nearest gigabyte.\r\n"),
lpuliTotal->QuadPart / GIGABYTE , lpuliAvailable->QuadPart / GIGABYTE, lpuliFree->QuadPart / GIGABYTE) > 0)
{
_tprintf(lpszMessages);
}
HeapFree(GetProcessHeap(), 0, (LPVOID)lpszMessages);
}
}
HeapFree(GetProcessHeap(), 0, (LPVOID)lpuliFree);
}
HeapFree(GetProcessHeap(), 0, (LPVOID)lpuliTotal);
}
HeapFree(GetProcessHeap(), 0, (LPVOID)lpuliAvailable);
}
_gettchar();
return 0;
} |
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Jun 09, 2009 3:42 pm Post subject: |
|
|
SAL = more understandable?
Undoubtedly uglier, at the cost of some pretty compile time information as to if your code is going to blow up. Needless to say, it strikes me as something the compiler should deal with (and hopefully will) in the future.
Same with hungarian notation, the compiler knows what type it is. I guess it's good at complicating things for whoever is reading it though.
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Wed Jun 10, 2009 2:07 pm Post subject: |
|
|
| It compiled on my C compiler perfectly.
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Jun 10, 2009 2:43 pm Post subject: |
|
|
| slovach wrote: | SAL = more understandable?
Undoubtedly uglier, at the cost of some pretty compile time information as to if your code is going to blow up. Needless to say, it strikes me as something the compiler should deal with (and hopefully will) in the future.
Same with hungarian notation, the compiler knows what type it is. I guess it's good at complicating things for whoever is reading it though. |
hungarian notation i do agree is not very useful in strong typed languages. but in something like assembly i do use it so i think it has its uses but shouldn't be mindlessly applied to everything
|
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Wed Jun 10, 2009 5:02 pm Post subject: |
|
|
| Hungarian notation helps me remember which variable is which; for example, I could get DelayTime and DelayInterval mixed up, but dwDelayTime and hDelayInterval leave me no doubt as to which is which.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|