Console APP&&WIN32 APP

来源:互联网 发布:软件开发报价单模板 编辑:程序博客网 时间:2024/04/20 11:12

http://www.codeproject.com/KB/winsdk/winconsole.aspx

今天在code project上看到了一篇感觉有趣的小代码,分享一下给大家,上面是连接谢谢原作者,感觉这个程序能给人inspiration~~嘿嘿~~

image

//resource.h

//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by test.rc
//
#define IDD_DIALOG                      101
#define IDC_EDIT                        1001

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        103
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1003
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

//main.cpp


// define _MT so that _beginthread( ) is available
#ifndef _MT
#define _MT
#endif

#include
#include
#include
#include "resource.h"

// global flag
bool bDone = false;
   
// this function is called by a new thread
void InputThreadProc( void *dummy )

    // create the dialog window
    HWND hWnd = ::CreateDialog(NULL,MAKEINTRESOURCE(IDD_DIALOG),NULL,NULL);
    if(hWnd!=NULL)
    {
        // show dialog
        ::ShowWindow(hWnd,SW_SHOW);
    }
    else
    {
        printf("Failed to create dialog/n");
        bDone = true;
        return;
    }
    // message loop to process user input
    MSG msg;
    while(1)
    {
        if(::PeekMessage(&msg,hWnd,0,0,PM_REMOVE))
        {
            if(msg.message==WM_KEYUP)
            {
                int nVirtKey = (int)msg.wParam;
                // if the user pressed the ESCAPE key, then
                // print the text the user entered and quit
                if(nVirtKey==VK_ESCAPE)
                {
                    // get the edit control
                    HWND hEdit = ::GetDlgItem(hWnd,IDC_EDIT);
                    if(hEdit)
                    {
                        // get the input text the user entered and print
                        // it to the console window
                        char pText[3201];
                        int nSize = ::GetWindowText(hEdit,pText,3200);
                        pText[nSize] = 0;
                        printf("/nYou have entered the following text in a second thread:/n/n%s/n/n",pText);
                    }
                    else
                    {
                        printf("Failed to get edit control/n");
                    }
                    // destroy the dialog and get out of the message loop
                    ::DestroyWindow(hWnd);
                    bDone = true;
                    break;
                }
            }
            // process message
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
        else
        {
            // if there is no message to process,
            // then sleep for a while to avoid burning
            // too much CPU cycles
            ::Sleep(100);
        }
    }
}

void main( int argc, char** argv )
{
    printf("Hello, world of console apps/n");
    // create a new thread to allow user input
    if(_beginthread(InputThreadProc, 0, NULL )==-1)
    {
        printf("Failed to create thread");
        return;
    }
    // wait for the new thread to finish
    while(!bDone)
    {
        // sleep 3 seonds
        ::Sleep(3000);
        printf("main thread running/n");
    }
}

这个对话框并没有定义定义成员数据,其实从本质上说明了一个完整程序的组成:消息循环、主函数、线程、消息处理函数。有人说console app和win32 app最大的区别就是console app没有消息循环。我们也可以根据这个例子来写自己的隐藏进程(for virus?),只要把显示对话框改成别的就行了。

原创粉丝点击