Thread: Usefull.h file, ExitWindows, Fullscreen...

  1. #1
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133

    Usefull.h file, ExitWindows, Fullscreen...

    Hi.
    I created a little .h file to make some thing easier, it may sucks but that is your opion, if you don't like it... don't download it. If you use this you can just simple typ in your source Exit; to shut down your computer, typ Fullscreen; to open Win32 Console in fullscreen, typ Smallscreen; to make Win32 Console smaller, oh yeah and I've add an useless function: if you typ: Cls; it clear's your screen. To use or how I use it: place the Usefull.h file somewhere on your computer, I put in C:\Dev-Cpp\include. Then [IMPORTANT] you NEED to typ at the top of your source the following line: #define _WIN32_WINNT 0x0501 This is 100% neccesary (sorry). And under that you need to add (off corse) #include <Usefull.h> (you don't need to include windows.h now). For me it's quite usefull so thought that I can give it to you, I created this but I used some source from Br5an and Xei. If you know a way that I don't need to typ #define _WIN32_WINNT 0x0501 at the top of my source (that I can put it in Usefull.h) please tell me.
    Last edited by Yuri; 08-13-2005 at 08:05 PM.

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    that nice but hasnt anyone told you about using system();
    if youd look through the fact and come up with a better
    way to do that, more people might be interested.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I'll keep the obvious comments to myself, but I think in C++ the general convention is not to use #define's. According to this FAQ here: http://www.parashift.com/c++-faq-lit...s.html#faq-9.5 they are evil in 4 ways! Plus, just having some mystical thing like Exit; somewhere in the source of anything will creep me out. It's like, what the ........ is that doing?

  4. #4
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    I don't get it, what's wrong with #define, please tell me. I have looked on the site but I still don't get it. And what's with system();?
    Last edited by Yuri; 08-14-2005 at 05:27 AM.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The system() function is rarely a good idea. It is always slow. It spawns a process to perform the actions, thus it is slow. If someone chooses to replaces the CLS program with one of their own, your application will run it. If you are using the more advanced console programming routines, the CLI spawned by system() will intercept and process the messages, your program will not work.

    system() is not a good idea.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't get it, what's wrong with #define, please tell me.
    There's almost always a better solution, that's what's wrong with #define, and the preprocessor in general. A few inline functions would be much better than textually replacing a string in the source file with several logical lines of code.
    My best code is written with the delete key.

  7. #7
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Ok, I just thought it would come usefull for some starters. I don't know anything from inline functions, can you give me a link to a tutorial for inline? Thanks.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If had clicked my link then you may have learned a bit about inline functions and why #define's are bad. It's merely a keyword to specify that the compiler will try to "inline" the function, thus reducing the function call overhead. This is similar to your insane #define's in that a simple call like Exit(); with the inline specifier will be expanded by the compiler to whatever the inline'd Exit() is. I'm extremely offended and all, etc. etc.

  9. #9
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Sorry, I didn't mean to offend you, but it is just like I said; I didn't understand it, on that site. I gonna try to understand that site and read it now (again). I am Dutch so my english isn't so good but thanks anyway.

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Don't worry, it's not wrong just frowned upon. Here's an identical program using inline functions...

    main.cpp
    Code:
    //#define _WIN32_WINNT 0x0501
    #include <iostream>
    #include <windows.h>
    #include "Usefull.h"
    
    using namespace std;
    
    int main()
    {
        char ans;
        cout<<"Do you wish to shut down your computer?"<<endl;
        cout<<">>";
        cin>>ans;
        
        if(ans=='y')
        {
            shutdown();
        }
        else
        {
            cout<<"Ok then cool";
        }
        cin.get();
        cin.get();
        return 0;        
        
        
    }
    Usefull.h
    Code:
    //save this file as Usefull.h
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void shutdown(void)
    {
      TOKEN_PRIVILEGES tkp; 
      HANDLE hToken; 
      OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken); 
      LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); 
      tkp.PrivilegeCount = 1; 
      tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
      AdjustTokenPrivileges(hToken,FALSE,&tkp,0,NULL,0); 
      ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0);
    }

  11. #11
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Ok I got it like this:
    But I still can't figure it out how I don't need to #define_WIN32_WINNT 0x0501 above in my source if I want to use Usefull.h.
    Code:
    #ifndef _Usefull2_H
    #define _Usefull2_H
    
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void Exit(void)
    {
        TOKEN_PRIVILEGES tkp;
        HANDLE hToken;
        OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken);
        LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
        tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        AdjustTokenPrivileges(hToken,FALSE,&tkp,0,NULL,0);
        ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0);
    }
    
    void Fullscreen(void)
    {
        COORD Coord;
        SetConsoleDisplayMode( GetStdHandle( STD_OUTPUT_HANDLE ), CONSOLE_FULLSCREEN_MODE, &Coord);
    }
    
    void Smallscreen(void)
    {
        COORD Coord;
        SetConsoleDisplayMode( GetStdHandle( STD_OUTPUT_HANDLE ), CONSOLE_WINDOWED_MODE, &Coord);
    }
    
    void Cls(void)
    {
        system("cls");
    }      
    
    #endif
    Do you know a way how I can put _WIN32_WINNT 0x0501 in Usefull.h and not at the top of every source, thanks anyway.

    EDIT
    Here is Usefull2.h (only #define to inline is changed). You still need add at the top of your source: #define _WIN32_WINNT 0x0501 and off corse #include <Usefull2.h> You don't need to #include <windows.h>. Here it is:
    Last edited by Yuri; 08-15-2005 at 10:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM