h1

How to Shutdown & Restart Windows XP & Windows 2000 using command line.

November 28, 2009

Windows 2000 (with the Resource Kit installed) and Windows XP (natively) have an actual shutdown command that can be launched from a command prompt — and which, therefore, also can be launched from a shortcut. To see all available options for this command, click Start, click Run, type CMD, and click OK. Then, in the box that appears, type:

SHUTDOWN /?

You can also study the available options in the Microsoft KB 317371, “How to Use the Remote Shutdown Tool to Shut Down and Restart a Computer in Windows 2000.” (The article is basically suitable for Win XP also.)

This command starts a 30-second countdown for a shutdown or restart, which permits you to abort it (with a shutdown -a command). It you want the command to execute, use the -t flag, which lets you set the time lapse in seconds. The examples below use a 1-second delay.

For a shortcut to RESTART Windows XP:
SHUTDOWN -r -t 01

For a shortcut to SHUT DOWN Windows XP:
SHUTDOWN -s -t 01

Unfortunately — especially on Windows XP — this option only shuts down Windows. It does not shut down your computer, at least on most hardware. For that, on Win XP (and for most Win 2000 users also), I recommend the freeware utility Shutdown.exe (not to be confused with the Windows utility by the same name) by MS-MVP Andrej Budja. I’ve seen several shutdown utilities recommended, but this is the only one that I’ve seen actually shut down Windows XP and then powerdown the computer behind it. For more information on the tool, see here. After you place this utility in the root folder of C:, the commands for a shutdown, restart, or hibernate (respectively), each without a time delay, would be shutdown -u -t 0 for shutdown, shutdown -r -t 0 for restart, and shutdown -h -t 0 for hibernate.

h1

Dynamic Memory concept in C++

August 1, 2009

Until now, in all our programs, we have only had as much memory available as we declared for our variables, having the size of all of them to be determined in the source code, before the execution of the program. But, what if we need a variable amount of memory that can only be determined during runtime? For example, in the case that we need some user input to determine the necessary amount of memory space.

The answer is dynamic memory, for which C++ integrates the operators new and delete.

Operators new and new[]
In order to request dynamic memory we use the operator new. new is followed by a data type specifier and -if a sequence of more than one element is required- the number of these within brackets []. It returns a pointer to the beginning of the new block of memory allocated. Its form is:

pointer = new type
pointer = new type [number_of_elements]

The first expression is used to allocate memory to contain one single element of type type. The second one is used to assign a block (an array) of elements of type type, where number_of_elements is an integer value representing the amount of these. For example:

int * bobby;
bobby = new int [5];

In this case, the system dynamically assigns space for five elements of type int and returns a pointer to the first element of the sequence, which is assigned to bobby. Therefore, now, bobby points to a valid block of memory with space for five elements of type int.

The first element pointed by bobby can be accessed either with the expression bobby[0] or the expression *bobby. Both are equivalent as has been explained in the section about pointers. The second element can be accessed either with bobby[1] or *(bobby+1) and so on…

You could be wondering the difference between declaring a normal array and assigning dynamic memory to a pointer, as we have just done. The most important difference is that the size of an array has to be a constant value, which limits its size to what we decide at the moment of designing the program, before its execution, whereas the dynamic memory allocation allows us to assign memory during the execution of the program (runtime) using any variable or constant value as its size.

The dynamic memory requested by our program is allocated by the system from the memory heap. However, computer memory is a limited resource, and it can be exhausted. Therefore, it is important to have some mechanism to check if our request to allocate memory was successful or not.

C++ provides two standard methods to check if the allocation was successful:

One is by handling exceptions. Using this method an exception of type bad_alloc is thrown when the allocation fails. Exceptions are a powerful C++ feature explained later in these tutorials. But for now you should know that if this exception is thrown and it is not handled by a specific handler, the program execution is terminated.

This exception method is the default method used by new, and is the one used in a declaration like:

