Category: Windows

XOR facts

If you XOR a number even times by itself, you will end up with zero but if you do it odd times, you will be getting the number itself. This logic can be used in many different problems, e.g. finding the odd and even number of times a number occurs and so on.

Continue Reading →

vs2005: fatal error C1083: Cannot open include file: 'Mscat.h': No such file or directory

add the following code: extern “C” { typedef HANDLE HCATADMIN; typedef HANDLE HCATINFO; typedef struct CATALOG_INFO_ { DWORD cbStruct; WCHAR wszCatalogFile[MAX_PATH]; } CATALOG_INFO; BOOL WINAPI IsCatalogFile( HANDLE hFile, WCHAR* pwszFileName ); BOOL WINAPI CryptCATAdminAcquireContext( HCATADMIN* phCatAdmin, const GUID* pgSubsystem, DWORD dwFlags ); BOOL WINAPI CryptCATAdminCalcHashFromFileHandle( HANDLE hFile, DWORD* pcbHash, BYTE* pbHash, DWORD dwFlags ); HCATINFO

Continue Reading →

Iterate through all the tabs in Internet Explorer Windows

I found this code useful when you want to go through each tab and then look into their URLs and so on. You can also quit each tab individually. [sourcecode lang=”cpp”] // Implementation #include #include typedef BOOL (CALLBACK * IEENUMPROC)(HWND hwnd, LPVOID pif, LPARAM lParam); static BOOL CALLBACK EnumIEChildWindows(HWND hwnd, LPARAM lParam) { TCHAR szClassName[100];

Continue Reading →

IHTMLCollection iterating through HTML frames / GUI manipulation

LRESULT lResult; UINT nMsg = ::RegisterWindowMessage( _T(“WM_HTML_GETOBJECT”) ); ::SendMessageTimeout( handleToInternetExplorerWindow, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lResult); LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, “ObjectFromLresult” ); HRESULT hr; CComPtr htmlDoc2; hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0, (void**)&htmlDoc2); if ( SUCCEEDED(hr) ) { IHTMLFramesCollection2 *p; htmlDoc2->get_frames(&p); long lLen; p->get_length(&lLen); for (long i = 0; i < lLen; i++) {

Continue Reading →

_SECURE_SCL issues

Be careful when using _SECURE_SCL option when integrating your DLLs with DLLs you received from someone else. If you have compiled your dlls with this option but the dlls you use havent, you are most likely going to get a runtime application compatibility error. To fix this just remove this option. When secure SCL is

Continue Reading →

Getting the parent id of a process

Below is the code to return the parent id of a process, given a process id. This can be helpful in circumstances when we have to scan all the running processes to find out the spawned processes from another process. [code language=”cpp”] // change this method signature according to your requirements BOOL GetParentPID( DWORD dwParentId,

Continue Reading →

Using Virtual Box COM APIs to list the Virtual machine snapshots

int Helper_ReturnVms(IVirtualBox *virtualBox) { HRESULT rc; bool bFound=false; /* * First we have to get a list of all registered VMs */ SAFEARRAY *machinesArray = NULL; rc = virtualBox->get_Machines(&machinesArray); if(FAILED(rc)) { SafeArrayDestroy (machinesArray); return -1; } IMachine **machines; rc = SafeArrayAccessData (machinesArray, (void **) &machines); if(FAILED(rc)) { SafeArrayUnaccessData (machinesArray); SafeArrayDestroy (machinesArray); return -1; } for

Continue Reading →

reading messages from message table

Sometimes data is also stored as message instead of resources. the easiest way to read this is as below: ofstream myfile; myfile.open (“example.txt”); HMODULE handle = LoadLibrary(“AgentRes.dll”); // dll for reading the file DWORD err=::GetLastError(); for(int i=0;i<10000;i++) { LPTSTR    lpMsgBuffer=NULL; DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE, handle, i, // number of the corresponding message // you

Continue Reading →

Windows debugging using windbg

Sometimes or most times when you get a crash dump from your customer, its always good to know how to reproduce the crash and point out where the actual crash happens. Today I was debugging some bug with my lead, I found this very useful: Here are the steps: First of all open the windows

Continue Reading →

Hacking into DLLs [ COM ]

This is especially useful when you want to use someone else’s dlls. 1) Open the dll in windows dependency viewer: You should see the following, if its a COM dll. This basically is related to COM. If you go through COM tutorials you will know about this. 1)  next open the dll you need in

Continue Reading →

Using DebugBreak()

I learnt this from my manager. DebugBreak is powerful tool you can use when debugging applications. I had this scenario where I need to use a java app which was internally calling C++ methods, basically JNI functions. The error in question was in the C++ side, so not really an option to use debugger. Therefore

Continue Reading →

Virtual Store and UAC

As mentioned previously, many legacy Windows applications were created so you could access parts of the file system and registry that are now locked in Windows Vista, and many of these applications are not being immediately updated. However, Microsoft has devised an interesting solution within Windows Vista to provide backward compatibility so that legacy software

Continue Reading →

SubVersion error: 1053: not able to create a service

When trying to run a subversion or svn service, we get this error stating the service is not running, this is because of the way you create the service: Below are the right and wrong way to create it: WRONG WAY: F:Program FilesSubversionbin>sc create svnserver binpath= “F:Program FilesS ubversionbinsvnserve.exe –service -r F:Dev FilesRepository” displayname= “S

Continue Reading →

Windows developer tools you need to have

Some tools which I really find helpful are: a. Multimon – Support for multiple monitors b. Process Monitor c. Process Explorer d. Desktops – Desktops allows you to organize your applications on up to four virtual desktops. e. OleView – For COM purposes f. Spy++ – For windows GUI handling g. Resource hunter – Reading

Continue Reading →

Detecting memory leaks in VC++

One of the simplest ways to detect a leak in your code would be to use debugCRT. Simple usage: a. Create you project in visual studio b. run the project in debug mode, then only you will find the memory leak dumps int main() { _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_ALLOC_MEM_DF); //below call will break where the leak

Continue Reading →

creating a new registry key in windows using VC++

Below is the code to create a registry key in VC++. I used visual studio 2008 for this(VC9) RegCreateKeyEx is the command to create the key. More documentation can be found online@ msdn. #include “stdafx.h” #include “Windows.h” int _tmain(int argc, _TCHAR* argv[]) { HKEY hOutKey; RegCreateKeyEx(HKEY_LOCAL_MACHINE,  L”SOFTWARE\ABC\Registration\addkey”, 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hOutKey, NULL); return

Continue Reading →

comparing two systemtime variables using filetime in vc++

lets take a look at how to compare two systemtime variables but using Filetime to do so. We make use of the utility method CompareFiletime to do this. 1) store the systemtime in a variable 2) convert it into filetime. Use this method SystemTimeToFileTime(&systemtime1, &filetime1) SystemTimeToFileTime(&systemtime2, &filetime2) 3) Compare the filetimes using the method CompareFileTime(&filetime1,

Continue Reading →

How I make use of beyond compare when coding?

Recently I trying to read a token file(basically a file which considers some encrypted text/plain text containing a password) into a buffer and then read that for verification purposes. Example file as below: <?xml version=”1.0″ encoding=”UTF-8″ standalone=”no” ?> <RSAKeyValue><Modulus>HOYfY2s4gKaT2DbXNgcps3vVfloYz5DzrEK</:Modulus> </RSAKeyValue> </enabled>NULL The above is just a example of the content inside the file. Now I

Continue Reading →

Windows 7 –

Security center is now Action center: Searching for security center in windows 7, you would come up with nothing. All the items over here are shifted to Action Center.  Not sure why they did that, but security center was better User Access control: UAC has four stages. Each stage has option to configure the level

Continue Reading →

Implementing GetSystemDEPPolicy Function

For those of you, who dont know about GetSystemDEPPolicy Function, Please refer to this link: http://msdn.microsoft.com/en-us/library/bb736298%28VS.85%29.aspx For others lets see on implementing this: GetSystemDEPPolicy Function Gets the data execution prevention (DEP) policy setting for the system. Syntax in C++: DEP_SYSTEM_POLICY_TYPE  WINAPI  GetSystemDEPPolicy(void); Describe a typedef in the beginning. typedef      DEP_SYSTEM_POLICY_TYPE (WINAPI *ptr_getsystemdeppolicy) (void); Next you

Continue Reading →

Memory Leaks when using windows handles

We use handles in most cases when opening up a file, for eg: void methodA() { int xHandle = 0; xHandle = open(“c:windowssys.txt”, O_RDONLY); } If we do not close this handle, we are bound to get a memory leak everytime we call this method. You can verify this by opening up task manager and

Continue Reading →

Error: WINDOWINFO not found while compiling visual studio 2008

Incase you get a similar type of error when trying to compile visual studio 2008, goto microsoft.com and download Microsoft platform SDK for windows 2003 SP1. the link is: http://www.microsoft.com/downloads/details.aspx?familyid=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5&displaylang=en Warning: do not download the file through the download button on the page. Scroll down and you will find some files for download with similar

Continue Reading →

How to unit test a windows service

I was writing some unit test using CppUnit for some methods. I needed to test some methods which was written in windows to manipulate windows services. Below is basically a rough idea on how to do this: One such example is a method which tests the windows service state. Let us call this method TestServiceState():

Continue Reading →

wbemuuid.lib: compilation errors vc6

wbemuuid.lib: compilation errors vc6 One of the errors we often see, when compiling vc6 and when you have included the library wbemuuid.lib, is the recompile module or corrupt error. The best thing to do would be replace this lib in c:Program filesMicrosoft Platform SDKLib This would work fine

Continue Reading →

creating videos compatible with zune

Instead of spending money on buying a convertor to play your videos on zune. Download windows movie maker OR I suppose this is a component already on your windows OS. Import videos into your windows video maker. Then save your videos in the WMV format. When done just synchronize your videos with zune. Your done!!!

Continue Reading →