Go Back   DailyFX Forex Forum | FX Forum > FXCM Products and Services > FXProgrammers > Discussion / Support Forum > Order2Go COM Trading API Support

Reply
 
Thread Tools Rate Thread
  #1 (permalink)  
Old 08-24-2009, 03:28 PM
Member
 
Join Date: Dec 2008
Posts: 5
Stash7 is on a distinguished road
Thumbs up C++ Login Help

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;
}
Reply With Quote
  #2 (permalink)  
Old 08-25-2009, 09:29 AM
Member
 
Join Date: Oct 2008
Posts: 609
Nikolay.Gekht is on a distinguished road
Hm... To tell the truth, I recommend you to use simpler language like C#.

But, regarding your code:
1) Dashes in the include names. Here was a forum problem. It printed - instead of h.

So, you have to write #include <windows.h> instead of #include <windows.-> and so on.

2) I'm not sure why you use "stdafx.-". This file is usually used to collect all header which must be precompiled, but all such headers are also already included into the application body. Remove it.

3) winsock and ws2tcpip must be included after windows

4) There is no IdWinSock2 among the standard Microsoft headers.

5) I don't know what it Currency_Type.hpp file. But I commended out this header and it does not affect the compilation. :-)

6) The is not namespace System. Looks like you're confused with .NET.

Looks like that's all. After these changes, the code was successfully compiled.

I attached the modified source to this post. Just rename it to q.cpp.
Attached Files
File Type: txt q.txt (4.3 KB, 38 views)

Last edited by Nikolay.Gekht; 08-25-2009 at 03:21 PM.. Reason: problem with "h" is fixed :-)
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Disclaimer: Trading foreign exchange on margin carries a high level of risk, and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to trade foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with foreign exchange trading, and seek advice from an independent financial advisor if you have any doubts. Any opinions, news, research, analyses, prices, or other information contained on this website is provided as general market commentary and does not constitute investment advice. Forex Capital Markets LLC. will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.

All times are GMT -5. The time now is 07:56 AM.
Copyright ©2009 Daily FX. All Rights Reserved.