对无线网卡信号强度提取的程序中的一点问题(附源码)

ti365 2005-07-12 04:39:42
程序功能:提取无线网卡的信号强度
问题:设备不能打开
无线网卡驱动的系统文件打不开,创建不了相关的设备hDevice
在DeviceIoControl中 参数IOCTL_GET_INFO 不知怎么定义

请各位赐教,谢谢!!


readSS.cpp

#include "StdAfx.h"
#include <stdio.h>
#include <windows.h>
#include "readSS.h"


#define __NOT_WINCE__

#ifdef __NOT_WINCE__
#define SPRINT sprintf
#define STRCPY strcpy
#define STRCAT strcat
#define STRLEN strlen

#else //! __NOT_WINCE__
#define SPRINT swprintf
#define STRCPY wcscpy
#define STRCAT wcscat
#define STRLEN wcslen
#endif //__NOT_WINCE__

#define ShowMessage(str) \
::MessageBox( NULL, TEXT(str), TEXT("Warning"),MB_OKCANCEL )

#define ShowMessageFmt(str) TCHAR lpCaption[260] ;\
SPRINT(lpCaption ,TEXT("%s") , TEXT(str) , GetLastError() ) ; \
ShowMessage(lpCaption );


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++SteP4++++++++++++++++++++++++++++++
//IoControlOpenCall(hDevice);
//++++++++++++++++++++SteP4++++++++++++++++++++++++++++++
#define IOCTL_GET_INFO 0x00000017//0x170002 //0x170002 (EX_AC_BD_CLOSE_ERR) Bd close err = %d, bd # = %d, phy mod # = %d, bd type = %d
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//此处如何定义满足要求!


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++GetApInfo+++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++GetApInfo+++++++++

void CreadSS::GetApInfo(/*HWND hWnd*/ )
{
HANDLE hDevice ;
//++++++++++++++++++++SteP1++++++++++++++++++++++++++++++
hDevice = CreateFile("\\\\.\\C:\\WINDOWS\\system32\\drivers\\sis162u.sys",//"\\\\?\\H:\\winnt\\system32\\drivers\\wlluc48.sys",//m_pDriverName,//driverStr,
0,
FILE_SHARE_READ|FILE_SHARE_WRITE,
0,//NULL,
OPEN_EXISTING,
0,
0//NULL
);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 此处创建不了 hDevice;参数设置不正确。
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//C:\\WINDOWS\\system32\\drivers\\sis162u.sys







//++++++++++++++++++++SteP2++++++++++++++++++++++++++++++
if( hDevice == INVALID_HANDLE_VALUE )
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );

LocalFree( lpMsgBuf );

ShowMessageFmt("Can't open the driver getApinfo [%d]" );
return ;
}

//++++++++++++++++++++SteP3++++++++++++++++++++++++++++++
memset(m_pOutBuf, 0x00, m_dwOutBuf_Size );


//++++++++++++++++++++SteP4++++++++++++++++++++++++++++++
IoControlOpenCall(hDevice);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//参数出错,hDevice;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




//++++++++++++++++++++SteP5++++++++++++++++++++++++++++++
if ( (m_pOutBufW[0] !=0 ) | (m_pOutBufW[1] !=0 ) )
{
// Driver ready for action

do{
// Read access point scan results
//++++++++++++++++++++SteP5.A++++++++++++++++++++++++++++++
IoControlCall(hDevice, 0x820, 0x5DD);

//++++++++++++++++++++SteP5.B++++++++++++++++++++++++++++++
PrintResult(/*hWnd*/);

} while( (m_dwBytesReturned>12) && (m_dwBytesReturned<=0x5DD) ) ;
// while( (m_dwBytesReturned<=12) | (m_dwBytesReturned>0x5DD) ) ;
// Broadcast the scan request

//++++++++++++++++++++SteP6++++++++++++++++++++++++++++++
IoControlScanCall(hDevice);

}
// Tell the driver we are done.

//++++++++++++++++++++SteP7++++++++++++++++++++++++++++++
IoControlCloseCall(hDevice);

