Bitcoin Forum
May 13, 2024, 05:10:50 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [C++ Win32 Help Needed] Open Source Last BTC Price showing in system tray  (Read 2645 times)
bit.dev (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
April 19, 2014, 07:06:22 PM
 #1

Hello Bitcoiners,

I started a little win32 program to show the latest BTC price from bitstamp.net/api/ticker/ each 10 seconds showing a MessageBox with price.
The think is that the MessageBox is annoying and is just for testing.

I want to make it display the price in the system tray (notification bar) like the hour and date, but i don't know how to continue, please help, I'm a noob.


Here is the main code:
Code:
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <string>
#include <sstream>


#pragma comment(lib, "Ws2_32.lib")

void show_price()
{

WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.bitstamp.net");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*) host->h_addr);

connect(Socket, (SOCKADDR*) (&SockAddr), sizeof (SockAddr));
int bytesSent = 0;
std::string szGetDataHTTP = "GET /api/ticker/ HTTP/1.1\r\nHost: www.bitstamp.net\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36\r\n\r\n";
send(Socket, szGetDataHTTP.c_str() + bytesSent, szGetDataHTTP.size() - bytesSent, 0);
std::stringstream szEndBuf;
char buffer[1024];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, sizeof (buffer) - 1, 0)) > 0) {
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
szEndBuf << buffer[i];
i += 1;
}
std::string szData = szEndBuf.str();
//MessageBox(NULL,buffer,"recv buffer",MB_OK);

int pos,pos1;
pos = szData.find("\"last\": \"");
pos1 = szData.find("\",");
if (pos <= 0 ) {
MessageBox(NULL,"not found","ERROR",MB_OK);
closesocket(Socket);
break;
}
else
{
std::string enddata = szData.substr(pos, pos1);
std::string price = enddata.substr(enddata.find("\"last\": \"") + 9, enddata.find("\",") - enddata.find("\"last\": \"") - 9);
std::string usd = "1BTC = $" + price + " USD";
MessageBoxA(NULL,usd.c_str(),"bitstamp Last Price",MB_OK);
closesocket(Socket);
break;
}
}
}

int main(int argc, char** argv) {

while(true)
{
show_price();
Sleep(10000); // 10 seconds
}
WSACleanup();

return 0;
}

Visual Studio 2010 project download with compiled executable:
http://www.sendspace.com/file/p2kn6f

Hope it is useful and people contribute to it.
If you know how to do this please contribute and make it working in the system tray area and post here the updated version.

I also thought to add max, min price alert and settings to change currency and BTC price source from other market, but i want to make it work in the task bar first.

Thank you all
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!