Archive for the ‘C++、’ Category

C++网络安全工具编程(二)Web目录扫描器

星期二, 四月 30th, 2013 136 views

我们知道对目标网站的访问是基于Http协议的,而Http又是基于TCPsocket连接的,所以本例中将会使用到上一节中的部分代码,即关于C++最简socket连接建立的Demo。
建立socket连接的过程请参考《C++网络安全工具编程(一)connect端口扫描器》中的基础部分。

当成功与目标服务器的80端口(即http服务端口)建立连接后,我们即可通过发送请求和接收消息来与目标网站服务器进行信息交互,大部分浏览器的实现也是基于此原理。

那么,应该怎样与目标网页服务器进行交互呢。这里我们需要先了解一下Http报文。

先来看下Http请求报文


(图来自网络)
(更多…)

C++网络安全工具编程(一)connect端口扫描器

星期日, 四月 28th, 2013 171 views

端口扫描的种类有很多,其中最简单的即为基于TCP三次握手协议的connect扫描器。其原理为与服务端口建立一个socket连接,当连接上时即可判断端口打开,之后断开连接,进行下一端口的试探。

首先来看一个最简的c++客户端与目标服务器建立socket连接的基础代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "stdafx.h"
#include <string .h>
#include <winsock .h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    //初始化Windows Sockets 动态库

    WSADATA wsaData;
    if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    {
        cout< <"找不到可使用的WinSock dll!"<<endl;
        return 1;
    }

    //创建套接字
    SOCKET sClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(sClient==INVALID_SOCKET)
    {
        cout<<"创建客户端socket失败!"<<endl;
        return 1;
    }
    //连接服务器
    SOCKADDR_IN addrServ;
    addrServ.sin_family=AF_INET;
    addrServ.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    addrServ.sin_port=htons(10046);

    if(connect(sClient,(sockaddr *)&addrServ,sizeof(sockaddr))==SOCKET_ERROR)
    {
        cout<<"连接服务器失败!"<<endl;
        closesocket(sClient);
        return 1;
    }
    else
        cout<<"连接服务器成功!"<<endl;
   
    //关闭套接字,释放资源
    closesocket(sClient);
    WSACleanup();

    return 0;

(更多…)

C++ for Arduino EventTimer(事件定时器)循环队列

星期六, 四月 20th, 2013 579 views

我们知道在Arduino中是没有线程这个概念的,如果需要同时按照时间间隔执行多个事件,使用系统自带的delay()进行阻塞势必会影响到后面的事件。例如在loop()中我们需要不间断的向电机发送脉冲信号使之行走,同时每间隔1s向控制端发送一次数据。如果采用delay(1000)的方法,电机则会每隔1s才获得一次脉冲信号,这不是我们想要的。所以,我们需要为此自定义一个事件定时器。

事件定时器类的C++代码如下:
EventTimer.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once
#include "Arduino.h"
typedef void (*LPFUNC)();//typedef定义一个函数指针
class EventTimer
{
public:
    EventTimer(void);
    ~EventTimer(void);
    void Init();//初始化
    void Looper();//事件循环
    void AddEvent(LPFUNC fun,int delay,int repeat);//添加一个事件
   
private:
        /*事件结构体*/
    struct EventNode{
        LPFUNC fun;//事件函数体
        int delay;//执行延时
        int repeat;//执行次数
        int lasttime;//上一个时间
        EventNode * next,* prior;//上一级、下一级指针

    };
    EventNode *head;
    EventNode *tail;
};

(更多…)

QT paintEvent重绘事件及窗体圆角代码

星期日, 一月 27th, 2013 191 views

利用重回paintEvent重绘事件来进行窗体重绘,本例中使用一个圆角代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void MainWindow::paintEvent(QPaintEvent *){

    QBitmap objBitmap(size());
    //QPainter用于在位图上绘画
    QPainter painter(&objBitmap);
    //填充位图矩形框(用白色填充)
    painter.fillRect(rect(),Qt::white);
    painter.setBrush(QColor(0,0,0));
    //在位图上画圆角矩形(用黑色填充)
    painter.drawRoundedRect(this->rect(),10,10);
    //使用setmask遮罩
    setMask(objBitmap);

}

