Greetings,
I am trying to figure out how to get use to programming in C++ and I am struggling to find a solid example to go by when creating my own trade desk. I recently came across this code example from the forum and I am having compile time problems. I am sure they are minor but I would like as much help as possible while I am still getting used to programming in C++. The Compiler I am using is Microsoft Visual C++ Express edition. What I am trying to do is program from windows based C++ and then extend to G++ on Linux. As much support as possible is greatly appreciated.
This is the code snippet:
// LoginPossibility.cpp : main project file.
#include "stdafx.-"
#include <winsock.->
#include <ws2tcpip.->
#include <IdWinSock2>
#include <atlbase.->
extern CComModule _Module;
#include <atlcom.->
#include <stdio.->
#include <conio.-> //for getche()
#include <cassert>
#include <windows.-> // for sleep function
#include <climits>
#include <queue>
#include <time.->
#include <stdlib.->
#include <signal.->
#include <sys/types.->
#include <math.->
#include <strstream>
#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
#include "Currency_Type.hpp"
#import "C:\\Program Files\\CandleWorks\\FXOrder2Go\fxcore.dll"
using namespace System;
using namespace FXCore; // CLoginAndTradeDlg message handlers
using namespace std;
using std::ifstream;
// global variable declarations/initializations
FXCore::ICoreAutPtr m_pCore; // Trading engine
FXCore::ITradeDeskAutPtr m_pTradeDesk; // creates Trading desktop
bool m_bLoggedIn; // true if server connection is established
BSTR sAccountID;
bool LoggedIn; // true if server connection is established. This is used instead of m_bLoggedIn.
const char* Username = "..."; // ....... New account: FX.........
const char* Password = "...."; // .... New account: ....
const char* URL = "http://www.fxcorporate.com"; //
http://www.fxcorporate.com
const char* Connection = "Demo"; // Demo
const char* sAccountNumber = "........"; // ......... New account: ......
// function declaration
double _getOffer(LPCSTR szOffer, _bstr_t &sQuoteID, bool bAsk);
void OnLogin();
bool verifyAccount(LPCSTR szAcct);
_bstr_t getLastErrorCom();
void showComError(_com_error &e);
int _tmain()
{
// Login procedures
CoInitialize(NULL);
CLSID clsid;
HRESULT hRes = CLSIDFromProgID(L"FXCM.CORE", &clsid);
if (FAILED(hRes))
{
cerr << "Order2Go not installed";
return false;
}
m_pCore=0;
try
{
HRESULT hr = m_pCore.CreateInstance(clsid);
if (FAILED(hr))
cerr << "Cannot initialize Trading Engine" << endl;
}
catch(_com_error e)
{
cout << "Error initilaizing Trading Engine (function 'Main()')" << endl;
showComError(e);
}
if(m_pCore)
cerr << "Core initiated" << endl;
else
cerr << "Core not initiated" << endl;
// call to login function
m_pTradeDesk = m_pCore->CreateTradeDesk("customer");
OnLogin();
// check to see if account is valid.
bool bAcctValid = verifyAccount(sAccountNumber);
if (!bAcctValid)
{
cout << "Requested account number not found under this login." << endl;
}
else
{
cout << "Account Verified." << endl << endl;
}
return 0;
}
//Login Function
void OnLogin()
{
try
{
cout << "Establishing trade desk..." << endl;
// create TradingDesktop
//m_pTradeDesk = m_pCore->CreateTradeDesk("customer");
cout << "Logging in..." << endl;
// perform login
m_pTradeDesk->Login(Username, Password, URL, Connection); if (!m_pTradeDesk->IsLoggedIn())
{
cout << "An error occurred upon login attempt." << endl;
}
else
{
cout << "Login Succeeded." << endl;
LoggedIn = true;
}
}
catch(_com_error e)
{
cout << "Error in Login Function..." << endl;
showComError(e);
cout << endl;
}
}
// Verify account
bool verifyAccount(LPCSTR szAcct)
{
ITablesManagerAutPtr pTblMgr = m_pTradeDesk->GetTablesManager();
ITableAutPtr pAcctTbl = pTblMgr->FindTable("accounts", "");
IRowsEnumAutPtr pAccounts = pAcctTbl->GetRows();
_variant_t vAcct;
_variant_t vBalance;
for (int i = 0; i < pAccounts->Count; i++)
{
CComDispatchDriver pAcct = pAccounts->Item(i+ 1).pdispVal;
pAcct.Invoke0(L"AccountName", &vAcct);
_bstr_t wcsAcct(vAcct);
_variant_t Account_ID;
ITablesManagerAutPtr pTblMgr = m_pTradeDesk->GetTablesManager();
ITableAutPtr pAcctTbl = pTblMgr->FindTable("accounts", "");
IRowsEnumAutPtr pAccounts = pAcctTbl->GetRows();
pAcct.Invoke0(L"AccountID", &Account_ID);
_bstr_t wcsAccount_ID(Account_ID); // globally identified
sAccountID = wcsAccount_ID.copy();
cout << "Account Number: " << wcsAcct << " Account ID: " << wcsAccount_ID << endl;
if (wcsAcct == _bstr_t(szAcct))
return true;
}
return false;
}
// show additional info if com_error occured
_bstr_t getLastErrorCom()
{
IErrorInfo *pErr = 0;
_bstr_t err = "";
if (SUCCEEDED(GetErrorInfo(0, &pErr)))
{
BSTR v = 0;
if (pErr && SUCCEEDED(pErr->GetDescription(&v)))
err = v;
}
return err;
}
void showComError(_com_error &e)
{
_bstr_t err = getLastErrorCom();
if (err.length() == 0)
err = e.ErrorMessage();
cout << err;
}