Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Bit of C++ help, please?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Maes
Advanced Cheater
Reputation: 10

Joined: 09 Apr 2009
Posts: 50

PostPosted: Wed Mar 09, 2011 12:30 pm    Post subject: Bit of C++ help, please? Reply with quote

How might I use a button to display the current websites favicon, in a C++ browser.
_________________
Account reclaimed by former owner?
Back to top
View user's profile Send private message
AhMunRa
Grandmaster Cheater Supreme
Reputation: 27

Joined: 06 Aug 2010
Posts: 1117

PostPosted: Wed Mar 09, 2011 1:53 pm    Post subject: Reply with quote

In C#

Code:

public static Image favicon(String u, string file)
{
    Uri url = new Uri(u);
    String iconurl = "http://" + url.Host + "/favicon.ico";

    WebRequest request = WebRequest.Create(iconurl);
    try
    {
        WebResponse response = request.GetResponse();

        Stream s = response.GetResponseStream();
        return Image.FromStream(s);
    }
    catch (Exception ex)
    {
        //return a default icon in case
        //the web site doesn`t have a favicon
        return Image.FromFile(file);
    }
}


From http://www.codeproject.com/KB/cs/WBrowser.aspx

May give you an idea or two.

_________________
<Wiccaan> Bah that was supposed to say 'not saying its dead' lol. Fixing >.>
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Wed Mar 09, 2011 3:46 pm    Post subject: Reply with quote

This is an example of making an HTTP request to download the icon:

Code:

#pragma comment( lib, "WS2_32.lib" )
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>

// Site to [attempt to] download icon from.
std::string g_vSite = "www.google.com";
std::string g_vIcon = "favicon.ico";

int main( int argc, TCHAR* argv[] )
{
   WSADATA wsaData = { 0 };
   if( WSAStartup( MAKEWORD( 2, 0 ), & wsaData ) != 0 )
   {
      std::cout << "WSAStartup failed with error: " << WSAGetLastError() << std::endl;
      return 0;
   }

   SOCKET sock = { 0 };
   if( ( sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 )
   {
      std::cout << "socket failed with error: " << WSAGetLastError() << std::endl;
      WSACleanup();
      return 0;
   }

   struct hostent* rhostent = { 0 };
   rhostent = gethostbyname( g_vSite.c_str() );

   struct sockaddr_in saddr = { 0 };
   saddr.sin_family      = AF_INET;
   saddr.sin_addr.s_addr   = *(u_long*)rhostent->h_addr_list[0];
   saddr.sin_port         = htons( 80 );

   if( connect( sock, (struct sockaddr *)&saddr, sizeof( sockaddr_in ) ) < 0 )
   {
      std::cout << "connect failed with error: " << WSAGetLastError() << std::endl;
      closesocket( sock );
      WSACleanup();
      return 0;
   }

   // Build HTTP GET request to obtain icon file.
   std::string strHttpRequest = "GET /favicon.ico HTTP/1.0\r\n";
   strHttpRequest += "Host: " + g_vSite + "\r\n";
   strHttpRequest += "\r\n";

   if( send( sock, strHttpRequest.c_str(), strHttpRequest.length(), 0 ) != strHttpRequest.length() )
   {
      std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
      closesocket( sock );
      WSACleanup();
      return 0;
   }

   // Retrieve buffer from request.
   std::string strHttpResponse;
   char szBuffer[ 512 ] = { 0 };
   int nReadSize = 512;

   while( nReadSize > 0 )
   {
      nReadSize = recv( sock, (char*)&szBuffer, 512, NULL );
      if( nReadSize == 0 ) break;
      strHttpResponse += std::string( szBuffer ).substr( 0, nReadSize );
   }

   // Cleanup and return.
   closesocket( sock );
   WSACleanup();

   return 0;
}


I wrote this in a few minutes so its prone to have issues (and no error checking). Used the following for references:

http://www.zedwood.com/article/113/cpp-winsock-basic-http-connection
http://msdn.microsoft.com/en-us/library/ms738524%28v=vs.85%29.aspx

You'll have to strip the response header from the response yourself, but that's one way to download the icon via Http. There are other download API that you can use to directly download the icon as well.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites