📄 gpsdll.cpp
字号:
// GpsDll.cpp : 定义 DLL 的初始化例程。
//
#include "stdafx.h"
#include "GpsDll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
//TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
// 则从此 DLL 导出的任何调入
// MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
// 该函数的最前面。
//
// 例如:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // 此处为普通函数体
// }
//
// 此宏先于任何 MFC 调用
// 出现在每个函数中十分重要。这意味着
// 它必须作为函数中的第一个语句
// 出现,甚至先于所有对象变量声明,
// 这是因为它们的构造函数可能生成 MFC
// DLL 调用。
//
// 有关其他详细信息,
// 请参阅 MFC 技术说明 33 和 58。
//
// CGpsDllApp
BEGIN_MESSAGE_MAP(CGpsDllApp, CWinApp)
END_MESSAGE_MAP()
// CGpsDllApp 构造
CGpsDllApp::CGpsDllApp()
{
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
m_gpsHandle=NULL;
}
// 唯一的一个 CGpsDllApp 对象
CGpsDllApp theApp;
// CGpsDllApp 初始化
BOOL CGpsDllApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
//开启GPS接收机,并开启一个线程监控两个事件:GPS状态改变事件和新的GPS数据事件
void CGpsDllApp::GpsConnect2(CWnd* owner)
{
//若句柄不为NULL,则说明已连接上,直接返回
if(m_gpsHandle!=NULL)
{
return;
}
m_owner=owner;
m_newLocationHandle = CreateEvent(NULL, 0, 0, NULL);
m_deviceStateChangedHandle = CreateEvent(NULL, 0, 0, NULL);
//stopHandle = CreateEvent(IntPtr.Zero, 0, 0, null);
m_hgpsapi=LoadLibrary(_T("gpsapi.dll"));
if(m_hgpsapi==NULL)
{
AfxMessageBox(_T("加载gpsapi.dll失败!"));
return;
}
//打开GPS Intermediate Driver
HANDLE hGPS = CreateFile(L"GPD0:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hGPS != INVALID_HANDLE_VALUE)
{
DeviceIoControl(hGPS,IOCTL_SERVICE_START,0,0,0,0,0,0);
CloseHandle(hGPS);
}
//打开GPS接收机
FGPSOpenDevice GPSOpenDeviceFun=(FGPSOpenDevice)GetProcAddress(m_hgpsapi,_T("GPSOpenDevice"));
m_gpsHandle = GPSOpenDeviceFun(m_newLocationHandle, m_deviceStateChangedHandle, NULL, 0);
m_threadIsOn=true;
//开启一个线程监控两个事件:GPS状态改变事件和新的GPS数据事件
m_hReadThread=::CreateThread(NULL,0,ReadThreadFun,this,0,&m_dwReadThreadID);
}
DWORD CGpsDllApp::ReadThreadFun(LPVOID lparam)
{
CGpsDllApp* gpsDll=(CGpsDllApp*)lparam;
HANDLE* lpHandles=new HANDLE[2];
lpHandles[0]=gpsDll->m_deviceStateChangedHandle;
lpHandles[1]=gpsDll->m_newLocationHandle;
while(gpsDll->m_threadIsOn)
{
int index=WaitForMultipleObjects(2,lpHandles,false,60000);
if(index==WAIT_FAILED)
{
AfxMessageBox(_T("failed!"));
}
if(index==WAIT_TIMEOUT)
{
//AfxMessageBox(_T("time out!"));
gpsDll->m_gpsState=0;
gpsDll->m_saveGpsPositionAndState(gpsDll->m_owner,new GPS_POSITION(),gpsDll->m_gpsState);
Sleep(5000);
}
if(index==WAIT_OBJECT_0)
{
FGpsGetDeviceState GpsGetDeviceStateFun=(FGpsGetDeviceState)GetProcAddress(gpsDll->m_hgpsapi,_T("GPSGetDeviceState"));
GPS_DEVICE* gpsDevice=new GPS_DEVICE();
GpsGetDeviceStateFun(gpsDevice);
//AfxMessageBox(_T("new state!"));
}
if(index==WAIT_OBJECT_0+1)
{
GPS_POSITION* gpsPosition=new GPS_POSITION();
gpsPosition->dwVersion=GPS_VERSION_1;
gpsPosition->dwSize=sizeof(GPS_POSITION);
FGPSGetPosition GPSGetPositionFun=(FGPSGetPosition)GetProcAddress(gpsDll->m_hgpsapi,_T("GPSGetPosition"));
GPSGetPositionFun(gpsDll->m_gpsHandle,gpsPosition,500000, 0);
if(gpsPosition->dwSatelliteCount==0)
{
//AfxMessageBox(_T("断开连接"));
gpsDll->m_gpsState=0;
gpsDll->m_saveGpsPositionAndState(gpsDll->m_owner,gpsPosition,gpsDll->m_gpsState);
Sleep(5000);
}
else
{
if(gpsPosition->dblLatitude==0&&gpsPosition->dblLongitude==0)
{
//AfxMessageBox(_T("连接,无定位"));
gpsDll->m_gpsState=1;
gpsDll->m_saveGpsPositionAndState(gpsDll->m_owner,gpsPosition,gpsDll->m_gpsState);
Sleep(5000);
}
else
{
//AfxMessageBox(_T("连接,有定位"));
gpsDll->m_gpsState=2;
gpsDll->m_saveGpsPositionAndState(gpsDll->m_owner,gpsPosition,gpsDll->m_gpsState);
Sleep(5000);
}
}
}
}
return 0;
}
void CGpsDllApp::SetSaveGpsPositionAndState2(FSaveGpsPositionAndState saveGpsPositionAndStateFun)
{
m_saveGpsPositionAndState=saveGpsPositionAndStateFun;
}
void CGpsDllApp::GpsDisConnect2()
{
//若句柄为NULL,则说明已断开连接上,直接返回
if(m_gpsHandle==NULL)
{
return;
}
m_threadIsOn=false;
if(WaitForSingleObject(m_hReadThread,1000)==WAIT_TIMEOUT)
{
TerminateThread(m_hReadThread,0);
}
m_hReadThread = NULL;
FGPSCloseDevice GPSCloseDeviceFun=(FGPSCloseDevice)GetProcAddress(m_hgpsapi,_T("GPSCloseDevice"));
GPSCloseDeviceFun(m_gpsHandle);
m_gpsHandle=NULL;
FreeLibrary(m_hgpsapi);
m_hgpsapi=NULL;
}
GPS_DEVICE* CGpsDllApp::GpsGetDeviceState2()
{
if(m_hgpsapi!=NULL)
{
FGpsGetDeviceState GpsGetDeviceStateFun=(FGpsGetDeviceState)GetProcAddress(m_hgpsapi,_T("GPSGetDeviceState"));
GPS_DEVICE* gpsDevice=new GPS_DEVICE();
GpsGetDeviceStateFun(gpsDevice);
return gpsDevice;
}
else
{
m_hgpsapi=LoadLibrary(_T("gpsapi.dll"));
if(m_hgpsapi==NULL)
{
AfxMessageBox(_T("加载gpsapi.dll失败!"));
return NULL;
}
FGpsGetDeviceState GpsGetDeviceStateFun=(FGpsGetDeviceState)GetProcAddress(m_hgpsapi,_T("GPSGetDeviceState"));
GPS_DEVICE* gpsDevice=new GPS_DEVICE();
GpsGetDeviceStateFun(gpsDevice);
FreeLibrary(m_hgpsapi);
m_hgpsapi=NULL;
return gpsDevice;
}
}
void GpsConnect(CWnd* owner)
{
theApp.GpsConnect2(owner);
}
void GpsDisConnect()
{
theApp.GpsDisConnect2();
}
GPS_DEVICE* GpsGetDeviceState()
{
return theApp.GpsGetDeviceState2();
}
void SetSaveGpsPositionAndState(FSaveGpsPositionAndState saveGpsPositionAndStateFun)
{
theApp.SetSaveGpsPositionAndState2(saveGpsPositionAndStateFun);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -