📄 wireless.cpp
字号:
// Wireless.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "Wireless.h"
#include "RegistryEx.h"
#include "SerialPort.h"
#include "Comm.h"
#include <string>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <string.h>
#include <ras.h>
#include <raserror.h>
#pragma comment(lib,"rasapi32.lib")
#include <Winsock2.h>
#pragma comment(lib,"Ws2_32.lib")
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CWirelessApp
BEGIN_MESSAGE_MAP(CWirelessApp, CWinApp)
//{{AFX_MSG_MAP(CWirelessApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWirelessApp construction
CWirelessApp::CWirelessApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CWirelessApp object
BOOL CWirelessApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
m_hGetCSQHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
return CWinApp::InitInstance();
}
//初始化模块
#include <setupapi.h>
#pragma comment(lib,"setupapi.lib")
#include "RasConnection.h"
//////////////////////////////////////////////////
/* typedef DWORD (WINAPI *lpfnDllFuncRGCS)(HRASCONN hRasConn,
RAS_STATS *lpStatistics);
//RasEnumConnections
typedef DWORD (WINAPI *lpfnDllFuncRENCS)(LPRASCONN lprasconn,
LPDWORD lpcb,
LPDWORD lpcConnections);
lpfnDllFuncRGCS lpfnDllFuncConnstat;
lpfnDllFuncRENCS lpfnDllFuncEnumConn;
LPRASDEVINFO m_lpRasDevInfo;
HINSTANCE m_hDLLib;
CString m_strLinkName;
*/
///////////////////////////////////////////////////
CSerialPort m_SerPort;
#include "Datahead.h"
CString m_strDeviceName,m_strModemName;
DEVICEINFO m_DevInfo;
HRASCONN hRasConn = NULL;
NUMBERLIST numberlist;
SMSLIST smslist;
char szModem[64] = "\0",szCom[32] = "\0";
int m_nCom = 0;
int m_nRate = 0;
bool ThreadEnd = false;
int netType = -1;
CWnd *m_MsgHand, *m_CallHand;
BYTE winver = UNKOWNE;
char *g_inifile = NULL;
bool m_DevIsOK = false;
bool m_isGetCSQ = false;
CWinThread *m_ComStateThread;
BOOL GetPcMciaModemInfo(LPCSTR modemid, int modemcom)
{
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i,err;
BOOL Found;
//
// Create a Device Information Set with all present devices.
//
GUID ModemClassGuid = {0x4D36E96DL, 0xE325, 0x11CE, {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};
DeviceInfoSet = SetupDiGetClassDevs(&ModemClassGuid, // All Classes
0,
0,
DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
TRACE(_T("GetClassDevs(All Present Devices)")) ;
return FALSE;
}
//_tprintf(_T("\n\nSearch for Device ID: [%s]\n\n"),HardwareId);
//
// Enumerate through all Devices.
//
Found = FALSE;
i = err = 0;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
//for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
while ( !Found && SetupDiEnumDeviceInfo(DeviceInfoSet, i++, &DeviceInfoData))
{
DWORD DataT;
LPTSTR p,buffer = NULL;
DWORD buffersize = 0;
if(winver == WIN98 || winver == WINME)
{
buffersize = 1024;
buffer = (char *)LocalAlloc(LPTR,buffersize);
}
//
// We won't know the size of the HardwareID buffer until we call
// this function. So call it with a null to begin with, and then
// use the required buffer size to Alloc the nessicary space.
// Keep calling we have success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INVALID_DATA) {
//
// May be a Legacy Device with no HardwareID. Continue.
//
break;
}
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
//
// We need to change the buffer size.
//
if (buffer)
LocalFree(buffer);
buffer = (char *)LocalAlloc(LPTR,buffersize);
}
else
{
//
// Unknown Failure.
//
TRACE(_T("GetDeviceRegistryProperty"));
err = 1;
break;
}
}
if (GetLastError() == ERROR_INVALID_DATA)
continue;
if( err) break;
//
// Compare each entry in the buffer multi-sz list with our HardwareID.
//
for (p=buffer;p&&*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
{
if (!_stricmp(modemid,p))
{
DWORD dwDataType,dwDataLen = 1024;
BYTE bData[1024];
{
HKEY hkey = SetupDiOpenDevRegKey(DeviceInfoSet,&DeviceInfoData,DICS_FLAG_GLOBAL,0,DIREG_DRV, KEY_READ);
if(hkey != INVALID_HANDLE_VALUE)
{
DWORD dwType = REG_SZ;
BYTE szPort[32];
memset(szPort,0,32);
DWORD dwRet = 32;
if(RegQueryValueEx(hkey,"AttachedTo",0,&dwType,szPort,&dwRet) == ERROR_SUCCESS)
{
int nCom = atoi((char *)(szPort+3));
if(nCom != modemcom)
{
RegCloseKey(hkey);
continue;
}
m_nCom = nCom;
}
dwType = REG_BINARY;
BYTE bDCB[128];
memset(bDCB,0,128);
dwRet = 128;
if(RegQueryValueEx(hkey,"DCB",0,&dwType,bDCB,&dwRet) == ERROR_SUCCESS)
{
memcpy(&m_nRate,&bDCB[4],sizeof(DWORD));
}
RegCloseKey(hkey);
ZeroMemory(bData,1024);
if(SetupDiGetDeviceRegistryProperty(DeviceInfoSet,&DeviceInfoData,
SPDRP_FRIENDLYNAME,&dwDataType,bData,dwDataLen,&dwDataLen))
{
m_strModemName = (char *)bData;
}
}
}
Found = TRUE;
break;
}
}
if (buffer) LocalFree(buffer);
if (Found) break;
}
if (GetLastError() != NO_ERROR)
{
TRACE(_T("EnumDeviceInfo %d"),GetLastError());
}
err = GetLastError();
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
SetLastError(err);
return Found;
}
extern "C" __declspec(dllexport) bool InitDevice()
{
CMobileCfg cfg;
char szCurDir[_MAX_PATH];
GetModuleFileName(NULL, szCurDir, _MAX_PATH);
char *p = strrchr(szCurDir, (int)'\\');
if(p == NULL)
return FALSE;
*(p+1) = 0;
strcat(szCurDir, "Mobile.cfg");
int DEVICECNT = cfg.GetCfgFile(szCurDir, NULL);
if(DEVICECNT == 0)
return FALSE;
LPDEVICEINFO DeviceInfo = new DEVICEINFO[DEVICECNT];
ZeroMemory(DeviceInfo, DEVICECNT*sizeof(DEVICEINFO));
cfg.GetCfgFile(szCurDir, DeviceInfo);
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i,err;
bool Found;
//
// Create a Device Information Set with all present devices.
//
DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
0,
0,
DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
TRACE(_T("GetClassDevs(All Present Devices)")) ;
delete [] DeviceInfo;
return false;
}
//
// Enumerate through all Devices.
//
Found = FALSE;
i = err = 0;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
while ( !Found && SetupDiEnumDeviceInfo(DeviceInfoSet, i++, &DeviceInfoData))
{
DWORD DataT;
LPTSTR p,buffer = NULL;
DWORD buffersize = 0;
if(winver == WIN98 || winver == WINME)
{
buffersize = 1024;
buffer = (char *)LocalAlloc(LPTR,buffersize);
}
//
// We won't know the size of the HardwareID buffer until we call
// this function. So call it with a null to begin with, and then
// use the required buffer size to Alloc the nessicary space.
// Keep calling we have success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INVALID_DATA) {
//
// May be a Legacy Device with no HardwareID. Continue.
//
break;
}
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
//
// We need to change the buffer size.
//
if (buffer)
LocalFree(buffer);
buffer = (char *)LocalAlloc(LPTR,buffersize);
}
else
{
//
// Unknown Failure.
//
TRACE(_T("GetDeviceRegistryProperty"));
err = 1;
break;
}
}
if (GetLastError() == ERROR_INVALID_DATA)
continue;
if( err) break;
//
// Compare each entry in the buffer multi-sz list with our HardwareID.
//
for (p=buffer;p&&*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
{
if (Found)
break;
for(int i = 0; i < DEVICECNT; i++)
{
TRACE("[%s] --- [%s]\r\n", DeviceInfo[i].DeviceID, p);
if (!_stricmp(DeviceInfo[i].DeviceID,p))
{
//设备类型
//查找ID对应的设备名;
DWORD dwDataType,dwDataLen = 1024;
BYTE bData[1024];
ZeroMemory(bData,1024);
if(SetupDiGetDeviceRegistryProperty(DeviceInfoSet,&DeviceInfoData,
SPDRP_FRIENDLYNAME,&dwDataType,bData,dwDataLen,&dwDataLen))
{
CString DevName = (char *)bData;
DevName.MakeUpper();
char szDevName[128];
ZeroMemory(szDevName, 128);
strcpy(szDevName, DeviceInfo[i].DevName);
strcpy(szDevName, strupr(szDevName));
if( strncmp(szDevName, DevName, strlen(szDevName)))
continue;
m_strDeviceName = (char *)bData;
}
memcpy(&m_DevInfo, DeviceInfo+i,sizeof(DEVICEINFO));
//串口
if(strcmp(m_DevInfo.DevName, m_DevInfo.ModemName))
{
int pos = m_strDeviceName.Find("(COM");
if(pos != -1)
{
char szCom[12];
strncpy(szCom, m_strDeviceName.Mid( pos+4 ), 12);
int nCom = atoi(szCom);
//读MODEM 信息;
GetPcMciaModemInfo( DeviceInfo[i].ModemID, nCom);
}
}else
{
m_strModemName = (char *)bData;
HKEY hkey = SetupDiOpenDevRegKey(DeviceInfoSet,&DeviceInfoData,DICS_FLAG_GLOBAL,0,DIREG_DRV, KEY_READ);
if(hkey != INVALID_HANDLE_VALUE)
{
DWORD dwType = REG_SZ;
BYTE szPort[32];
memset(szPort,0,32);
DWORD dwRet = 32;
if(RegQueryValueEx(hkey,"AttachedTo",0,&dwType,szPort,&dwRet) == ERROR_SUCCESS)
{
m_nCom = atoi((char *)(szPort+3));
}
dwType = REG_BINARY;
BYTE bDCB[128];
memset(bDCB,0,128);
dwRet = 128;
if(RegQueryValueEx(hkey,"DCB",0,&dwType,bDCB,&dwRet) == ERROR_SUCCESS)
{
memcpy(&m_nRate,&bDCB[4],sizeof(DWORD));
}
RegCloseKey(hkey);
}
}
Found = true;
break;
}
}
}
if (buffer) LocalFree(buffer);
if (Found) break;
}
if (GetLastError() != NO_ERROR)
{
TRACE(_T("EnumDeviceInfo %d"),GetLastError());
}
//
// Cleanup.
//
//char temp[64] = "\0";
// sprintf(temp,"硬件ID:[%s] ,硬件名:[%s] ,硬件类型:[%d] ,接口类型[%d] ,连接类型[%d]",DeviceInfo->DeviceID,DeviceInfo->DevName,DeviceInfo->ModuleType
// ,DeviceInfo->InterfaceType,DeviceInfo->ConnectType);
//AfxMessageBox(temp);
netType = DeviceInfo->ConnectType;
delete [] DeviceInfo;
err = GetLastError();
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
SetLastError(err);
return Found;
}
void GetModemPort()
{
HKEY hKey;
LONG ret;
OSVERSIONINFO osvi;
BOOL bOsVersionInfoEx;
char keyinfo[100],comm_name[200],ValueName[200];
int i;
DWORD sType,Reserved,cbData,cbValueName;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
memset(keyinfo,0,100);
strcpy(keyinfo,"HARDWARE\\DEVICEMAP\\SERIALCOMM");
i=0; sType=REG_SZ;Reserved=0;
bOsVersionInfoEx = GetVersionEx(&osvi);
ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyinfo,0,KEY_ALL_ACCESS,&hKey);
if (ret==ERROR_SUCCESS)
{
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
for(i=1;i<=10;i++)
{
sprintf(comm_name,"COM%d",i);
}
}
else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
cbData=200;cbValueName=200;
memset(comm_name,0,200);
memset(ValueName,0,200);
do {
ret=RegEnumValue(hKey,i++,ValueName,&cbValueName,NULL,&sType,(LPBYTE)comm_name,&cbData);
if (ret==ERROR_SUCCESS || ret == ERROR_MORE_DATA)
{
if (cbValueName > 0 && cbData > 0)
{
CString comstr,strcom;
comstr = comm_name;
strcom = comstr.Right(1);
if (comstr.GetLength() > 4)
strcom = comstr.Right(2);
m_nCom = atoi(strcom);
m_nRate = 460800;
if(StartCom(NULL))//m_pMainDlg->m_port.InitPort(m_pMainDlg, atoi(strcom), 115200,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -