Tuesday, May 3, 2011

Callbacks using C# & DLL's


When callback is required:


1) Sample Application ---------interacts with-------> Library(dll).

2) sample Application calls certain method say STOP() of library.

3) When the task inside method STOP() is over, then if we want to make a reference to a method CALLBACK() of SampleApplication we use the concept of callbacks


To achieve this feature in visual studio using c#

we need to work with Delegates & Events.



Inside Library(.dll)


1) In class where you have created STOP method write these 2 lines:


2) Create STOP method:



Inside Sample Application


1) When user clicks any button call the STOP() method of the library


2) Recieving callback





Wednesday, April 27, 2011

Send Web Request from WinForms


When i had to do this long time back i searched a lot and now i am sharing this with all.
Hope this helps somebody
Enjoy Coding :)

StringBuilder sb  = new StringBuilder();


// used on each read operation
byte[]        buf = new byte[8192];


// prepare the web page we will be asking for
HttpWebRequest  request \;
request = (HttpWebRequest)WebRequest.Create("http://www.feefifofum.com/login.aspx?userid=XXX&pass=YYYY");


// execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();


// we will read data via the response stream
Stream resStream = response.GetResponseStream();


string tempString = null;
int    count      = 0;
do
{
    // fill the buffer with data
    count = resStream.Read(buf, 0, buf.Length);


    // make sure we read some data
    if (count != 0)
    {
        // translate from bytes to ASCII text
        tempString = Encoding.ASCII.GetString(buf, 0, count);


        // continue building the string
        sb.Append(tempString);
    }
}
while (count > 0); // any more data to read?


// print out page source
Console.WriteLine(sb.ToString());


Some Others Links:
http://stackoverflow.com/questions/2054712/send-information-through-a-url-from-desktop-based-application/2054819#2054819

http://msdn.microsoft.com/en-us/library/fhd1f0sw(VS.80).aspx

http://msdn.microsoft.com/en-us/library/0645045y(VS.80).aspx