You are on page 1of 7

==============================ModuleA===================================== The program examples' source codes have been arranged in the same order that appeared in the

Tutorial. This is unedited and unverified compilation. Published as is basis for educational, reacretional and brain teaser purposes. All trademarks, copyrights and IPs, wherever exist, are the sole property of their respective owner and/or holder. Any damage or loss by using the materials presented in this tutorial is USER responsibility. Part or full distribution, reproduction and modification is granted to any body. Copyright 2003-2005 Tenouk, Inc. All rights reserved. Distributed through http://www.tenouk.com

=========================================================================== Compiled using VC++/VC++ .Net.....unmanaged... #include #include #include #include <windows.h> <direct.h> <stdio.h> <tchar.h>

//Buffer, be careful with terminated NULL //Must match with ++mydrives[1]...that is one space //Example if no one space: "A:"--> ++mydrives[0]; TCHAR mydrives[] = " A: "; //Or char mydrives[] = {" A: "}; //Or char mydrives[] = " A: "; int main() { //Get the drives bit masks...1 is available, 0 is not available //A = least significant bit... ULONG DriveMask = _getdrives(); //If something wrong if(DriveMask == 0) printf("_getdrives() failed with failure code: %d\n", GetLastError()); else { printf("This machine has the following logical drives:\n"); while (DriveMask) { //List all the drives... if(DriveMask & 1) printf(mydrives); //Go to the next drive strings with one space ++mydrives[1]; //Shift the bit masks binary //to the right and repeat DriveMask >>= 1; } printf("\n"); } return 0; }

---------------------------------------------------------------------------------------------#include #include #include #include <windows.h> <direct.h> <stdio.h> <tchar.h>

TCHAR g_szText[] = _T("Drive Total_clus Available_clus Sec/Cluster Bytes/Sec\n" ); TCHAR g_szText1[] = _T("----- ---------- -------------- ----------- ---------\n" ); TCHAR g_szInfo[] = _T("-> \n" ); //For data display format... //Right justified, thousand comma separated and other format //for displayed data void utoiRightJustified(TCHAR* szLeft, TCHAR* szRight, unsigned uValue) { TCHAR* szCur = szRight; int nComma = 0; if(uValue) { while(uValue && (szCur >= szLeft)) { if(nComma == 3) { *szCur = ','; nComma = 0; } else { *szCur = (uValue % 10) 0x30; uValue /= 10; ++nComma; } --szCur; } } else { *szCur = '0'; --szCur; } if(uValue) { szCur = szLeft; while(szCur <= szRight) {//If not enough field to display the data... *szCur = '*'; ++szCur; } } } int main() {

TCHAR szMsg[4200]; struct _diskfree_t df = {0}; //Search drives and assigns the bit masks to //uDriveMask variable... ULONG uDriveMask = _getdrives(); unsigned uErr, uLen, uDrive; printf("clus - cluster, sec - sector\n"); printf(g_szText); printf(g_szText1); for(uDrive = 1; uDrive <= 26; ++uDrive) { //If the drive is available... if(uDriveMask & 1) { //Call _getdiskfree()... uErr = _getdiskfree(uDrive, &df); //Provide some storage memcpy(szMsg, g_szInfo, sizeof(g_szInfo)); szMsg[3] = uDrive + 'A' - 1; //If _getdiskfree() is no error, display the data if(uErr == 0) { utoiRightJustified(szMsg+4, szMsg+15, df.total_clusters); utoiRightJustified(szMsg+18, szMsg+29, df.avail_clusters); utoiRightJustified(szMsg+27, szMsg+37, df.sectors_per_cluster); utoiRightJustified(szMsg+40, szMsg+50, df.bytes_per_sector); } else {//Print system message and left other fields empty uLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, uErr, 0, szMs g+8, 4100, NULL); szMsg[uLen+6] = ' '; szMsg[uLen+7] = ' '; szMsg[uLen+8] = ' '; } printf(szMsg); } //shift right the found drive bit masks and //repeat the process uDriveMask >>= 1; } return 0; } ------------------------------------------------------------------------------------------------------/*******cruntime.cpp*******/ /***Visual C++ .Net/7.0****/ #include <stdio.h> #include <conio.h> #include <direct.h> #include <stdlib.h> #include <ctype.h> int main(void) { int chr, drive, curdrive;

