下沙论坛

标题: [求助]HZZH进,有关于网络低层控制的问题请教 [打印本页]

作者: Eagle    时间: 2003-9-21 10:56
标题: [求助]HZZH进,有关于网络低层控制的问题请教
调用一般的微软公布的API只能执行一般的连接到网络之类的功能,无法实时获得网络设备以及数据传输之类的信息。7 B" }+ b) T# q8 C
想编一个程序,实现类似于网通拨号器的功能$ Y" @% h8 ^: r1 }  \/ G

0 N. B% c0 z2 n) t) L10:51:59 Message: 正在打开端口,请稍候...
+ ^2 A) M+ R( z. ^/ u1 H10:51:59 Message: 端口已经打开' ?$ ~8 ~- {* `. r, w+ B; H
10:51:59 Message: 正在连接网络设备,请稍候...- H  E+ k8 l/ C
10:51:59 Message: 网络设备连接成功: P! p4 o8 W/ U% b! U& T' D
10:51:59 Message: 网络设备已经准备就绪
, R( {6 n7 h9 H9 b7 a10:51:59 Message: 正在验证用户帐号和口令,请稍候.../ M' x/ B) Y" ?0 t0 ?2 e
10:52:00 Message: 正在接收验证/授权过程通知消息8 P1 s- m/ {7 n& e
10:52:00 Message: 正在与服务器进行数据交互,请稍候...
, j% l7 O. x" _8 r. b: G' q9 b10:52:00 Message: 正在接收验证/授权过程通知消息# \, }. d# }6 V% e
10:52:00 Message: 交互过程结束
9 \3 W; k# B% c' G, M6 e( x10:52:00 Message: 正在接收验证/授权过程通知消息
( u+ Y6 {# w' t' U( g4 @10:52:00 Message: 用户帐号/口令验证通过,正在建立连接9 F5 n$ U' r) A; c. q' U5 `, _
10:52:02 Message: PPPoE连接建立成功
( P. J$ z: i6 p; n+ D10:52:02 Message: IP -> 192.168.0.2
8 g: I* l  \2 A6 [10:52:02 Message: IP -> 218.109.100.70% u3 A0 V; N1 S( b, |4 a3 K, J4 ]' p
10:52:02 Message: IP -> 218.109.98.169
2 {4 K: _1 R- y( @, K9 u/ w
& m; V  r: Z( Y' c2 z, I5 O3 o给一个实例的代码好吗?
8 L& I  l( w/ e4 A我的邮箱是:eagle_twenty@163.com
作者: hzzh    时间: 2003-9-21 21:00
网通的拨号器实际上是一个PPPOE的拨号软件,因此你的要求是要自己编写一个PPPOE的拨号程序,这样就能够显示出所有的信息来,前提是你必须熟悉pppoe的编程。# l+ y, K6 A- I# }4 [/ n
我没有编写过ppp之类的软件,没法给你一个实例。
% I. w+ P' N9 `/ [. e- r我想,偷懒的话,可以安装一个RASPPPOE软件。RASPPPOE只是提供了一个pppoe的协议,拨号直接利用了微软的拨号器,因此只要按标准的拨号软件编程方法就可以编写出一个拨号软件来,可能得到的信息稍微少了一点。
& y: `. e( P# h4 s* q0 {. t, \; X标准的拨号软件编程方法可以参考MSDN的Establishing a Dial-Up Connection to the Internet 中的API  K& u  Y5 z5 n) U. s

作者: Eagle    时间: 2003-9-21 21:45
以下是引用hzzh在2003-9-21 21:00:00的发言:" w  [0 j$ m( M/ n; w: \
我没有编写过ppp之类的软件,没法给你一个实例。
' I: t8 N  b7 D5 j: i 我想,偷懒的话,可以安装一个RASPPPOE软件。RASPPPOE只是提供了一个pppoe的协议,拨号直接利用了微软的拨号器,因此只要按标准的拨号软件编程方法就可以编写出一个拨号软件来,可能得到的信息稍微少了一点。
- ~; ?8 \7 A  h3 g% ^9 ]9 i 标准的拨号软件编程方法可以参考MSDN的Establishing a Dial-Up Connection to the Internet 中的API
4 f* W2 H' [2 q0 H4 A
% x( Y0 J. G2 i7 {9 Z
1 V' ~+ h5 E5 c- b; R
很可惜,真的太可惜了。; h7 k' C# e% c( ~2 d
标准的拨号只要调用几个简单的API就可以做到了,但就像系统自带的RASPHONE那个程序一样,只是简单地提供拨号过程前后的状态。我想要的是介入拨号过程,唉,我自己再找找资料吧。只是网上这方面的中文资料太冷门了。我找了一个上午也没有找到。
作者: Eagle    时间: 2003-9-21 21:51
Thank you all the same!
作者: hzzh    时间: 2003-9-22 18:51
试试这个,CRasMonitor v1.2, A freeware MFC class to monitor RAS (aka Dial-Up Networking) connections0 q+ l; F/ f8 ?) [& t; S

" @6 D5 m6 m' U) u  U5 E! x是一个监控拨号网络连接的MFC类,或许能够得到一些拨号过程消息
( {# ]1 \7 ?5 I2 k; p* Q5 f4 N' q
http://chulsoft.xiloo.com/code/ras.zip
作者: Eagle    时间: 2003-10-25 00:48
谢谢周老师,不过这个问题我自己查了将近一个月的MSDN,终于搞定了。0 T3 ?, t% w) w+ z) Y/ J) s
有一个RasDial拨号函数的定义如下:
( y* R' \% Q- g( R9 C. bDWORD RasDial(2 G7 {- `% ]5 K$ t$ d" E
LPRASDIALEXTENSIONS dialExtensions,
. D9 Y# Y/ {( fLPTSTR phoneBookPath , - Z" D7 z2 P( O
LPRASDIALPARAMS rasDialParam , % ~5 J: P  E. I. w- k) t9 D
DWORD NotifierType, 1 }2 E% @0 p- \4 ]2 C) K8 O- z
LPVOID notifier,
: S& d2 d( m& P4 G" A! N1 LLPHRASCONN pRasConn );
$ z; |1 U* j$ `' R2 z, R6 w8 [
, }7 N; A% ~* p$ j5 R1 T2 y我的MSDN的解说如下:
$ A3 R: ?" ~) \5 D, H& DParameters/ D  Q: J1 Q0 L! p2 o
dialExtensions / U5 d$ V' r2 Y; z  O( @+ h
This parameter is ignored and should be set to NULL. On Windows CE, RasDial always uses the default behaviors for the RASDIALEXTENSIONS options.
% o; Y9 T% p9 T$ }9 Y" g$ fphoneBookPath
+ M. E. f9 Z6 r, C2 _: ^This parameter is ignored and should be set to NULL. Dial-up networking stores phone-book entries in the registry rather than in a phone-book file. 0 X) @9 k: E/ \# m# ?+ L8 O
rasDialParam ; _' H4 N' z3 e5 j! b7 Y; C
Pointer to a RASDIALPARAMS structure that specifies calling parameters for the RAS connection.
% j4 U( i( ~1 `  j" t! Y- ~The caller must set the RASDIALPARAMS structure’s dwSize member to the sizeof(RASDIALPARAMS) to identify the version of the structure being passed. ( m/ W5 s4 r9 q& b6 E
  O4 R* V( @3 y1 B; z* u$ I- X
NotifierType 8 T0 g* Y5 I+ Y7 I. G3 U1 H
Specifies the nature of the notifier parameter. If notifier is NULL, NotifierType is ignored. If notifier is not NULL, set NotifierType to the following value:
! i' k8 e2 s3 mValue Description : j2 X0 \; e6 T
0xFFFFFFFF1 y! W% z( t( S: Z2 x; a
0xFFFFFFFF The notifier parameter is a handle to a window to receive progress notification messages. In a progress notification message, wParam indicates the connection state  (rasconnstate) which the RAS connection is about to enter, while  lParam indicates whether or not an error occurred.  - {% e* F. z8 v* a! ]
The progress notification message uses the WM_RASDIALEVENT message code.
& e% B- g9 R! j# E1 Y( W
) E$ D  ], m; r/ ~' V( D8 @0 r+ @/ e; w/ Z" {' n

8 @' c3 j9 g& N% D4 u
- P. g% a0 J7 W+ i7 vnotifier 7 U3 e$ W' H5 ~0 H8 t5 g! r
Pointer to a window handle to receive RasDial event notifications. If this parameter is not NULL, RasDial sends the window a message for each RasDial event. Additionally, the RasDial call operates asynchronously: RasDial returns immediately, before the connection is established, and uses the window to communicate its progress. ( L, C: F" b9 r: E+ Y2 @, e
If notifier is NULL, the RasDial call operates synchronously: RasDial does not return until the connection attempt has completed successfully or failed. * Y: d, O8 N% W; k$ e  X
- W: Q& r7 R$ x9 w$ }! Q4 c
If notifier is not NULL, notifications to the window can occur at any time after the initial call to RasDial. Notifications end when one of the following events occurs:
, I/ ]8 n: B1 K2 w0 m: d
: v8 Q6 J. c$ v/ n% eThe connection is established. In other words, the RAS connection state is RASCS_Connected.
1 p2 |; \8 [1 c% K4 c, \* O* GThe connection fails. In other words, dwError is nonzero. 3 \, {0 I, E0 l
RasHangUp is called on the connection.
: f& U6 s9 M8 o$ x9 R/ c+ SThe callback notifications are made in the context of a thread captured during the initial call to RasDial.
  a0 o9 m1 V" d$ `8 f. {* V
) C; u* v; }: B6 l* L' QpRasConn
6 M; l  |) y( k  T- q) BPointer to a variable of type HRASCONN. You must set the HRASCONN variable to NULL before calling RasDial. If RasDial succeeds, it stores a handle to the RAS connection into pRasConn. , |- _- @* D  R( ^
Return Values2 k: X! J7 j0 [* B( i" h  k
Zero indicates success. In addition, the function stores a handle to the RAS connection into the variable pointed to by pRasConn. A nonzero error value, either from the set listed in the RAS header file or ERROR_NOT_ENOUGH_MEMORY, indicates failure.
' w# C6 c& W# O- V% S& M/ R- J$ A! T2 A* g7 M( J. g
Include Raserror.h for definitions of the RAS error codes.
9 E4 a- x- `9 K1 ~! {% O6 b* c! ^/ a
( i* `6 N. p0 y- R8 p0 _" i1 b  NRemarks# h9 a& l2 U0 Q* C) g: H5 V
The szCallBackNumber and szPhoneNumber members of the structure pointed to by rasDialParam are not used and should be set to NULL.
* m& {% z1 n$ g7 C( w
, c- |7 q9 @; D4 x2 KRasDial will not automatically display the logon dialog box. This is currently done through the Remote Networking application. Applications are responsible for getting the information from the user.7 \! ]( j& a! y2 b% B4 b, s$ N
$ S) c% |, a! n- {. e
Errors that occur after the immediate return can be detected by RasGetConnectStatus. Data is available until an application calls RasHangUp to hang up the connection. 9 u3 Z; \5 M& f! u. |! `* x

" {5 M7 W3 y/ ?% h& V0 k+ RAn application must eventually call RasHangUp whenever a non-NULL connection handle is stored into pRasConn. This applies even if RasDial returns a nonzero (error) value.
( s* b9 K+ l; @, g4 F6 x8 u7 q' v: b, V1 v! J: k6 b4 k
An application can safely call RasHangUp from a RasDial notifier handler. If this is done, however, the hangup does not occur until the routine returns.
' D; I( o3 L' _
1 D) x; j7 R2 j* bThe window handle-based notification only works if the underlying configuration supports the PostMessage function. PostMessage is exposed through the msgque component, which is a part of the GWES module. Event notification through a window handle can only work if GWES is part of the underlying configuration.
1 j1 x+ }: q8 q! @' _  P* H. I# C& N- T: |0 }' A
看了好久才算是略略通了一点,要实现我原先预期的效果,首先要用RasDial函数进行拨号% X+ P  B" N. N/ G- `8 z
DWORD dwRet = RasDial(NULL, NULL, &RasDialParams, 0L, (RASDIALFUNC)RasDialFunc, &hRasConn);
% }( h# ^/ ]' ~( Q  L- c; F' J, M第五个参数是个回调函数,一般都定义如下:
" S  r3 H0 y' dvoid WINAPI CDialerDlg::RasDialFunc(UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError)
! H; l& {8 X/ j+ W1 J{
5 [1 K! p5 }4 H! Y- M# V8 x        CDialerDlg * RasDlg = (CDialerDlg*)AfxGetApp()->m_pMainWnd;
) d9 I+ B6 j5 ]* W( z. k+ B        + G4 _  a5 M- O, p4 Y' K
        RasDlg->ostMessage(WM_RASDIALEVENT,(WPARAM)rasconnstate,(LPARAM)dwError);7 e. V/ H1 g; G$ @2 [
}! r0 W# d* X9 {2 z4 C
这个回调函数将会把拨号的状态POST到各个窗口句柄。3 O  O* i& R0 |9 E( H5 h

  b" Z9 N& K! w8 {: U3 }! O
作者: hzzh    时间: 2003-10-28 12:53
呵呵,自己把MSDN给啃出来了,这是最高的境界了,可喜可贺。确实MSDN中什么都有,只要肯啃8 U3 d2 g: O8 s5 h# [6 J2 K
7 ~0 S$ \: g. ~3 i) K6 Z. o

. K' z( J; a4 q/ }* i0 G/ @在回调函数void WINAPI CDialerDlg::RasDialFunc(UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError)中,不需要再用PostMessage把消息发到主窗口了,只要直接用switch(rasconnstate)就可以输出: Z5 v& n9 v* K7 i) s! {$ T4 L
10:51:59 Message: 正在打开端口,请稍候...
( c6 p" a0 D, ^. d10:51:59 Message: 端口已经打开
$ f8 T( {& d& L) N3 X10:51:59 Message: 正在连接网络设备,请稍候...2 b' \4 z( m( J/ O( U  w
10:51:59 Message: 网络设备连接成功
* @: a0 W" E, }- w$ b6 `/ u- t10:51:59 Message: 网络设备已经准备就绪
( v6 i1 C& ?5 z$ g' v2 B% M+ ]2 S10:51:59 Message: 正在验证用户帐号和口令,请稍候...
& n, J5 j. H! l8 a- R0 R  f10:52:00 Message: 正在接收验证/授权过程通知消息
: V% x+ _6 x) f0 D# t10:52:00 Message: 正在与服务器进行数据交互,请稍候...
% S  B, ~; h7 J3 H! k0 v6 x8 H  v4 l10:52:00 Message: 正在接收验证/授权过程通知消息
/ \2 R% m9 ?9 p% X10:52:00 Message: 交互过程结束& w6 [2 G, K2 u0 M$ {4 M
10:52:00 Message: 正在接收验证/授权过程通知消息
+ E7 l% D6 N; U  b" ?6 g1 X6 `2 |10:52:00 Message: 用户帐号/口令验证通过,正在建立连接2 I- c% G5 W- J% Y. U6 J
10:52:02 Message: PPPoE连接建立成功  ^# Y8 b- W( R) p8 ]8 t+ `4 E
10:52:02 Message: IP -> 192.168.0.22 V' V% P2 \. N( B
10:52:02 Message: IP -> 218.109.100.703 J3 I8 R% w  U) P
10:52:02 Message: IP -> 218.109.98.1699 _8 E: {( i; V# u- t3 a/ Y
  F3 [4 a, m. Q

作者: Eagle    时间: 2003-11-13 14:55
很少接触回调函数。好像回调函数要求使用static型的,可是static型的函数用着很麻烦,还不如把消息post到主窗口再进行分流处理。
作者: hzzh    时间: 2003-11-22 09:11
ok




欢迎光临 下沙论坛 (http://bbs.xiasha.cn/) Powered by Discuz! X3.3