//++++++++++++++++++++SteP8++++++++++++++++++++++++++++++
CloseHandle(hDevice);

tCount++;

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++GetApInfo+++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++GetApInfo+++++++++




//++++++++++++++++++++SteP4++++++++++++++++++++++++++++++
//IoControlOpenCall(hDevice);
//++++++++++++++++++++SteP4++++++++++++++++++++++++++++++
void CreadSS::IoControlOpenCall(HANDLE hDevice )
{
BOOL iDeviceIoControlFlag=false;

iDeviceIoControlFlag = DeviceIoControl (hDevice,
(DWORD)( IOCTL_GET_INFO ) ,//没有定义 IOCTL_GET_INFO
//IOCTL_GET_INFO,
&m_dwInBuf,sizeof(m_dwInBuf),
&m_pOutBuf,sizeof(m_pOutBuf),
&m_dwBytesReturned,
(LPOVERLAPPED)NULL
);

if( !iDeviceIoControlFlag )
{
LPVOID lpMsgBuf;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );

LocalFree( lpMsgBuf );
//The LocalFree function frees the specified local memory object and invalidates its handle.
//This function is provided only for compatibility with 16-bit versions of Windows.


ShowMessageFmt("DeviceIoControl Error [%d]" );
}

}
...全文
143 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Juchiyufei 2006-04-07
好乱呀.
回复
ti365 2005-07-12
readSS.h

#pragma once

#ifndef __readSS_H__
#define __readSS_H__

#ifdef __cplusplus
extern "C"
{
#endif

struct CSignal
{
CString MACAdress;
int Channel;
int SignalStrength;
int noise;
CString networkID;
};


class CreadSS
{
private :

TCHAR m_pDriverName[260] ;
LPCTSTR driverStr;

#define m_dwOutBuf_Size 0x200B//0x4016
BYTE m_pOutBuf[m_dwOutBuf_Size] ;

DWORD m_dwInBuf;
DWORD m_dwBytesReturned;

private :

void IoControlCall(HANDLE,WORD, WORD );
void IoControlOpenCall(HANDLE);
void IoControlScanCall(HANDLE);
void IoControlCloseCall(HANDLE);


void PrintResult();
void FindDriverName();

public :

CreadSS(void);
~CreadSS(void);
WORD* m_pOutBufW;
int tCount;
TCHAR lpszText[260],lpszText1[260],lpszText2[260];
TCHAR lpszText3[1000],temp[260];
TCHAR lpCaption[260];
CSignal signal[10];

void GetApInfo();
void FormCreate(/*CObject*/);
// void TimerTimer( /*HWND CObject*/);

};

#ifdef __cplusplus
}

#endif

#endif //__readSS_H__
回复
ti365 2005-07-12
//++++++++++++++++++++SteP7++++++++++++++++++++++++++++++
//IoControlCloseCall(hDevice);
//++++++++++++++++++++SteP7++++++++++++++++++++++++++++++
void CreadSS::IoControlCloseCall(HANDLE hDevice )
{
BOOL iDeviceIoControlFlag=false ;

m_pOutBufW[2] = 0x45;
m_pOutBufW[3] = 1;
m_pOutBufW[4] = 0;
m_pOutBufW[5] = 0;

iDeviceIoControlFlag = DeviceIoControl (hDevice,
//(DWORD)( IOCTL_GET_INFO ) ,
IOCTL_GET_INFO,
&m_dwInBuf,sizeof(m_dwInBuf),
&m_pOutBuf,sizeof(m_pOutBuf),
&m_dwBytesReturned,
0
) ;

if( !iDeviceIoControlFlag )
{
ShowMessageFmt("DeviceIoControl Error [%d]" );
}

}
//++++++++++++++++++++SteP7++++++++++++++++++++++++++++++
//IoControlCloseCall(hDevice);
//++++++++++++++++++++SteP7++++++++++++++++++++++++++++++




//++++++++++++++++++++SteP8++++++++++++++++++++++++++++++
//CloseHandle(hDevice);