static char path[_MAX_PATH]; char buffer[_MAX_PATH]; char newdir[50] = "\\testdir"; char path1[50] = "C:\\WINNT\\System32\\config"; /*Save current drive.*/ curdrive = _getdrive(); printf("Available drives in this machine are: \n"); /*If we can switch to the drive, it exists.*/ for(drive = 1; drive <= 26; drive++) if(!_chdrive(drive)) printf("%c: ", drive + 'A' - 1); printf("\n\nType drive letter to check: "); chr = _getch(); if(chr == 27) printf("Illegal drive input\n"); if(isalpha(chr)) _putch(chr); if(_getdcwd(toupper(chr) - 'A' + 1, path, _MAX_PATH) != NULL) printf("\nCurrent directory on that drive is:\n%s\n", path); /*Restore original drive.*/ _chdrive(curdrive); /*Get the current working directory*/ if(_getcwd(buffer, _MAX_PATH) == NULL) perror("_getcwd error"); else printf("\nCurrent working directory is: %s\n", buffer); /*Create a directory and then delete */ if(_mkdir(newdir) == 0) { printf("\nDirectory %s was successfully created\n", newdir); system("dir \\testdir"); if(_rmdir("\\testdir") == 0) printf("\nDirectory %s was successfully removed\n", newdir); else printf("\nProblem removing directory %s\n", newdir); } else printf("\nProblem creating directory %s\n", newdir); /*Uses _chdir() function to verify that a given directory exists*/ printf("\n"); printf("Change directory........\n"); if(_chdir(path1)) printf("Unable to locate the directory: %s\n", path1); else system("dir *.log /a"); printf("\n"); return 0; } ------------------------------------------------------------------------------------------------------

/*The use of the 32-bit _find functions to print a list of all files (and their attributes) in the current directory.*/ #include <stdio.h> #include <io.h> #include <time.h> #include <direct.h> #include <conio.h> #include <ctype.h> int main() { char path[50] = "C:\\WINNT\\System32\\config"; struct _finddata_t c_file; long hFile; printf("Change to %s\n", path); if(_chdir(path)) printf("Unable to locate the directory: %s\n", path); else /* Find first in the current directory */ hFile = _findfirst("*.*", &c_file); /* List the files... */ printf("Listing of files in the directory %s\n\n", path); printf("\nRDO HID SYS ARC FILE DATE %25c SIZE\n", ' '); printf("--- --- --- --- ------- %25c ----\n", ' '); printf((c_file.attrib & _A_RDONLY) ? " Y " : " N "); printf((c_file.attrib & _A_SYSTEM) ? " Y " : " N "); printf((c_file.attrib & _A_HIDDEN) ? " Y " : " N "); printf((c_file.attrib & _A_ARCH) ? " Y " : " N "); printf(" %-12s %.24s %9ld\n", c_file.name, ctime(&(c_file.time_write)), c_file. size); /*Find the rest of the files*/ while(_findnext(hFile, &c_file) == 0) { printf((c_file.attrib & _A_RDONLY) ? " Y " printf((c_file.attrib & _A_SYSTEM) ? " Y " printf((c_file.attrib & _A_HIDDEN) ? " Y " printf((c_file.attrib & _A_ARCH) ? " Y " printf(" %-12s %.24s %9ld\n", c_file.name, e.size); } _findclose(hFile); return 0; } --------------------------------------------------------------------------------------------------#include #include #include #include #include <io.h> <fcntl.h> <sys/types.h> <sys/stat.h> <stdio.h>

: " N "); : " N "); : " N "); : " N "); ctime(&(c_file.time_write)), c_fil

int main() {

int fhdl, result; char fname[20] = "C:\\data.txt"; /*Open a file*/ if((fhdl = _open(fname, _O_RDWR _O_CREAT, _S_IREAD _S_IWRITE)) != -1) { printf("%s file length before running _chsize(): %ld\n", fname, _filelengt h(fhdl)); /*Change the file size*/ printf("Executing _chsize(fhdl, 123456)...\n"); if((result = _chsize(fhdl, 123456)) == 0) printf("%s file size successfully changed!\n", fname); else printf("Problem in changing the %s size\n", fname); /*New size*/ printf("%s file length after changing the size: %ld\n", fname, _filele ngth(fhdl)); /*close the file handle*/ _close(fhdl); } return 0; } ------------------------------------------------------------------------------------------------------#include #include #include #include <stdio.h> <conio.h> <stdlib.h> <direct.h>

void DisplayFullPath(char *relPath) { /*Buffer*/ char full[_MAX_PATH]; if(_fullpath(full, relPath, _MAX_PATH) != NULL) printf("The full path is: %s\n", full); else printf("Invalid path\n"); } int main() { DisplayFullPath("test.txt"); DisplayFullPath("\\test.txt"); DisplayFullPath("..\\test.txt"); return 0; } ---------------------------------------------------------------------------------------------------------#include <stdlib.h> #include <stdio.h> int main() { char path_buffer[_MAX_PATH]; char drive[_MAX_DRIVE];

char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _makepath(path_buffer, "g", "\\Testdir\\myexample\\", "testfile", "txt"); printf("Path created with _makepath(): %s\n", path_buffer); _splitpath(path_buffer, drive, dir, fname, ext); printf("Path extracted with _splitpath():\n"); printf(" Drive: %s\n", drive); printf(" Dir: %s\n", dir); printf(" Filename: %s\n", fname); printf(" Ext: %s\n", ext); return 0; } ================================================================================ =====================

You might also like