利用网卡序列号设计软件注册程序
(
十八
)
本文讲述如何利用网卡序列号来设计软件的注册程序。
1.
创建一个对话框工程:
NetMACRegDlg
2.
添加一个文本框用来显示获取的网卡地址。
3.
添加一个按钮生成序列号,代码如下:
CString
code
;
CString
regcode
,
tmp
;
GetDlgItem
(
IDC_MACADDR_EDIT
)->
GetWindowText
(
code
);
code
.
MakeLower
();
CString
seg1
,
seg2
,
seg3
;
int
num
;
seg1
=
code
.
Mid
(0,4);
seg2
=
code
.
Mid
(5,4);
seg3
=
code
.
Mid
(10,4);
char
*
cpseg1
=
new
char
[4];
char
*
cpseg2
=
new
char
[4];
char
*
cpseg3
=
new
char
[4];
cpseg1
=
seg1
.
GetBuffer
(0);
cpseg2
=
seg2
.
GetBuffer
(0);
cpseg3
=
seg3
.
GetBuffer
(0);
char
temp
;
int
i
;
for
(
i
=0;
i
<4;
i
++)
{
temp
=
cpseg1
[
i
];
if
(
temp
>=
'a'
&&
temp
<=
'f'
)
num
=
temp
-
'a'
+10;
else
num
=
temp
-
'0'
;
tmp
.
Format
(
"%c"
,
base
[
num
*4+
i
]);
regcode
+=
tmp
;
}
for
(
i
=0;
i
<4;
i
++)
{
temp
=
cpseg2
[
i
];
if
(
temp
>=
'a'
&&
temp
<=
'f'
)
num
=
temp
-
'a'
+10;
else
num
=
temp
-
'0'
;
tmp
.
Format
(
"%c"
,
base
[
num
*4+
i
]);
regcode
+=
tmp
;
}
for
(
i
=0;
i
<4;
i
++)
{
temp
=
cpseg3
[
i
];
if
(
temp
>=
'a'
&&
temp
<=
'f'
)
num
=
temp
-
'a'
+10;
else
num
=
temp
-
'0'
;
tmp
.
Format
(
"%c"
,
base
[
num
*4+
i
]);
regcode
+=
tmp
;
}
regcode
.
MakeUpper
();
GetDlgItem
(
IDC_REGCODE_EDIT
)->
SetWindowText
(
regcode
);
其中:
char
base
[64]={
'd'
,
'c'
,
'a'
,
'b'
,
'h'
,
'g'
,
'e'
,
'f'
,
'l'
,
'k'
,
'i'
,
'j'
,
'p'
,
'o'
,
'm'
,
'n'
,
't'
,
's'
,
'q'
,
'r'
,
'x'
,
'w'
,
'u'
,
'v'
,
'b'
,
'a'
,
'y'
,
'z'
,
'f'
,
'e'
,
'c'
,
'd'
,
'j'
,
'i'
,
'g'
,
'h'
,
'n'
,
'm'
,
'k'
,
'l'
,
'r'
,
'q'
,
'o'
,
'p'
,
'v'
,
'u'
,
's'
,
't'
,
'z'
,
'y'
,
'j'
,
't'
,
'o'
,
'v'
,
'w'
,
'm'
,
'q'
,
'x'
,
'p'
,
'f'
,
'w'
,
'x'
,
'd'
,
'c'
};
用来实现简单的加密来计算序列号。
4.
OnInitDialog
()
来获取网卡地址
//
以下是获取
MAC
地址
NCB
ncb
;
LANA_ENUM
lenum
;
ADAPTER_STATUS
state
;
UCHAR
ucReturnCode
;
ncb
.
ncb_command
=
NCBENUM
;
ncb
.
ncb_buffer
=(
UCHAR
*)&
lenum
;
ncb
.
ncb_length
=
sizeof
(
lenum
);
ucReturnCode
=
Netbios
(&
ncb
);
if
(
lenum
.
length
>=0)
{
int
num
=
lenum
.
lana
[0];
UCHAR
buf
[128];
memset
(&
ncb
,0,
sizeof
(
ncb
));
ncb
.
ncb_command
=
NCBRESET
;
ncb
.
ncb_lana_num
=
num
;
ucReturnCode
=
Netbios
(&
ncb
);
memset
(&
ncb
,0,
sizeof
(
ncb
));
ncb
.
ncb_command
=
NCBASTAT
;
ncb
.
ncb_lana_num
=
num
;
ncb
.
ncb_buffer
=(
unsigned
char
*)&
state
;
ncb
.
ncb_length
=
sizeof
(
state
);
strcpy
( (
char
*)
ncb
.
ncb_callname
,
"*"
);
ucReturnCode
=
Netbios
(&
ncb
);
CString
strMac
;
strMac
.
Format
(
"%02X%02X-%02X%02X-%02X%02X/n"
,
state
.
adapter_address
[0],
state
.
adapter_address
[1],
state
.
adapter_address
[2],
state
.
adapter_address
[3],
state
.
adapter_address
[4],
state
.
adapter_address
[5]);
GetDlgItem
(
IDC_MACADDR_EDIT
)->
EnableWindow
(
FALSE
);
GetDlgItem
(
IDC_MACADDR_EDIT
)->
SetWindowText
(
strMac
);
}
注意:要包含头文件:
#include
"nb30.h";link
中添加:
netapi32.lib
。或者用下面的语句:
#pragma
comment(lib,
"netapi32.lib"
)
完成。