Saturday, April 10, 2010

WIN32 API

Sample Code To Create and Kill A Process:

void CWindowHandleDlg::OnBnClickedOk()

{
   STARTUPINFO StartupInfo;

   PROCESS_INFORMATION PrcInfo;
   HANDLE hHandle;
   DWORD dwExitCode=0;

    memset(&StartupInfo,0,sizeof(StartupInfo));
   StartupInfo.cb = sizeof(StartupInfo);
   StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
   StartupInfo.wShowWindow = SW_SHOW;

   ::CreateProcess("C:\\Windows\\System3\\Notepad.exe",NULL,NULL,NULL,FALSE,0,NULL,NULL,&StartupInfo,&PrcInfo);  /*Create Process

    hHandle = ::OpenProcess(PROCESS_ALL_ACCESS,0,PrcInfo.dwProcessId); // handle of that process

  ::GetExitCodeProcess(hHandle,&dwExitCode); // Check for the status of that handle

   if(dwExitCode==STILL_ACTIVE) // if its active terninate the process.
      ::TerminateProcess(hHandle,dwExitCode);
 }

 Initially a Thread is created by CreateProcess

Create A Process:

 BOOL WINAPI CreateProcess(

_in_opt LPCTSTR lpApplicationName, __inout_opt LPTSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCTSTR lpCurrentDirectory,
__in LPSTARTUPINFO lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);

  • lpApplicationName - the Exe which has to be launched. The 9 th and the 10 th parameter must be initialized .
  • You can leave all the other parameters  as null and the bool variable bInheritHandles (which allows inheritance) can be set to  FALSE .
  • A structure STARTUPINFO has to be passed. Before passing it is memset to zero. The structure's variable cb has to be intialized to the structure size.
  • lpProcessInformation has to be passed which stores all the information about the launched exe such as ProcessID ,ThreadID..etc.
Check the Status of Process Created:

  • Before Terminating the process its better to check for the exsistence of the process.
  • Handle of the process created is recieved from the variable lpProcessInformation.dwProcessId.
  • If the process is still active then we can terinate the process.

Terminating the process:
       ::TerminateProcess(hHandle,dwExitCode);
  • Just the window handle alone with the status of that handle.

Sunday, April 4, 2010

Remote Debugging In Visual Studio

How to debug an issue which is replicated in a test machine but not in your debugger. That's where Remote Debugger comes  handy!

Target Machine - Test machine where issue is replicated.
Host  Machine - Machine where Visual Studio Debugger is present ( Source Code present in debug mode).

The concept applied in Remote Debugger is " Application which is running in target machine is attached to our  host machine through visual debugger". When once the process/application is attached to the host machine the debugger (Visual Studio) considers the apllication to be running locally on the target machine.

Steps followed to set up Remote Debugger:

Note:
  • Before setting up remote debugger make sure that the build EXE is the exact match of the source code  present in host machine. 
  • Make sure the .pdb file which is generated is present in host machine . This  .pdb file is generated automatically on building an EXE.
Steps to be followed in Target Machine:
  • Copy the [drive]:\Program Files\ Microsoft Visual Studio9.0\Common7\IDE\Remote Debugger folder present in host machine  into Target Machine(any drive).
  • Launch your application in Target Machine.
  • Launch msvsmon.exe in Target Machine. This msvsmon.exe is present in Remote Debbuer folder which you have copied in the target machine.
  • When you run the msvsmon.exe you will get a pop regarding security issues  which is set to guest only.Click "yes".
  • Next you might get a pop up regarding configuration of firewall.Select second or third option to allow the host machine to attach to the process/application running in the that machine.
  • Next go to tool/options . On the group box select No Authenitication and click on Allow any user to debug .
  • Make note of the server name and port which will be required from the host side. Click OK .
Steps to be followed in host machine:
  • On Host machine go  Visual Studio ->Tools->AttachTo Process (Ctrl-Alt-P).Change Transport to Remote( Native Only with no authentication).
  • Browse for target machine on your network in the Qualifier Input.
  • It will automatically show all the process attched in that machine or select Show process from all users.
  • Then select the process/EXE to which you would want to debug .Select Attach .
When once the process is attched to our host machine we would be able to debug as usual. Happy Debugging!