bobby = new int [5]; // if it fails an exception is thrown

The other method is known as nothrow, and what happens when it is used is that when a memory allocation fails, instead of throwing a bad_alloc exception or terminating the program, the pointer returned by new is a null pointer, and the program continues its execution.

This method can be specified by using a special object called nothrow, declared in header , as argument for new:

bobby = new (nothrow) int [5];

In this case, if the allocation of this block of memory failed, the failure could be detected by checking if bobby took a null pointer value:

int * bobby;
bobby = new (nothrow) int [5];
if (bobby == 0) {
// error assigning memory. Take measures.
};

This nothrow method requires more work than the exception method, since the value returned has to be checked after each and every memory allocation, but I will use it in our examples due to its simplicity. Anyway this method can become tedious for larger projects, where the exception method is generally preferred. The exception method will be explained in detail later in this tutorial.

Operators delete and delete[]
Since the necessity of dynamic memory is usually limited to specific moments within a program, once it is no longer needed it should be freed so that the memory becomes available again for other requests of dynamic memory. This is the purpose of the operator delete, whose format is:

delete pointer;
delete [] pointer;

The first expression should be used to delete memory allocated for a single element, and the second one for memory allocated for arrays of elements.

The value passed as argument to delete must be either a pointer to a memory block previously allocated with new, or a null pointer (in the case of a null pointer, delete produces no effect).

// rememb-o-matic
#include
#include
using namespace std;

int main ()
{
int i,n;
int * p;
cout <> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout <> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}

How many numbers would you like to type? 5
Enter number : 75
Enter number : 436
Enter number : 1067
Enter number : 8
Enter number : 32
You have entered: 75, 436, 1067, 8, 32,

Notice how the value within brackets in the new statement is a variable value entered by the user (i), not a constant value:

p= new (nothrow) int[i];

But the user could have entered a value for i so big that our system could not handle it. For example, when I tried to give a value of 1 billion to the "How many numbers" question, my system could not allocate that much memory for the program and I got the text message we prepared for this case (Error: memory could not be allocated). Remember that in the case that we tried to allocate the memory without specifying the nothrow parameter in the new expression, an exception would be thrown, which if it's not handled terminates the program.

It is a good practice to always check if a dynamic memory block was successfully allocated. Therefore, if you use the nothrow method, you should always check the value of the pointer returned. Otherwise, use the exception method, even if you do not handle the exception. This way, the program will terminate at that point without causing the unexpected results of continuing executing a code that assumes a block of memory to have been allocated when in fact it has not.

Dynamic memory in ANSI-C

Operators new and delete are exclusive of C++. They are not available in the C language. But using pure C language and its library, dynamic memory can also be used through the functions malloc, calloc, realloc and free, which are also available in C++ including the header file (see cstdlib for more info).

The memory blocks allocated by these functions are not necessarily compatible with those returned by new, so each one should be manipulated with its own set of functions or operators.

h1

Multithread Programming Concept in C++

August 1, 2009
h1

Designing a Thread Class in C++

August 1, 2009

This section is written by Bimlesh and the document is located here .
Introduction

Multi threaded programming is becoming ever more popular. This section presents a design for a C++ class that will encapsulate the threading mechanism. Certain aspects of thread programming, like mutexes and semaphores are not discussed here. Also, operating system calls to manipulate threads are shown in a generic form.
Brief Introduction To Threads

To understand threads one must think of several programs running at once. Imagine further that all these programs have access to the same set of global variables and function calls. Each of these programs would represent a thread of execution and is thus called a thread. The important differentiation is that each thread does not have to wait for any other thread to proceed. All the threads proceed simultaneously. To use a metaphor, they are like runners in a race, no runner waits for another runner. They all proceed at their own rate.

