Files
HLStock/HookMsg/HookMsg.cpp
2013-07-20 23:23:05 +00:00

404 lines
11 KiB
C++

// HookMsg.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "HookMsg.h"
#include "HookMsgDlg.h"
#include "LogonDlg.h"
#include <initguid.h>
#include "HookMsg_i.c"
#include "./UtilClass/LimitSingleInstance.h"
CLimitSingleInstance g_SingleInstanceObj(TEXT("{1F80F079-B2CD-49ac-AD84-52160FBDB753}"));
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHookMsgApp
BEGIN_MESSAGE_MAP(CHookMsgApp, CWinApp)
//{{AFX_MSG_MAP(CHookMsgApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
//}}AFX_MSG_MAP
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHookMsgApp construction
CHookMsgApp::CHookMsgApp()
{
m_bSuccess = FALSE;
m_strLoginServer1 = "";
m_strLoginServer2 = "";
for(int i=0;i<MAX_FIND;i++){m_hFindWindow[i]=NULL;} // 실제 찾은 핸들을 저장함.
m_bInfoTickerConnect=FALSE; // 거래소 InfoTicker 연결여부.
m_bTopmostWnd=FALSE; // 최상위 윈도우 상태.
m_bShowLockContorlDlg=FALSE; // 상태제어창 보이기.
m_nLogonSvr=0; // 로그온 인증서버 선택.
m_bLogonPasswdSave=FALSE; // 로그온 패스워드 기억.
m_strLogonPasswd=_T(""); // 로그온 패스워드.
m_strBankAccount=_T(""); // 계좌번호.
m_strBankAccountPermitted=_T(""); // 인증허가된 계좌번호 리스트.
m_bHotkeyCodeInputRule1 = TRUE; // 잠김때 중복/수동종목코드 입력않함.
m_bHogaLevelCalc = TRUE; // 현재가미적용시 매매단계 호가자동계산 적용.
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CHookMsgApp object
CHookMsgApp g_theApp;
void CHookMsgApp::LoadAppEnv()
{
/* memset( &g_AppEnv, NULL, sizeof(g_AppEnv));
*/
m_bHogaLevelCalc = GetProfileInt("Settings", "bHogaLevelCalc", (int)TRUE );
m_bHotkeyCodeInputRule1 = GetProfileInt("Settings", "bHotkeyCodeInputRule1", (int)TRUE );
m_bTopmostWnd = GetProfileInt("Settings", "bTopmostWnd", (int)TRUE );
m_bShowLockContorlDlg = GetProfileInt("Settings", "bShowLockContorlDlg", (int)FALSE );
char szKeyName[100];
UINT nl;
for( int i=0; i<MAX_FIND; i++ )
{
sprintf(szKeyName,"hFindWindow[%d]", i);
HWND * hWnd;
if( GetProfileBinary("Settings", szKeyName, (LPBYTE*)&hWnd, &nl))
{
memcpy( &m_hFindWindow[i], hWnd, sizeof(HWND) );
delete [] hWnd;
}
}
m_nLogonSvr = GetProfileInt("Settings", "nLogonSvr", (int)0 );// 로그온 인증서버.
m_bLogonPasswdSave = GetProfileInt("Settings", "bLogonPasswdSave", (int)TRUE );// 로그온 패스워드 기억.
m_strLogonPasswd = GetProfileString("Settings", "strLogonPasswd", "5525" );// 로그온 패스워드.
m_strLoginServer1 = GetProfileString("Settings", "strLoginServer1", "http://dnasoft.com.ne.kr/HookMsg/User.html" );// 로그온 서버1주소.
m_strLoginServer2 = GetProfileString("Settings", "strLoginServer2", "http://myhome.hanafos.com/~innovision/myhome_upload/user.txt" );// 로그온 서버2주소.
m_bInfoTickerConnect = GetProfileInt("Settings", "bInfoTickerConnect", (int)FALSE );// 거래소 InfoTicker 연결여부.
}
void CHookMsgApp::SaveAppEnv()
{
WriteProfileInt("Settings", "bHogaLevelCalc", (int)(m_bHogaLevelCalc) );
WriteProfileInt("Settings", "bHotkeyCodeInputRule1", (int)(m_bHotkeyCodeInputRule1) );
WriteProfileInt("Settings", "bTopmostWnd", (int)(m_bTopmostWnd) );
WriteProfileInt("Settings", "bShowLockContorlDlg", (int)(m_bShowLockContorlDlg) );
char szKeyName[100];
for( int i=0; i<MAX_FIND; i++ )
{
sprintf(szKeyName,"hFindWindow[%d]", i);
WriteProfileBinary("Settings", szKeyName, (LPBYTE)&(m_hFindWindow[i]), sizeof(HWND));
}
WriteProfileInt("Settings", "nLogonSvr", (int)m_nLogonSvr );// 로그온 인증서버.
WriteProfileInt("Settings", "bLogonPasswdSave", (int)m_bLogonPasswdSave );// 로그온 패스워드 기억.
WriteProfileString("Settings", "strLogonPasswd", m_strLogonPasswd );// 로그온 패스워드.
WriteProfileString("Settings", "strLoginServer1", m_strLoginServer1 );// 로그온 서버1주소.
WriteProfileString("Settings", "strLoginServer2", m_strLoginServer2 );// 로그온 서버2주소.
WriteProfileInt("Settings", "bInfoTickerConnect", (int)(m_bInfoTickerConnect) );// 거래소 InfoTicker 연결여부.
}
/////////////////////////////////////////////////////////////////////////////
// CHookMsgApp initialization
BOOL CHookMsgApp::InitInstance()
{
if (!InitATL())
return FALSE;
if (g_SingleInstanceObj.IsAnotherInstanceRunning())
{
MessageBox ( NULL, "프로그램이 이미 실행중입니다.", "HookMsg 실행", MB_APPLMODAL | MB_OK );
return FALSE;
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
WNDCLASS wc;
// Get the info for this class.
// #32770 is the default class name for dialogs boxes.
::GetClassInfo(AfxGetInstanceHandle(), "#32770", &wc);
// Change the name of the class.
wc.lpszClassName = "HookMsg_MainWnd";
// Register this class so that MFC can use it.
AfxRegisterClass(&wc);
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// AfxOleInit();
AfxSocketInit();
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
//#ifdef _AFXDLL
// Enable3dControls(); // Call this when using MFC in a shared DLL
//#else
// Enable3dControlsStatic(); // Call this when linking to MFC statically
//#endif
SetRegistryKey(_T("YouriSoft"));
///////////////////////////////////////////////////////////////////
TCHAR szFilePath[_MAX_PATH]={0,};
::GetModuleFileName(NULL, szFilePath, _MAX_PATH);
*(strrchr( szFilePath, '\\' ) + 1) = 0;
strcat( szFilePath, "\\config.ini" );
g_ConfigINI.SetINIFile( szFilePath );
///////////////////////////////////////////////////////////////////
g_ConfigINI.ReadConfigEnvFromINI();
g_ConfigINI.ReadConfigEnvFromINI_DEBUG_SECTION(); // 디버깅을 위한 설정로드.
LoadAppEnv();
///////////////////////////////////////////////////////////////////
CLogonDlg logon_dlg;
int nLogon = logon_dlg.DoModal();
if( nLogon != IDOK )
return FALSE;
///////////////////////////////////////////////////////////////////
CString _strFileName;
CString _strDate;
char szTmp[100];
_strdate( szTmp );
_strDate = szTmp;
_strDate.Replace( '/', '-' );
::GetModuleFileName(NULL, szFilePath, _MAX_PATH);
*(strrchr( szFilePath, '\\' ) + 1) = 0;
_strFileName.Format( "%s로그_%s.log", szFilePath, _strDate );
HANDLE hFile = CreateFile(_strFileName,
GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("로그파일을 만들 수 없습니다."));
else
{
g_pLogFile = new CFile(hFile);
g_pLogFile->SeekToEnd();
}
CHookMsgDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
m_bSuccess = TRUE;
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
// [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
CString CHookMsgApp::RegReadString(HKEY hKeyName, CString strPath, CString strSubKey)
{
CString RegKey = "";
HKEY hKey;
::RegOpenKeyEx(hKeyName, strPath, 0, KEY_ALL_ACCESS, &hKey);
DWORD dwType;
DWORD dwBytes = 10240;
char szBuf[10240] = {0,};
LONG lRet = ::RegQueryValueEx(hKey, strSubKey, 0, &dwType, (LPBYTE)szBuf, &dwBytes);
if( lRet == ERROR_SUCCESS )
{
::RegCloseKey(hKey);
RegKey = szBuf;
return RegKey;
}
else
{
::RegCloseKey(hKey);
return "";
}
}
BOOL CHookMsgApp::RegWriteString(HKEY hKeyName, CString strPath, CString strSubKey, CString strValue)
{
HKEY hKey;
::RegCreateKeyEx(hKeyName, strPath, 0, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
LONG lRet = ::RegSetValueEx( hKey, strSubKey, 0, REG_SZ,
(const unsigned char*)(LPCTSTR)strValue,
strValue.GetLength() );
if( lRet == ERROR_SUCCESS )
{
::RegCloseKey(hKey);
return TRUE;
}
else
{
Message_Box( "레지스트리 쓰기실패", ::GetLastError() );
::RegCloseKey(hKey);
return FALSE;
}
}
int CHookMsgApp::ExitInstance()
{
if( m_bSuccess )
{
///////////////////////////////////////////////////////////////////
g_ConfigINI.WriteConfigEnvToINI();
SaveAppEnv();
///////////////////////////////////////////////////////////////////
if (m_bATLInited)
{
_Module.RevokeClassObjects();
_Module.Term();
CoUninitialize();
}
if( g_pLogFile )
{
g_pLogFile->Flush();
g_pLogFile->Close();
delete g_pLogFile;
}
}
return CWinApp::ExitInstance();
}
///////////////////////////////////////////////////////////////////////
CHookMsgModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
LONG CHookMsgModule::Unlock()
{
AfxOleUnlockApp();
return 0;
}
LONG CHookMsgModule::Lock()
{
AfxOleLockApp();
return 1;
}
LPCTSTR CHookMsgModule::FindOneOf(LPCTSTR p1, LPCTSTR p2)
{
while (*p1 != NULL)
{
LPCTSTR p = p2;
while (*p != NULL)
{
if (*p1 == *p)
return CharNext(p1);
p = CharNext(p);
}
p1++;
}
return NULL;
}
BOOL CHookMsgApp::InitATL()
{
m_bATLInited = TRUE;
#if _WIN32_WINNT >= 0x0400
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
#else
HRESULT hRes = CoInitialize(NULL);
#endif
if (FAILED(hRes))
{
m_bATLInited = FALSE;
return FALSE;
}
_Module.Init(ObjectMap, AfxGetInstanceHandle());
_Module.dwThreadID = GetCurrentThreadId();
LPTSTR lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
TCHAR szTokens[] = _T("-/");
BOOL bRun = TRUE;
LPCTSTR lpszToken = _Module.FindOneOf(lpCmdLine, szTokens);
while (lpszToken != NULL)
{
if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
{
_Module.UpdateRegistryFromResource(IDR_HOOKMSG, FALSE);
_Module.UnregisterServer(TRUE); //TRUE means typelib is unreg'd
bRun = FALSE;
break;
}
if (lstrcmpi(lpszToken, _T("RegServer"))==0)
{
_Module.UpdateRegistryFromResource(IDR_HOOKMSG, TRUE);
_Module.RegisterServer(TRUE);
bRun = FALSE;
break;
}
lpszToken = _Module.FindOneOf(lpszToken, szTokens);
}
if (!bRun)
{
m_bATLInited = FALSE;
_Module.Term();
CoUninitialize();
return FALSE;
}
hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE);
if (FAILED(hRes))
{
m_bATLInited = FALSE;
CoUninitialize();
return FALSE;
}
return TRUE;
}