How to create a GUID programmatically?

Do the following… [sourcecode language=’cpp’]GUID guId = { 0 }; CoCreateGuid( &guId ); // or UUID uid = { 0 }; UuidCreate( &uid );[/sourcecode] For converting to string use… [sourcecode language=’cpp’]PUCHAR pChar = 0; // Convert to string UuidToString( &uid, &pChar ); MessageBox( NULL, (char*)pChar, NULL, MB_OK ); //Don’t forget to free allocated string RpcStringFree( […]
Continue reading…

 

Freeing unused libraries while using COM!

It’s a good practice to free unused COM dlls if it’s not used anymore. So is there a function call which does this? Yes, it’s called CoFreeUnusedLibraries or CoFreeUnusedLibrariesEx! Are the libraries immediately removed from the internal COM dll list, no there is a time delay of 10 minutes for MTAs and neutral apartment models, […]
Continue reading…

 

Useful macros for working with VARIANT!

VARIANT is a COM data type desgined to hold different kinds types of values. VARIANT structure contains a member vt of type VARTYPE. This vt determines the type of data that a VARIANT object will hold! For eg: [sourcecode language=’cpp’]VARIANT vtVarUINT; vtVarUINT.vt = VT_UI4; vtVarUINT.ulVal = 123;[/sourcecode] So every vt type has a corresponding value […]
Continue reading…

 

Dynamically determining the base class of a class using templates

Well nearly dynamically! When working on the ATL COM framework I came across a piece of framework code which makes a template parameter as the base class of a class which looked quite useful to me. Here is a demo… [sourcecode language=’cpp’]template class SomeWnd : public BaseClass {     public: LRESULT WindowProc(UINT uMessage, WPARAM wParam, […]
Continue reading…

 

How to resolve a shortcut!

Ever wondered how to resolve a shortcut! There is a hidden helper function if you are using MFC, it’s called AfxResolveShortcut. I customized it a bit, so that we can use it independently without MFC, and here is the finished product. 😉 [sourcecode language=’cpp’]BOOL ResolveShortcut( HWND hWnd_i,                       LPCTSTR lpctszFileIn_i,                       LPTSTR  lptszFileOut_o,                       const int nPathLength_i […]
Continue reading…

 

Easy way to display Gif’s, Ico’s, JPEG’s and BMP’s on your window

Here is a function that does this! You only have to call this function with an image file, (gif, jpeg, jpg, bmp, ico) and a window handle onto which picture is to be painted. This is originally from MSDN but I modified it slightly to make it into one function… 😉 [sourcecode language=’cpp’]// This function loads […]
Continue reading…

 

How to create SafeArray of BSTR’s?

Here is a sample to create an array of BSTR’s… [sourcecode language=’cpp’]const long lCount = 100; _bstr_t bstrArray[lCount]; COleSafeArray cosaSafeArray; cosaSafeArray.CreateOneDim( VT_BSTR, lCount ) // Fill out this array for( long lIndex = 0; lIndex < lCount; ++lIndex ) { cosaSafeArray.PutElement( &lIndex, static_cast( bstrArray[lIndex] ); } // Now you have a safe array of bstr’s and since […]
Continue reading…