Why use threads you might ask. Well threads can often improve the performance of an application and they do not incur significant overhead to implement. They effectively give good bang for a buck. Imagine an image server program that must service requests for images. The program gets a request for an image from another program. It must then retrieve the image from a database and send it to the program that requested it. If the server were implemented in a single threaded approach, only one program could request at a time. When it was busy retrieving an image and sending it to a requestor, it could not service other requests. Of course one could still implement such a system without using threads. It would be a challenge though. Using threads, one can very naturally design a system to handle multiple requests. A simple approach would be to create a thread for each request received. The main thread would create this thread upon receipt of a request. The thread would then be responsible for the conversation with the client program from that point on. After retrieving the image, the thread would terminate itself. This would provide a smooth system that would continue to service requests even though it was busy serviceing other requests at the same time.
Basic Approach

The create a thread, you must specify a function that will become the entry point for the thread. At the operating system level, this is a normal function. We have to do a few tricks to wrap a C++ class around it because the entry function cannot be a normal member function of a class. However, it can be a static member function of a class. This is what we will use as the entry point. There is a gotcha here though. Static member functions do not have access to the this pointer of a C++ object. They can only access static data. Fortunately, there is way to do it. Thread entry point functions take a void * as a parameter so that the caller can typecast any data and pass in to the thread. We will use this to pass this to the static function. The static function will then typecast the void * and use it to call a non static member function.
The Implementation

It should be mentioned that we are going to discuss a thread class with limited functionality. It is possible to do more with threads than this class will allow.

class Thread
{
public:
Thread();
int Start(void * arg);
protected:
int Run(void * arg);
static void * EntryPoint(void*);
virtual void Setup();
virtual void Execute(void*);
void * Arg() const {return Arg_;}
void Arg(void* a){Arg_ = a;}
private:
THREADID ThreadId_;
void * Arg_;

};

Thread::Thread() {}

int Thread::Start(void * arg)
{
Arg(arg); // store user data
int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
return code;
}

int Thread::Run(void * arg)
{
Setup();
Execute( arg );
}

/*static */
void * Thread::EntryPoint(void * pthis)
{
Thread * pt = (Thread*)pthis;
pthis->Run( Arg() );
}

virtual void Thread::Setup()
{
// Do any setup here
}

virtual void Thread::Execute(void* arg)
{
// Your code goes here
}

It is important to understand that we are wrapping a C++ object around a thread. Each object will provide an interface to a single thread. The thread and the object are not the same. The object can exist without a thread. In this implementation, the thread does not actually exist until the Start function is called.

Notice that we store the user argument in the class. This is necessary because we need a place to store it temporarily until the thread is started. The operating system thread call allows us to pass an argument but we have used it to pass the this pointer. So we store the real user argument in the class itself and when the execute function is called it can get access to the argument.

Thread(); This is the constructor.

int Start(void * arg); This function provides the means to create the thread and start it going. The argument arg provides a way for user data to be passed into the thread. Start() creates the thread by calling the operating system thread creation function.

int Run(void * arg); This is a protected function that should never be tampered with.

static void * EntryPoint(void * pthis); This function serves as the entry point to the thread. It simply casts pthis to Thread * and

virtual void Setup(); This function is called after the thread has been created but before Execute() is called. If you override this function, remember to call the parent class Execute().

virtual void Execute(void *); You must override this function to provide your own functionality.
Using The Thread Class

To use the thread class, you derive a new class. you override the Execute() function where you provide your own functionality. You may override the Setup() function to do any start up duties before Execute is called. If you override Setup(), remember to call the parent class Setup().
Conclusion

This section presented an implementation of a thread class written in C++. Of course it is a simple approach but it provides a sound foundation upon which to build a more robust design.

If you have comments or suggestions, email to bimleshsharma@gmail.com

h1

How to verify running port of windows system

April 2, 2009

netstat -ao |find “Port number”

h1

How to shutdown windows system from command prompt

April 2, 2009

shutdown

h1

How to get task list and process ID of running programs on windows

April 2, 2009

Just type tasklist on your command prompt.

h1

How to get MAC address of windows system.