在mainwindow.cpp中直接添加 MainWindow::paintEvent 方法,主窗体运行时会自动调用该处的重绘代码。
本段代码使用了遮罩原理进行圆角绘制。举一反三后,其原理可以用到更多的地方。
最后效果如下:

注意,当使用了遮罩后,程序自带的标题框会消失,或者说是被覆盖了。
本文到此,感谢关注。

BeiTown
2013.01.27

QT外部调用qss样式表方法

星期日, 一月 27th, 2013 239 views

以利用qss样式在主窗口添加背景图片为例:

①新建qss文件
建立文本文件,内容先空着一会添加。更改文件后缀名为qss。本例中qss为style.qss

②创建qrc资源文件
右键点项目 “Add New(添加新文件)”->”QT Resource file(QT资源文件)” 生成一个qrc文件。本例中qrc为src.qrc

③将qss文件添加进qrc中
(更多…)

QT调用多个UI窗口的方法及error LNK2019修复

星期六, 一月 26th, 2013 3,241 views

首先是创建一个QT项目,选择QT Gui应用。然后一直下一步。

创建完成后项目结构如下

这个时候点运行,出现一个空的mainwindow界面,这里就不截图了。
(更多…)

C++调用DLL的方法

星期六, 十二月 15th, 2012 39 views

关于C++调用DLL的方法有几种,这里给出个人认为较简单的一种,做一个简单的代码范例,见代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//引入wtypes头文件
#include <wtypes.h>

//定义一个函数指针
typedef void (  * TULIPFUNC )(void);

//定义一个函数指针变量
TULIPFUNC FUNC;

int _tmain(int argc, _TCHAR* argv[])
{
    //加载TEST.Dll
    HINSTANCE hinst=::LoadLibrary("TEST.dll");  

    //找到Dll的TESTFUNC函数
    FUNC=(TULIPFUNC )GetProcAddress(hinst,"TESTFUNC");

    //调用TESTFUNC函数
    FUNC();
}

欢迎补充及交流

BeiTown
2012.12.15

C++获取硬盘信息(序列号)源码

星期五, 十二月 14th, 2012 519 views

