Tuesday, June 29, 2010

Creating A COM DLL USING VC++

Steps for creating a COM DLL using VC++ :

1.Create an ATL Project


File->New->ATL->ATL Project.

Select Support MFC CheckBox (if u want to use MFC classe).
 
2: Add Class

Select Class View.

Add a new Class.

Visual C++ ->ATL->ATL Simple Object.

Enter Class Name.(above slide)

3: Adding Methods to Class


Open Class View.

RightClick the interface (IComInerface).

Add->Add Method.(slide2)

Enter the method name and add all the Parameters for the method.(slide3)

(Note the return type of the method by default ll be HRESULT but parameters can be passed to methods as references, while entering the parameters the appropriate type has to be selected).


 




4: Implement Method:
Open the Com Class file(.cpp) the class that you created .

Find the method you added (slide 4)

write the functionality for the method.
 

........now when u have compiled your code the com dll is created !

Thursday, June 3, 2010

AutoExpansion in Visual Studio

Lets take a CStringArray. In the debugger (Watch Window) we will be able to see only the first element inserted into the array.

What if we wanted to see all the elements  present in the array?

1. Open the file "c:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\autoexp.dat" in notepad.
2.Search for the line "[AutoExpand]"
3.add the following line below it.
      CStringArray=size = //Add this line
4. Search for the line "[Visualizer]"
5. add the following line below it.
CStringArray{ children ( #array ( expr : ($e.m_pData[$i]), size : ($e.m_nSize) ) )}
6. Save this file and close All Visual studio application and open again and try.

You can also expand other structures or classes designed by you. Refer autoexp.dat for samples.

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!