//The CloseHandle function closes an open object handle.
//BOOL CloseHandle(
// HANDLE hObject // handle to object to close
//);
//++++++++++++++++++++SteP8++++++++++++++++++++++++++++++












//++++++++++++++++++++SteP01++++++++++++++++++++++++++++++
//CreadSS::CreadSS(void)
//++++++++++++++++++++SteP01++++++++++++++++++++++++++++++
CreadSS::CreadSS(void)
{
m_pOutBufW = (WORD*)( &m_pOutBuf[0] ) ;
m_dwInBuf=0xFF020401;
//driverStr="\\\\.\\{5ECE4190-B085-4375-8B8A-40C8FAED1002}";
tCount=0;
//FindDriverName();
//driverStr="\\\\.\\{0D8B9FB3-1966-4FFC-8D66-CA34AA1FA5C2}";
// driverStr="\\\\.\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}";
// driverStr="C:\\WINDOWS\\system32\\drivers\\sis162u.sys";
driverStr="\\\\.\\{{B8265A68-AD8D-41C3-AE9F-702B7893E44A}}";
}

CreadSS::~CreadSS(void)
{
}
//++++++++++++++++++++SteP01++++++++++++++++++++++++++++++
//CreadSS::CreadSS(void)
//++++++++++++++++++++SteP01++++++++++++++++++++++++++++++





//++++++++++++++++++++SteP02++++++++++++++++++++++++++++++
//void CreadSS::FindDriverName()
//++++++++++++++++++++SteP02++++++++++++++++++++++++++++++
void CreadSS::FindDriverName()
{
HKEY hKey;
// char driverString[0xFFFF];
TCHAR lpDriverName[260] = TEXT("") ;
TCHAR szInfo[] =
TEXT("SYSTEM\\CurrentControlSet\\Enum\\USB\\Vid_0b3b&Pid_1613\\5&3ae78c2c&0&1") ;
// TEXT("SYSTEM\\CurrentControlSet\\Enum\\PCMCIA\\Lucent_Technologies-WaveLAN/IEEE-C908\\1") ;

//HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\Vid_0b3b&Pid_1613\5&3ae78c2c&0&1

LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
//"SYSTEM\\CurrentControlSet\\Enum\\PCMCIA\\Lucent_Technologies-WaveLAN/IEEE-C908\\1",
szInfo, // Address of subkey name
(DWORD) 0, // Reserved value
KEY_QUERY_VALUE, // Desired security access
&hKey); // Opened handle buffer

// STRCPY( m_pDriverName, lpDriverName);

if (lRet != ERROR_SUCCESS)
{
ShowMessage("Can't find WaveLan card"); return ;
}

DWORD dwKeyType = REG_SZ;
TCHAR lpstr[261] ;
DWORD dwKeySize = 260 ;

lRet = RegQueryValueEx(hKey,
"driver",
NULL,
&dwKeyType,
(LPBYTE)&lpstr[0],
&dwKeySize);

TCHAR szInfo1[0xFFFF];
STRCPY(szInfo1,"SYSTEM\\CurrentControlSet\\Control\\Class\\");
STRCAT(szInfo1,lpstr);

if (lRet != ERROR_SUCCESS)
{
ShowMessage("Can't find WaveLan card driver"); return ;
}
else{

RegCloseKey(hKey);
}

lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
szInfo1, // Address of subkey name
(DWORD) 0, // Reserved value
KEY_QUERY_VALUE, // Desired security access
&hKey); // Opened handle buffer

if (lRet != ERROR_SUCCESS)
{
ShowMessage("Can't find WaveLan card driver Name"); return ;
}


lRet = RegQueryValueEx(hKey,
"NetCfgInstanceId",
NULL,
&dwKeyType,
(LPBYTE)&lpstr[0],
&dwKeySize);

if (lRet != ERROR_SUCCESS)
{
ShowMessage("Can''t load WaveLan driver name");
}
else
{
STRCPY( lpDriverName, TEXT("\\\\.\\") ) ;
STRCAT( lpDriverName , lpstr );
}

