|
本帖最后由 琼ヾ苍 于 2009-3-13 14:36 编辑 ! s `( I L( _* f8 o9 a
. @" E$ @" \, b
小弟在编写一个VC的ping程序,要将其ansi 转换成 unicoede 出现如下图问题:
" N) n, s6 U& E6 t
' f$ N7 {& m9 _& x; ~7 u5 R& Q- s& o" S5 i1 A' K; c
' |& r. ?: F; G% @. T1 U
* g4 p8 T! a$ Q- D; S& n
0 b% `4 l u; F1 V- \该问题让小弟头疼不已... 不得已才发上来求救下:' Y( j* b. D: j! J
我自己调试了下 目前面临的是 pstrHost 这个变量 我发现它仍然是 const char*类型 : A# h2 k1 z1 ~- _+ D7 K
而且我这个程序 在 ping 本地IP地址时可以顺利 测试且不显示乱码,
) r4 t: b: F+ D! O& M; K! a但是 在ping 远程地址(如 www.baidu.com) 就会出现找不到主机问题,
. a$ B9 b3 W1 k! ~经过我调试发现可能是pstrHost这个变量没转换过来....
' u& U7 J0 o2 W0 ` 不过我不知道如何 转换 用MultiByteToWideChar 函数转换也不成功。- i' w* C7 k0 A. B0 s* H, Q
7 y* c/ R- `1 P% k系统学院论坛里有 会VC的高手告诉能帮忙 给些指导意见 谢谢了。& m3 X# N! ~9 E) I# f* ?
' v4 J7 y8 V. h# P// ========以下为相关代码段====================
J$ |% _$ ]0 ?4 O, {& s a; A
( z y m7 G0 J& i7 U9 J, fvoid CPing:ping(UINT nRetries,LPCSTR pstrHost,HWND hWnd)
' b- U& O, Z: r{' t$ E: i# e) K- U/ ^
SOCKET rawSocket;
* l2 V6 O# Y& U3 W0 F LPHOSTENT lpHost;
. ^/ ^& V9 W+ R2 z3 {+ B; k9 _( U UINT nLoop;
; p B. J' n) P2 W/ {* {0 s int nRet;6 C' C- u/ E% F: z
struct sockaddr_in saDest;
1 ^* t: _$ W- o struct sockaddr_in saSrc;
3 R' q3 T2 A# `- v0 Q DWORD dwTimeSent;
6 t! C" ^4 s$ J" G1 K6 g0 |* p DWORD dwElapsed;8 X! o( U) D. |
u_char cTTL;
& p% b+ V8 d* g3 ~, {6 {" \
' o* J5 M7 J, W
* B T6 ]. `) F0 s! ] m_hWnd = hWnd;# x7 i8 f* y. H3 F" Q: `$ u ^: N
: C' ]2 e" A* ]
CString str;
8 Z3 a6 k0 x6 P0 O" @( b# L% b" u) C. d; t1 A; @1 X7 n
ASSERT(IsWindow(hWnd));1 s- C, J5 B0 `! k) Y' s
8 {1 T5 T4 ~! L2 m2 c
//创建一个Raw套节字
& q* O. O/ Q! k% H2 ]( d9 o z rawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
! U. r# |( n8 [ if (rawSocket == SOCKET_ERROR)
) Q$ j# ?( y9 f; \" |& q e& h {
) y, P* U1 | d8 C; u' H4 ~/ e, B WSAError("socket()");' G! B3 |1 m! W' g0 S. G. x" L
return;
8 T* S$ r* t$ E( A& J( w p }; N6 U! O2 g& y# J
9 O* W$ d Q7 ^0 C
//获得主机信息
$ Z% d% k+ i( x$ M& R$ J9 e# ~ lpHost = gethostbyname(pstrHost);1 @! P3 r1 j3 |" u2 M" N
& t( j8 ]0 u% I. |% s1 G
if (lpHost == NULL)! C; m! g W/ z
{5 w) @. `. T& |; t' D% h4 ~2 d2 H' C
str.Format(_T("Host not found: %s"),w);# A8 a& J' c8 [- H7 |
:postMessage(m_hWnd,WM_MSG_STATUS, 0, (LPARAM) AllocBuffer(str));
1 ]. h" R( k( |6 c :postMessage(m_hWnd,WM_PING_END, 0, 0);& D% |; }0 `' T, E( C
return;
' Y7 V/ { H. z& y. e4 J }
7 m9 y& n, L( u$ O) c' l 8 N9 [% [0 Z3 j# y4 C) W( S
//构造目标套节字地址信息
$ s, Y) C- r% o# w! e; s saDest.sin_addr.s_addr =
0 y' A9 t) o( v O& m*((u_long FAR *) (lpHost->h_addr));
( D+ I# Y6 L, v3 h saDest.sin_family = AF_INET;: A4 J5 a2 j4 z7 y
saDest.sin_port =) K) Y( F5 Q/ a8 b& n3 @
0;
9 A1 Q4 t. N9 @1 M* Q/ m1 U/ [9 U4 b3 ]2 D( a6 G3 t- q
//告诉用户我们现在的工作
; n4 [/ w. P/ ~6 Y) K str.Format(_T("Pinging %s [%s] with %d bytes of data:"),- B' \ E8 y5 m/ y9 p$ s8 `
w,2 f- X4 n- {4 \5 F; r) r
CString(inet_ntoa(saDest.sin_addr)),
# ^7 B6 c. r/ \" [6 N REQ_DATASIZE);
. {! [/ I5 W; s% v :postMessage(m_hWnd,WM_MSG_STATUS, 1, (LPARAM) AllocBuffer(str));, q& {7 h3 J" w8 z3 {
& j1 j- _0 A( z" t$ M& w
//多次ping
( T9 A0 }! D4 S) U7 n2 T6 K+ T: i! `! e! V; _9 _+ K
for (nLoop =
5 ]7 }. {! J- s0; nLoop < nRetries; nLoop++)
6 c' I6 K5 _- [. M5 {$ s2 v { q/ E4 w* X3 Y# w" C+ R( t
//发送ICMP回应请求- N( U3 q `4 j; g
SendEchoRequest(rawSocket, &saDest);( l! j& l( U* B# {
/ b1 l! Z$ O2 v+ G# |; x4 A: N9 X
nRet = WaitForEchoReply(rawSocket);
/ A/ }& g; ~6 n+ J if (nRet == SOCKET_ERROR)- p4 r! ~2 C- X6 q/ b
{
, l9 a) Z/ F$ \9 k X+ R- ` WSAError("select()");( O3 J1 G+ Y. ~ R
break;
0 K a" ^' r$ c# E( ]0 s1 } }
# r _, L( |) Q' ` if (!nRet); X6 J+ G$ g8 }" r$ J7 j% v
{
) V7 [' \7 B( m1 q# ?, m( ?7 [, g str.Format(_T("Request Timed Out"));
+ [( }- K5 [ R7 |/ F* H :postMessage(m_hWnd,WM_MSG_STATUS, 3, (LPARAM) AllocBuffer(str));
9 e( g% _5 _; S* o* f- I* F }$ U$ X7 j3 E& i8 }8 b$ M
else% I6 k8 o2 N+ i, |4 w
) N3 n0 Z5 v @ U4 c: T- a" ~" `
{- s2 V% k; E, x% B8 y
//获得回应
$ V- V9 I4 C" \* R8 y dwTimeSent = RecvEchoReply(rawSocket, &saSrc, &cTTL);9 u9 ^8 v. l5 X4 ]* d4 N# g, @/ j
( r* C& {% r8 f/ c( A' F5 {% r
//计算时间
3 h' {" C5 W# {& P, H* a dwElapsed = GetTickCount() - dwTimeSent;
& q& C" o( a$ v str.Format(_T("Reply[%d] from: %s: bytes=%d time=%ldms TTL=%d"),
$ \- b v- a/ }* |3 n nLoop+1,
# e5 W" Q- X$ S4 S9 }+ ] CString(inet_ntoa(saSrc.sin_addr)), 3 l1 l% V: `/ p
REQ_DATASIZE,0 z5 j6 F+ m5 q: Z0 y( E
dwElapsed,
- F7 m" N! e2 y, C3 t' J8 D cTTL);
& v v9 u8 {* G
# [$ s. v) y# D: a' T& [# W% J :postMessage(m_hWnd,WM_MSG_STATUS, 2, (LPARAM) AllocBuffer(str));) ?" w( s; q$ y. K3 {" C8 ^
; l/ q1 @9 E. n% {6 c+ w: W2 r8 j1 \ Sleep(1000);: k) l# \0 C4 F) c
}
" N; o& e# u: f) m( N5 { f }
* X3 {+ r5 V: _8 ^& v0 |- T
5 ?. A% F. I, j" u' J; \' V8 l :postMessage(m_hWnd,WM_PING_END, 0, 1);
. ?1 z8 m3 @' a% h
' I8 L, b3 Z8 D0 U nRet = closesocket(rawSocket);
( h0 a: \; x1 {" m if (nRet == SOCKET_ERROR)
" p) }6 E+ K( L6 Q( D WSAError("closesocket()");
8 I3 ?' O* n6 _$ s4 V# `5 J5 l
6 c$ v. O7 X& t9 g* ]}
- H4 n% ^0 D+ W! d, C2 z6 K; `" g5 |! X7 Y |
|