Stock ticker widget for windows 7 (Free)
DOWNLOAD EXE -17kb
DOWNLOAD SAMPLE PROJECT CODE -4MB
This application displays the stock indices of the markets and share prices of the companies.Here the user can add the market or the company name in the stockwidget.
I have made the widget to refresh the data every 5 minutes.Hard refresh can be done pressing enter in the editbox.
If you want to add new market or company name in the stock widget enter the symbol code of that company or market inside the edit box and press enter.
You can get the symbol codes by searching the name here
For example entering “WIPRO.NS” in the edit box adds the wipro(NSE) stocks in the ticker.
Entering “^NSEI” will add the S&P CNX NIFTY Index.
If user wants to clear or remove all the previosly stored company names or markets
Enter “clear” inside the edit box and press enter.
In the code for this application includes one important function which gets the stocks information from the internet using windows sockets as shown below.
int CStockWidget::StockSocket(void)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
int count,cbXfer;
UINT i,j;
ZeroMemory( &recvbuf, sizeof(recvbuf));
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
RETAILMSG(1,(TEXT("WSAStartup failed: %d\n"), iResult));
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("download.finance.yahoo.com",
DEFAULT_PORT,
&hints,
&result);
if ( iResult != 0 ) {
RETAILMSG(1,(L"getaddrinfo failed: %d\n", iResult));
DestroyWindow(iStockWnd);
MessageBox(iParentWnd,L"Error Connecting to Internet",L"ERROR",MB_OK);//if internet is down
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
RETAILMSG(1,(L"Error at socket(): %ld\n", WSAGetLastError()));
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
int err = GetLastError();
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
RETAILMSG(1,(L"Unable to connect to server!\n"));
WSACleanup();
return 1;
}
char sendBuffer[200],temp1[20];
//static char temp[200]=("GET /d/quotes.xml?s=BHEL.NS+BEL.NS+RCOM.NS+GAIL.NS+TCS.NS+RANBAXY.NS+");
TCHAR rcvEdit[20];
if(!Clear)
{
GetWindowText(iEditWnd,rcvEdit,sizeof(rcvEdit));
if(rcvEdit[0]!='\0')
{
// Convert String to ASCII
wcstombs(temp1,rcvEdit,sizeof(rcvEdit));
strcat(temp,"+");
strcat(temp,temp1);
Clear=false;
//strcat(temp,".NS");
}
}
SetWindowText(iEditWnd,NULL);
sprintf(sendBuffer, "%s&f=nl1p2 HTTP/1.1\r\nHost: download.finance.yahoo.com\r\n\r\n",temp);
iResult = send( ConnectSocket, sendBuffer, (int)strlen(sendBuffer), 0 );
if (iResult == SOCKET_ERROR) {
RETAILMSG(1,(L"send failed: %d\n", WSAGetLastError()));
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
} while( iResult == recvbuflen );
// cleanup
closesocket(ConnectSocket);
WSACleanup();
updateflag=true;
// return 1;
}