RegCloseKey( hKey );
STRCPY( m_pDriverName ,lpDriverName );

if( STRLEN( m_pDriverName ) == 0)
{
ShowMessage("Error finding WaveLan driver");
}

}
//++++++++++++++++++++SteP02++++++++++++++++++++++++++++++
//void CreadSS::FindDriverName()
//++++++++++++++++++++SteP02++++++++++++++++++++++++++++++



//++++++++++++++++++++SteP03++++++++++++++++++++++++++++++
//void CreadSS::FormCreate
//++++++++++++++++++++SteP03++++++++++++++++++++++++++++++
void CreadSS::FormCreate( /*CObject lpObject*/ )
{
m_pOutBufW = (WORD*)( &m_pOutBuf[0] ) ;
m_dwInBuf= 0xFF020401;
// driverStr="\\\\.\\{5ECE4190-B085-4375-8B8A-40C8FAED1002}";
// driverStr="\\\\.\\{0D8B9FB3-1966-4FFC-8D66-CA34AA1FA5C2}";

driverStr="\\\\.\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}";


FindDriverName();
}
//++++++++++++++++++++SteP03++++++++++++++++++++++++++++++
//void CreadSS::FormCreate
//++++++++++++++++++++SteP03++++++++++++++++++++++++++++++

回复
ti365 2005-07-12
//++++++++++++++++++++SteP5.A++++++++++++++++++++++++++++++
//IoControlCall(hDevice, 0x820, 0x5DD);
//++++++++++++++++++++SteP5.A++++++++++++++++++++++++++++++
void CreadSS::IoControlCall(HANDLE hDevice ,WORD wCode , WORD wLength )
{
BOOL iDeviceIoControlFlag=false ;
//m_dwInBuf= 0xFF020401;

m_pOutBufW[2] = 0;
m_pOutBufW[3] = 4;
m_pOutBufW[4] = wLength;
m_pOutBufW[5] = wCode;

iDeviceIoControlFlag =DeviceIoControl (hDevice,
//(DWORD)( IOCTL_GET_INFO ) ,
IOCTL_GET_INFO,
&m_dwInBuf,sizeof(m_dwInBuf),
&m_pOutBuf,sizeof(m_pOutBuf),
&m_dwBytesReturned,
0);

if( !iDeviceIoControlFlag )
{
ShowMessageFmt("DeviceIoControl Error [%d]" );
}

}
//++++++++++++++++++++SteP5.A++++++++++++++++++++++++++++++
//IoControlCall(hDevice, 0x820, 0x5DD);
//++++++++++++++++++++SteP5.A++++++++++++++++++++++++++++++