April 2, 2009

Just type below command at your DOS command promt:( That will work in XP and above)
getmac – quickly grab just the mac addresses of your NICs without having to use ipconfig /all

h1

How to install (PPTP client) VPN in ubuntu system

March 15, 2009

Ubuntu Linux

Ubuntu Linux has a built-in, open source VPN client that is compatible with our Cisco VPN system, so you don’t need to download the Linux client on the VPN download page. These instructions are for the Breezy, Dapper, and Edgy versions of Ubuntu. Run ‘lsb_release -c‘ to check your version. If you are running Warty or Hoary, we recommend that you upgrade the operating system first, as these versions no longer receive security updates.

Install the VPN client

  1. In a terminal on your personally-owned laptop or computer, become root:
    sudo bash
  2. Install the vpnc package:
    aptitude install vpnc
    If this command installs the package, you can skip to Configure the VPN client.
  3. If aptitude says it couldn’t find the “vpnc” package, type:
    lsb_release -c
    The output indicates your Ubuntu version. At the top-left of the screen, select:
    System -> Administration -> Synaptic Package Manager
    then select:
    Settings -> RepositoriesFor Edgy machines, the window will look like this:

    screenshot with all repository selection boxes checked

    Make sure all the check marks are checked in the Ubuntu 6.10 tab, as shown.
    Hit ‘Close’ and ‘Reload’. Close the Synaptic Package Manager and (as root) rerun:

    aptitude install vpnc

    For Dapper machines, the window will look like this:

    screenshot with Ubuntu 6.06 LTS (Binary) - Community maintained Universe entry checked

    Make sure the ‘Ubuntu 6.06 LTS (Binary) – Community maintained (Universe)’ entry is checked in the
    Installation Media tab, as shown. Hit ‘Close’ and ‘Reload’. Close the Synaptic Package Manager and (as root) rerun:

    aptitude install vpnc

    For Breezy machines, click on ‘Add’. The window will look like this:

    screenshot of Breezy Badger repositories all four components are checked

    Under the ‘Ubuntu 5.10 “Breezy Badger”‘ repository, ensure all four components are checked, as shown.
    Click on Ok, Ok, and then Yes to reload the repository database.
    Close the Synaptic Package Manager and (as root) rerun:

    aptitude install vpnc

  4. if aptitude install vpnc worked well or vpnc installed then proceed to configuration part.

Configure the VPN client( your correct login should work as root. so if any issue then prefix “sudo” with any command.)

  1. While still as root, go to the vpnc config directory:
    cd /etc/vpnc   (If permission denied issue then change permission of vpnc folder- sudo chmod 777 vpnc)
  2. Create a custom profile:
    cp example.conf cs-vpn.conf
  3. Edit cs-vpn.conf (e.g. ‘pico cs-vpn.conf‘)
  4. At the top you’ll see something like this: IPSec gateway 192.0.2.32
    IPSec ID myGroup
    IPSec secret myGroupPWD
    Xauth username myUserName
    Set the following information:

    IPSec gateway <Host Server>
    IPSec ID <Vpn Username>
    IPSec secret <Vpn Password>
    Xauth username <your system username>

    Where <Host>, <Username>, and <Password> are taken from the bottom of the VPN download page
    and <your_System_username> is your normal system username. (Don’t include the brackets.)

  5. Save the file and exit. Logout from root within the terminal.

Run the VPN client

In order to connect to the VPN, just type:

sudo vpnc cs-vpn

Enter your CS password when prompted.
To disconnect, type:

sudo vpnc-disconnect

h1

How to build VC2005(vcproj) project using command line

March 10, 2009

Open command prompt of windows box.

input the following command :

devenv <Path of  vcproject> /rebuild release

For example:

devenv C:\work\svn.s\trunk\L\blydll\blydll2005.vcproj /rebuild release
devenv C:\work\svn.s\trunk\L\blydll\blydll2005.vcproj /rebuild debug