Results 1 to 4 of 4

Thread: My first C++ program!

  1. #1

    Thread Starter
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I finally finished my first C++ program, with alot of help from Parksie & Yonatan.

    Here's the source, feel free to critize it, or whatever

    Code:
    // ReadOnlyKiller v2 by Jop de Klein
    // Thanks to Parksie, Yonatan & Vlatko
    
    #include <windows.h>
    #include <stdio.h>
    #include <string>
    #include <stdlib.h>
    
    
    using namespace std;
    
    inline string StrRight(string sText, int iPlaces) {
        return sText.substr(sText.length() - iPlaces, iPlaces); //thanks parksie
    }
    
    void CreateKey(char sPath[260])
    {
    HKEY retVal;
    strcat(sPath," \"%1\"");
    printf("Adding registry key...\n\n");
    RegCreateKey(HKEY_CLASSES_ROOT, "FOLDER\\SHELL\\Remove Read Only...\\Command\\", &retVal);
    RegSetValueEx(retVal, NULL, 0, REG_SZ, (LPBYTE)sPath, strlen(sPath));
    RegCloseKey(retVal);
    }
    
    void DeleteKey()
    {
    HKEY retVal;
    printf("Deleting registry key...\n\n");
    RegCreateKey(HKEY_CLASSES_ROOT, "FOLDER\\SHELL\\Remove Read Only...\\", &retVal);
    RegDeleteKey(retVal, "");
    }
    
    void DoFiles(string sRootPath) 
    {
    	//Thanks to mike (parksie) for translating this code from VB to C++
    	WIN32_FIND_DATA fndFiles;
    	HANDLE hSearch;
    	long sAttr;
    	
    			//If readonly, remove it
    			sAttr = GetFileAttributes((sRootPath).c_str());
    			if (sAttr && FILE_ATTRIBUTE_READONLY)
    			{
    				SetFileAttributes((sRootPath).c_str(), (sAttr && !FILE_ATTRIBUTE_READONLY));
    			}
    
    	hSearch = FindFirstFile((sRootPath + "*").c_str(), &fndFiles);
    	// Ignores ".", so does not print first file found
    
    	while(FindNextFile(hSearch, &fndFiles)) 
    	{
    		// Do not accept ".." either
    		if(!strcmp(fndFiles.cFileName, "..")) continue;
    
    		// If ReadOnly, Remove it
    		sAttr = GetFileAttributes((sRootPath + fndFiles.cFileName).c_str());
    		if (sAttr && FILE_ATTRIBUTE_READONLY)
    		{
    			SetFileAttributes((sRootPath + fndFiles.cFileName).c_str(), (sAttr && !FILE_ATTRIBUTE_READONLY));
    		}
    
    		if(GetFileAttributes((sRootPath + fndFiles.cFileName).c_str()) & FILE_ATTRIBUTE_DIRECTORY)
    		{
    			// It's a directory, so call this function again
    			DoFiles(sRootPath + fndFiles.cFileName + "\\");
    		}
    	}
    
    	FindClose(hSearch);
    }
    
    void main(int argc, char **argv) {
    	char sDir[260];
    	unsigned long sTime;
    
    	if(argc < 2) 
    	{
    		printf("Welcome to ReadOnlyKiller v2! \n");
    		printf("The fastest version of RoK ever!\n\n");
    		printf("Please enter directory to process:\n\n> ");
    		gets(sDir);
    		//scanf("%s", sDir);
    
    		if (strcmp(sDir, "/i") == 0)
    		{
    			CreateKey(argv[0]);
    			return;
    		}
    		if (strcmp(sDir, "/u") == 0)
    		{
    			DeleteKey();
    			return;
    		}
    
    		if (StrRight(sDir, 1) != "\\" && StrRight(sDir, 1) != "/")
    		{ 
    			lstrcat(sDir, "\\"); //check for trailing slash
    		}
    
    		sTime = GetTickCount();
    	DoFiles(sDir); //Call the actual sub
    		printf("Processed all files in ");
    		printf("%.00f", ((float)GetTickCount() - (float)sTime) / 1000.0f);
    		printf(" second(s)! \n\n");
    		system("pause");
    		return;
    	}
    
    		if (strcmp(argv[1], "/i") == 0)
    		{
    			CreateKey(argv[0]);
    			return;
    		}
    		if (strcmp(argv[1], "/u") == 0)
    		{
    			DeleteKey();
    			return;
    		}
    
    		printf("Please wait... processing files \n");
    
    		if (StrRight(argv[1], 1) != "\\" && StrRight(argv[1], 1) != "/")
    		{ 
    			lstrcat(argv[1], "\\"); //check for trailing slash
    		}
    
    	DoFiles(argv[1]); //Call the actual sub
    
    }
    And here's the readme file
    Code:
    - Read Only Killer v2 the C++ edition  |  By Jop de Klein -
    
    
    0. Disclaimer
    1. Intro
    2. Purpose
    3. Installation
    4. How to use
    
    
     (Disclaimer)
    Warning: the author of this program takes no responsibility for ANY
    damage done to your system using this prog. Don't bother me with 
    stories about systemcrashes, file losses or any other damage.
    - Distribution
    This software is freeware
    You are free to pass this software on to your friends or put 
    it on public cd-roms, as long as you:
    . Don't modify the program
    . Don't $ell the program
    . If possible send me a copy of the CD *don't bother with floppy's :)*
    
    
     (Intro)
    FAQ: Why making a second edition?
    
    Q: Hey Jop, your old Read Only Killer was working well, why did you
       even bothered making a new one?
    A: Because I'm now learning C++, and the previous Exe (installer) 
       was like 1,35 MB(!) and that was to large for such a small task.
       And by the way, C++ exe's are faster, and this one doesn't even 
       need to get installed!
    
    Q: Why was it so big then Jop?
    A: That was because I wrote it in a language called Visual Basic,
       it's easy, but as side-effect you have to ship large DLLs.
    
    Q: Wait a minute, no installation? Bleuh probably a bad program!
    A: Simplicity is the keyword for RoKv2 C++ edition, that's the
       whole idea about the new version, v1.5 used to much resources,
       the app was huge & slow.
       Now because it's written in C++ and uses no kind of fancy things
       it doesn't need to ship any DLL's with it, no installation is
       needed!
    
    Q: But I can't use the right-click menu in explorer, like in v1.5
    A: That's the only thing you need to 'install'. In RoKv2 type:
       /i to activate the menu in Explorer,
       to deactivate (when you want to uninstall RoK) type:
       /u when RoK asks about a directory.
       You can also use these command via the commandline:
       Click Start > Run and type c:\path-to-rokiller.exe /i
       or, to uninstall:
       Click Start > Run and type c:\path-to-rokiller.exe /u
       PLEASE NOTE: This feature might not be available in Windows NT/2K!
    
    Q: Ah I see
    A: cool.
    
    Q: But Jop, where's the interface like in RoK 1.5? I liked it!
    A: I did too :/ but when creating windows, it means bigger exe's,
       and, as I said, I tried to keep this version simple.
    
    Q: But Jop, Console Apps suck!
    A: First, I thought they did, but for this app, it doesn't matter,
       because you're going to run it with right-clicking folders anyway!
    
    Q: But Jop, I liked the Drag&Drop implementation!
    A: Don't worry, you can still Drag&Drop if you like! Just Drag&Drop
       a folder on the Exe (In explorer or My Computer) and you're done!
    
    Q: Jop, the old RoK always crashed when a directory or drive contained 
       more then 32767 files.
    A: Yep, that was a limitation of VB, this version won't crash and is a
       hell lot more faster ;)
    
    Q: But Jop, when I compare your RoK 1.5 with this new one, v2, I notice
       that the old one (1.5) is faster!
    A: That's not true, there was a bug in 1.5, in wich (at large directories)
       RoK told he was ready when he wasn't, in the background it was sneaky
       processing dirs ;)
    
    Q: Jop, actually your RoK v2 C++ edition rules!
    A: Exactely ;)
    
    
     (Purpose)
    When you copy progs from cd, ever got frustrated from all 
    those subdirs from wich you manually have to remove all 
    the read-only attributes to make the app work?  
    Now Read Only Killer will do this for you! Just select a 
    directory and RoKv2 will remove all those nasty read-only 
    attributes from dir and subdirs!
    
     (Installation)
    There are no DLLs or OCXes needed to be shipped with RoKv2, but to activate the
    right-click menu in Explorer and My Computer, use these:
    In RoKv2 type:
    /i to activate the menu in Explorer (this only has to be done
    once), to deactivate (when you want to uninstall RoK) type
    /u when RoK asks about a directory.
    
    You can also use these commands via the command-line:
    Click Start > Run and type c:\path-to-rokiller.exe /i
    or, to uninstall:
    Click Start > Run and type c:\path-to-rokiller.exe /u
    
    PLEASE NOTE: This feature might not be available in Windows NT/2K!
    
    
     (How to use)
    4 Ways to kill those nasty ReadOnly attributes:
    
    1. In explorer, right-click on a folder and select Remove Read Only...
       (note: first install this feature, as described above)
    2. Drag & Drop a folder on the Exe (in Explorer or My Computer).
    3. Start the prog and type the dir you need to kill
    4. Click Start > Run and type c:\path-to-rokiller.exe c:\dir-to-process
    
    
    Have fun with RoKv2!
    Thanks for the attention guys
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  2. #2
    Guest
    Good Job

    first:

    Creating windows apps does not add very much size.... I have made windows apps as small as 40kb.

    second:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    since you are already using the std namespace... use these headers:

    Code:
    #include <windows.h>
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    
    using namespace std;

  3. #3
    Lively Member
    Join Date
    Nov 2000
    Location
    Québec City
    Posts
    73

    Exclamation

    Denniswrenn, that's true, but you forgot what I think is the most important: he's using namespace std.

    So? so he can (should?) use arithmetics operators on strings!
    no more strcmp, strcat and strcrap! :-)

    examples:
    if (strcmp(sDir, "/u") == 0)
    becomes
    if (sDir == "/u")
    or (in needed)
    if (sDir > "/u")
    if (sDir < "/u")
    etc

    strcat(sPath," \"%1\"");
    becomes
    sPath= sPath +" \"%1\"";

    this is much more intuitive, isn't it?

    Krushstone

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It depends on the situation. For anything more than a few comparisons, I usually use a string. But if it's just something very small, for speed I just use the functions from string.h
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width