//++++++++++++++++++++SteP5.B++++++++++++++++++++++++++++++
//PrintResult(/*hWnd*/);
//++++++++++++++++++++SteP5.B++++++++++++++++++++++++++++++
void CreadSS::PrintResult(/*HWND hWnd*/ )
{
int i, j =0;
int APCount = (m_dwBytesReturned-12) / 50;
CString str1,str2;


// TCHAR lpszText[260],lpszText1[260],lpszText2[260];
// TCHAR lpCaption[260];
//HDC hDC ;

//lpszText3[]="";

if( APCount == 0 ) return ;
//hDC = GetDC(hWnd);

//for( i=0;i <APCount -1; i++)

STRCPY(lpszText3,"");

for( i=0;i<6;i++)
{
j = 6+i*25 ;

#define Swap_Val(a,b) (((int)(((BYTE*)(&a))[b])))
#define Swap(a) (Swap_Val(a,0)<<8) | Swap_Val(a,1)

// MAC address of access point
/*SPRINT(lpCaption, TEXT("%.4X%.4X%.4X "),
Swap(m_pOutBufW[j+3]), Swap(m_pOutBufW[j+4]), Swap(m_pOutBufW[j+5]) );*/

SPRINT(lpCaption,"%.4X%.4X%.4X ",Swap(m_pOutBufW[j+3]), Swap(m_pOutBufW[j+4]), Swap(m_pOutBufW[j+5]) );

signal[i].MACAdress=lpCaption;
signal[i].Channel=m_pOutBufW[j];
signal[i].SignalStrength=m_pOutBufW[j+2];
signal[i].noise=m_pOutBufW[j+1];
signal[i].networkID=(TCHAR*)(&m_pOutBuf[j*2+18]);

str1=lpCaption;
STRCAT(lpszText3,str1);

SPRINT(temp, "%ld %ld %ld \n",
m_pOutBufW[j], m_pOutBufW[j+2], m_pOutBufW[j+1]);
str2=temp;
STRCAT(lpszText3,str2);


SPRINT(lpszText,//lpCaption,
TEXT(" %ld %ld %ld %ld %ld"), //%s %d %ld %d
tCount,
j,//m_dwBytesReturned,
m_pOutBufW[6], // Channel
m_pOutBufW[7], // Signal
m_pOutBufW[8]);
/*m_pOutBufW[j], // Channel
m_pOutBufW[j+2], // Signal
m_pOutBufW[j+1]*///, // Noise
/*(TCHAR*)(&m_pOutBuf[j*2+18]) ); */// Net ID

SPRINT(lpszText1,//lpCaption,
TEXT(" %ld %ld %ld %ld %ld "), //%s %ld

tCount,
m_dwBytesReturned,
m_pOutBufW[31],
m_pOutBufW[32],
m_pOutBufW[33]

/*,
m_pOutBufW[j],
m_pOutBufW[j+2],
m_pOutBufW[j+1]*///,
/*(TCHAR*)(&m_pOutBuf[j*2+18])*/ );

SPRINT(lpszText2,TEXT(" %ld %ld %ld %ld %ld "), //%s
tCount,
m_dwBytesReturned,
m_pOutBufW[56],
m_pOutBufW[57],
m_pOutBufW[58]//,
/*(TCHAR*)(&m_pOutBuf[j*2+18])*/ );

/*TextOut(hDC,10,10,lpCaption, STRLEN(lpCaption));
TextOut(hDC,10,60,lpszText, STRLEN(lpszText) );
TextOut(hDC,10,80,lpszText1, STRLEN(lpszText1) );
TextOut(hDC,10,100,lpszText2, STRLEN(lpszText2) );
*/

}

// ReleaseDC(hWnd,hDC);
}

//++++++++++++++++++++SteP5.B++++++++++++++++++++++++++++++
//PrintResult(/*hWnd*/);
//++++++++++++++++++++SteP5.B++++++++++++++++++++++++++++++





//++++++++++++++++++++SteP6++++++++++++++++++++++++++++++
//IoControlScanCall(hDevice);
//++++++++++++++++++++SteP6++++++++++++++++++++++++++++++
void CreadSS::IoControlScanCall(HANDLE hDevice )
{
BOOL iDeviceIoControlFlag=false;

m_pOutBufW[2] = 0;
m_pOutBufW[3] = 2;
m_pOutBufW[4] = 0x800;
m_pOutBufW[5] = 6;

iDeviceIoControlFlag =DeviceIoControl (hDevice,
//(DWORD)( IOCTL_GET_INFO ) ,
IOCTL_GET_INFO,
&m_dwInBuf,sizeof(m_dwInBuf),
&m_pOutBuf,sizeof(m_pOutBuf),
&m_dwBytesReturned,
0//(LPOVERLAPPED)NULL
) ;

if( !iDeviceIoControlFlag )
{
ShowMessageFmt("DeviceIoControl Error [%d]" );
}

}
//++++++++++++++++++++SteP6++++++++++++++++++++++++++++++
//IoControlScanCall(hDevice);
//++++++++++++++++++++SteP6++++++++++++++++++++++++++++++

回复
相关推荐
发帖
网络编程
创建于2007-09-28

1.8w+

社区成员

VC/MFC 网络编程
申请成为版主
帖子事件
创建了帖子
2005-07-12 04:39
社区公告
暂无公告