代码是老外的,项目中需要用到所以临时找了很多版本,这是目前测试可用性最高的版本,所有代码都写进了一个CPP里
代码有点长,限于篇幅只传一部分吧,其他后面给压缩包。此处为获取硬盘信息的入口函数,感兴趣的朋友可以试着剥离出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
long getHardDriveComputerID ()
{
    int done = FALSE;
    // char string [1024];
    __int64 id = 0;
    OSVERSIONINFO version;

    strcpy (HardDriveSerialNumber, "");

    memset (&version, 0, sizeof (version));
    version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
    GetVersionEx (&version);
    if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
        //  this works under WinNT4 or Win2K if you have admin rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using physical access with admin rights\n");
#endif
        done = ReadPhysicalDriveInNTWithAdminRights ();

        //  this should work in WinNT or Win2K if previous did not work
        //  this is kind of a backdoor via the SCSI mini port driver into
        //     the IDE drives
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using the SCSI back door\n");
#endif
        // if ( ! done)
        done = ReadIdeDriveAsScsiDriveInNT ();

        //  this works under WinNT4 or Win2K or WinXP if you have any rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using physical access with zero rights\n");
#endif
        //if ( ! done)
        done = ReadPhysicalDriveInNTWithZeroRights ();

        //  this works under WinNT4 or Win2K or WinXP or Windows Server 2003 or Vista if you have any rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
        printf ("\nTrying to read the drive IDs using Smart\n");
#endif
        //if ( ! done)
        done = ReadPhysicalDriveInNTUsingSmart ();
    }
    else
    {
        //  this works under Win9X and calls a VXD
        int attempt = 0;

        //  try this up to 10 times to get a hard drive serial number
        for (attempt = 0;
            attempt < 10 && ! done && 0 == HardDriveSerialNumber [0];
            attempt++)
            done = ReadDrivePortsInWin9X ();
    }

    if (HardDriveSerialNumber [0] > 0)
    {
        char *p = HardDriveSerialNumber;

        WriteConstantString ("HardDriveSerialNumber", HardDriveSerialNumber);

        //  ignore first 5 characters from western digital hard drives if
        //  the first four characters are WD-W
        if ( ! strncmp (HardDriveSerialNumber, "WD-W", 4))
            p += 5;
        for ( ; p && *p; p++)
        {
            if ('-' == *p)
                continue;
            id *= 10;
            switch (*p)
            {
            case '0': id += 0; break;
            case '1': id += 1; break;
            case '2': id += 2; break;
            case '3': id += 3; break;
            case '4': id += 4; break;
            case '5': id += 5; break;
            case '6': id += 6; break;
            case '7': id += 7; break;
            case '8': id += 8; break;
            case '9': id += 9; break;
            case 'a': case 'A': id += 10; break;
            case 'b': case 'B': id += 11; break;
            case 'c': case 'C': id += 12; break;
            case 'd': case 'D': id += 13; break;
            case 'e': case 'E': id += 14; break;
            case 'f': case 'F': id += 15; break;
            case 'g': case 'G': id += 16; break;
            case 'h': case 'H': id += 17; break;
            case 'i': case 'I': id += 18; break;
            case 'j': case 'J': id += 19; break;
            case 'k': case 'K': id += 20; break;
            case 'l': case 'L': id += 21; break;
            case 'm': case 'M': id += 22; break;
            case 'n': case 'N': id += 23; break;
            case 'o': case 'O': id += 24; break;
            case 'p': case 'P': id += 25; break;
            case 'q': case 'Q': id += 26; break;
            case 'r': case 'R': id += 27; break;
            case 's': case 'S': id += 28; break;
            case 't': case 'T': id += 29; break;
            case 'u': case 'U': id += 30; break;
            case 'v': case 'V': id += 31; break;
            case 'w': case 'W': id += 32; break;
            case 'x': case 'X': id += 33; break;
            case 'y': case 'Y': id += 34; break;
            case 'z': case 'Z': id += 35; break;
            }                            
        }
    }

    id %= 100000000;
    if (strstr (HardDriveModelNumber, "IBM-"))
        id += 300000000;
    else if (strstr (HardDriveModelNumber, "MAXTOR") ||
        strstr (HardDriveModelNumber, "Maxtor"))
        id += 400000000;
    else if (strstr (HardDriveModelNumber, "WDC "))
        id += 500000000;
    else
        id += 600000000;

#ifdef PRINTING_TO_CONSOLE_ALLOWED

    printf ("\nHard Drive Serial Number__________: %s\n",
        HardDriveSerialNumber);
    printf ("\nHard Drive Model Number___________: %s\n",
        HardDriveModelNumber);
    printf ("\nComputer ID_______________________: %I64d\n", id);

#endif

    return (long) id;
}

具体就不分析了,有问题欢迎留言谈论,以下是效果截图

源码下载:
【Diskid32源码及演示下载】

C++在VS中导出DLL动态链接库文件的常规方法

星期三, 十二月 12th, 2012 78 views

①建立项目:
[新建项目]选择Win32控制台应用程序,输入项目名点击确定。
在Win32应用程序向导的应用程序类型中选择DLL,附加选项勾选空项目,如下图,之后点击完成。

②创建头文件,代码如下:

1
2
3
4
5
6
7
8
#ifndef DLLTEST_H
#define DLLTEST_H //防止重复定义
 
#define DECLDIR extern "C" __declspec(dllexport) //宏定义

DECLDIR int Test(int a, int b);  //函数声明

#endif

(更多…)