10 tips & tricks for C/C++ windows programming with Visual C++ 6.0

Discussion in 'Forum for discussion of ANTICHAT' started by Dracula4ever, 30 May 2006.

  1. Dracula4ever

    Dracula4ever Elder - Старейшина

    Joined:
    8 May 2006
    Messages:
    418
    Likes Received:
    183
    Reputations:
    26
    1)Hiding a console window-
    in order to hide a console window, just add the following line to your source code:
    #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

    2)Having smaller exe- by adding this line:
    #pragma comment(linker, "/OPT:NOWIN98")
    you can significantly resize your exe size.
    But, the cache is that it won't run smoothly on windows 98 OS.
    So, you should use this option only when you compile applications that will be used only on windows NT.

    3)Using windows hooks without an external DLL:
    Installing windows hooks is a powerfull ability, that can be exploited for maliciousness purpose.
    It seems like the MSDN attemps to "hide" the ability to install hooks without using external dll from the developers.
    So, in order to install global windows hook, we need to export function from a dll, we can solve this by simply adding this:
    __declspec(dllexport)
    before the function's definition.

    4) Share data between a few different instances of the program:

    By adding this section to your code, you can share data between a few instances of your program:

    #pragma data_seg(".SHARED")
    int shared=0;//variables that will be shared
    #pragma data_seg()
    #pragma comment(linker, "/section:.SHARED,RWS")

    5) Creating an edit box that accepts only numbers:
    In order to preform this action, you need to change the window's procedure.
    This action can be preformed by a technique called "Windows subclassing", you can find more information about this technique here:
    http://www.codeproject.com/dialog/messagehandling4.asp

    6) Extract the file name from a path:
    for this sake, there is no need to parse anything by yourself.
    you can simply use the _splitpath() functions, that splits path into a few parts, the drive, the
    directory, the file name and the file's extension.

    7) Link library:
    You can link library inside your source code by using this piece of code:
    #pragma comment(lib, "libname.lib");

    8) Displaying an error message:
    in order to display error message by it's error code, you can use the following function:

    void PrintError(DWORD error)
    {
    LPVOID buff;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,error,
    MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&buff,0,0);
    printf("%s",buff);
    }

    9) Create a console window in a windows application:
    for the purpose of creating a console windows in a windows application, and refferring all input and output to it, you can use the following piece of code:

    AllocConsole();
    freopen("conin$", "r", stdin); //console input buffer
    freopen("conout$", "w", stdout);//console output buffer
    freopen("conout$", "w", stderr); //console error device

    10) How to assure that only one instance of the application is running at a time:
    You can create a kernel object(mutex for example) and check if it is already
    exists.
    See the following code for example:

    char name[]="objectname1331"
    HANDLE hKernel;

    hKernel = CreateMutex(NULL, FALSE,
    name);

    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
    exit(0);
    }
     
Loading...