📄 gtmpeg.cpp
字号:
// GtMpeg.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "GtMpeg.h"
#include "lib\ViewCtl.h"
#include "lib\RecCtl.h"
#include "GtMpegWnd.h"
#include "SplashWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char g_cRegistryPath[_REGISTRY_PATH_LENGTH];
DWORD g_dwNumberOfSlots;
/////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////
BEGIN_MESSAGE_MAP(CGtMpegApp, CWinApp)
//{{AFX_MSG_MAP(CGtMpegApp)
//}}AFX_MSG
END_MESSAGE_MAP()
////////////////////////////////////
// CGtMpegApp construction
////////////////////////////////////
CGtMpegApp::CGtMpegApp()
{
m_pSplash=NULL;
m_bShutDown=TRUE;
m_bRecord=FALSE;
m_bNetTran=TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CGtMpegApp object
/////////////////////////////////////////////////////////////////////////////
CGtMpegApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CGtMpegApp initialization
/////////////////////////////////////////////////////////////////////////////
BOOL CGtMpegApp::InitInstance()
{
AfxEnableControlContainer();
#ifdef _AFXDLL
Enable3dControls();
#else
Enable3dControlsStatic();
#endif
if(!CreateMutexRun(m_pszExeName))
return FALSE;
//获取屏幕分辨率设置
CClientDC pDC(NULL);
int MaxHorzSize=pDC.GetDeviceCaps(HORZRES);
int MaxVertSize=pDC.GetDeviceCaps(VERTRES);
int MaxPixelBits=pDC.GetDeviceCaps(BITSPIXEL);
if(MaxHorzSize!=1024&&MaxVertSize!=768)
{
AfxMessageBox("请将屏幕设置为1024*768模式!");
return FALSE;
}
if(MaxPixelBits!=16)
{
AfxMessageBox("请将屏幕颜色数设置为增强16位色!");
return FALSE;
}
SetRegistryKey(_T("GtMpeg_Reg"));
CString strKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
CString CurrentDirectory=GetAppPath()+"GtMpeg.exe";
#ifndef _DEBUG
SetRegValueString(HKEY_LOCAL_MACHINE,strKey,"GtMpeg",CurrentDirectory);
#endif
//
if(GetAppPath()=="")
{
AfxMessageBox("请重新安装软件!");
return FALSE;
}
//
CFileFind Find;
CString sSplashFile=GetAppPath()+"FacePlug\\Splash.jpg";
if(!Find.FindFile(sSplashFile))
{
AfxMessageBox("没有发现"+sSplashFile);
return FALSE;
}
m_pSplash = new CSplashWnd(sSplashFile);
m_pSplash->Create();
if(!CheckFile())
return FALSE;
m_nSystemFlag=0; //0-98,1-95,2-nt40;
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(!GetVersionEx (&osvi))
return FALSE;
BOOL bIsWindows98orLater = (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
( (osvi.dwMajorVersion > 4) ||
( (osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0) ) );
if(bIsWindows98orLater)
m_nSystemFlag=0; //win98
else if(osvi.dwPlatformId==VER_PLATFORM_WIN32_NT)
m_nSystemFlag=2; //winnt
else if(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&osvi.dwMinorVersion ==0)
m_nSystemFlag=1; //win95
else
{
AfxMessageBox("请在WinNT4.0,Win95,Win98上运行!");
return FALSE;
}
//
m_bAlarm=GetProfileInt("系统参数","联动报警",0);
m_cHueDefault=GetProfileInt("缺省值","色调",0);
m_cContrastDefault=GetProfileInt("缺省值","对比度",75);
m_cBrightDefault=GetProfileInt("缺省值","亮度",45);
m_cSaturationDefault=GetProfileInt("缺省值","饱和度",64);
m_nChannels=GetProfileInt("系统参数","用户频道数",8);
m_nMaxChannels=GetProfileInt("系统参数","系统最大频道数",8);
m_bRecord=GetProfileInt("系统参数","录像",1);
m_nAlarmTimer=GetProfileInt("系统参数","报警持续时间",60)*1000;
m_nAlarmRecordTimer=GetProfileInt("系统参数","报警录像持续时间",300)*1000;
m_nExitDelay=GetProfileInt("系统参数","退出延时",30);
m_nEnterDelay=GetProfileInt("系统参数","进入延时",30);
m_nChunnel=GetProfileInt("系统参数","报警通道",1);
for (int i=0; i<m_nChannels; i++)
{
g_bSlotOpened[i] = FALSE;
g_pDevice[i] = NULL;
}
if(m_bRecord)
{
strcpy(g_cRegistryPath,"Software\\Winbond\\WMPEG200");
if (VIEWCTL_Init(g_cRegistryPath) != ERR_OK)
{
AfxMessageBox("注册压缩卡失败!");
return FALSE;
}
}
if (!AfxSocketInit())
{
AfxMessageBox("初始WinSocket失败!");
return FALSE;
}
WinsockInit();
//
CGtMpegWnd* pDlg=new CGtMpegWnd;
m_pMainWnd=pDlg;
if(m_bRecord)
{
int iReturnCode, iLoop;
if( (iReturnCode = RECCTL_Init( m_pMainWnd->m_hWnd, &g_dwNumberOfSlots ,(LPBYTE)g_cRegistryPath)) != ERR_OK )
{
if( iReturnCode == ERR_INITIAL_RECCTL_INVALID)
{
AfxMessageBox("初始压缩卡失败!");
return FALSE;
}
}
if (g_dwNumberOfSlots > (DWORD)m_nChannels)
g_dwNumberOfSlots = (DWORD)m_nChannels;
for( iLoop = 0; iLoop < (int) g_dwNumberOfSlots; iLoop ++ )
{
if( (iReturnCode = RECCTL_SetBufferFunction(iLoop, (COMMON_PHandleBufferFunction) VIEWCTL_HandleVideoBuffer)) != ERR_OK )
{
MessageBox( NULL, "建立实时缓冲区失败!", "信息", MB_OK );
return FALSE;
}
}
if (g_dwNumberOfSlots == 0)
{
AfxMessageBox("没有发现压缩卡!");
return FALSE;
}
}
else
g_dwNumberOfSlots=m_nChannels;
::SetProp(m_pMainWnd->GetSafeHwnd(),m_pszExeName,(HANDLE)1);
return CreatMpeg();
}
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
BOOL CGtMpegApp::CreateMutexRun(CString sFlag)
{
::CreateMutex(NULL,TRUE,sFlag);
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
CWnd *pPrevWnd=CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
while(pPrevWnd)
{
if(::GetProp(pPrevWnd->GetSafeHwnd(),sFlag))
return FALSE;
pPrevWnd=pPrevWnd->GetWindow(GW_HWNDNEXT);
}
return FALSE;
}
return TRUE;
}
//////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////
BOOL CGtMpegApp::CreatMpeg()
{
CGtMpegWnd* pDlg=(CGtMpegWnd*)m_pMainWnd;
if(pDlg->DoModal()==IDOK)
{
#ifndef _DEBUG
BOOL result;
if(m_bShutDown)
{
if(m_nSystemFlag==2)
{
result=SetPrivilegeInAccessToken();
if(!result)
AfxMessageBox("添加权力错误!");
result=InitiateSystemShutdown(NULL,"系统将关闭",5,TRUE,FALSE);
if(!result)
AfxMessageBox("重新启动错误!");
}
else
ExitWindowsEx(EWX_SHUTDOWN,0);
}
#endif
return TRUE;
}
return TRUE;
}
//////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
int CGtMpegApp::ExitInstance()
{
return CWinApp::ExitInstance();
}
//////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
BOOL CGtMpegApp::CheckFile()
{
CFileFind Find;
CString sIniFile=GetAppPath()+"GtMpeg.ini";
if(!Find.FindFile(sIniFile))
{
AfxMessageBox("没有发现"+sIniFile);
return FALSE;
}
int nFiles=GetPrivateProfileInt("List","Files",0,sIniFile);
if(nFiles==0)
{
AfxMessageBox(sIniFile+"已损坏,请与供应商联系");
return FALSE;
}
CString sKey,sFileName;
for(int i=0;i<nFiles;i++)
{
sFileName="";
sKey.Format("File%d",i+1);
sFileName=GetIniFileString("List",sKey,sIniFile);
if(sFileName=="")
{
AfxMessageBox(sIniFile+"已损坏,请与供应商联系");
return FALSE;
}
else
{
sFileName=GetAppPath()+sFileName;
if(!Find.FindFile(sFileName))
{
AfxMessageBox("没有发现"+sFileName);
return FALSE;
}
}
}
return TRUE;
}
//////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////
CString CGtMpegApp::GetIniFileString(CString sSection,CString sKey,CString sIniFileName)
{
char strRet[MAX_PATH];
for(int i=0;i<MAX_PATH;i++)
strRet[i]=0;
GetPrivateProfileString(sSection,sKey,"",strRet,MAX_PATH,sIniFileName);
CString sRet=strRet;
return sRet;
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CGtMpegApp::WinsockInit()
{
WSADATA WSAData;
memset( &WSAData, 0, sizeof( WSADATA ) );
WORD wversionrequested = MAKEWORD( 2, 0 );
int nReturn = ::WSAStartup( wversionrequested, &WSAData );
if(nReturn==0 )
{
struct hostent *pHostEnt;
char szHostName[260];
::gethostname(szHostName,260);
m_sHostName=szHostName;
if((pHostEnt = gethostbyname(szHostName)) != NULL)
m_sHostIP= inet_ntoa (*(struct in_addr *)*pHostEnt->h_addr_list);
::WSACleanup();
}
}
////////////////////////////////////////////////
//为应用程序添加关闭系统特权 for WinNT
////////////////////////////////////////////////
BOOL CGtMpegApp::SetPrivilegeInAccessToken()
{
HANDLE hProcess;
HANDLE hAccessToken;
LUID luidPrivilegeLUID;
TOKEN_PRIVILEGES tpTokenPrivilege;
hProcess = GetCurrentProcess();
if (!hProcess)
{ perror("GetCurrentProcess");
return(FALSE);
}
if (!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hAccessToken))
{
perror("OpenProcessToken");
return(FALSE);
}
if (!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luidPrivilegeLUID))
{
perror("LookupPrivilegeValue");
printf("\nThe above error means you need to log on as an Administrator");
return(FALSE);
}
tpTokenPrivilege.PrivilegeCount = 1;
tpTokenPrivilege.Privileges[0].Luid = luidPrivilegeLUID;
tpTokenPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges (hAccessToken,FALSE,&tpTokenPrivilege,sizeof(TOKEN_PRIVILEGES),NULL,NULL);
if ( GetLastError() != NO_ERROR )
{
perror("AdjustTokenPrivileges");
return(FALSE);
}
return(TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -