What is forward declaration? Where it benefits?
Forward declaration is important concept in C++, when you have to write classes such a way when two classes are interdependent on each other. Consider the situation your class A need to do some processing with object of Class B and vice versa. Now if you declare class A first and class B after that. You will get missing storage-class or type specifiers error for class B, though you have declaration of class B there in your project, same happen when you try to put class B first and class A afterward.
Only solution to this type of problem is Forward Declaration. You have to put dummy implementation of one class before other class, and declare the dummy class after ward.
Please see pointer also play there role there, as object must be pointer to the class, this because pointer become aware of the object when the memory to that pointer is allocated otherwise there mere dummy pointer pointing to garbage memory.
class A; // Forward Declaration
class B
{
A * aObj; //Declaration of object;public:
B(){};}
class A
{B * bObj;
public:
A();
} Â
Moving your window by dragging it by clicking on anywhere on the dialog?
For that you have to handle WM_NCHITTESTand returning HTCAPTION, which will instruct the Windows, that user is dragging through the Title-Bar and it have to move the window with it.
A small example :-
UINT CMyDialog::OnNcHitTest(CPoint point)
{
UINT nRet = CDialog::OnNcHitTest(point);
if(HTCLIENT == nRet)
{
nRet = HTCAPTION;
}return nRet;
}
Tip Courtesy :Mr. Ovidiu Cucu
Difference between WCHAR and BSTR Memory Allocation?
The BSTR – a string type used by COM than a WCHAR *. The differences are:
- Â BSTRs are allocated using the COM allocators, so they can passed to another process (and deallocated by another process). This is why you should use SysFreeString rather than delete or free.
- Â BSTRs have a length prefix (besides having null terminator).
The length is kept (as a UINT) in the memory location just before the first character.
The BSTR type is actually defined as a WCHAR *, pointing to the first character (just like a regular LPWSTR), so you can use BSTRs wherever you can use LPWSTRs. Just remember that you should use SysFreeString to deallocate it and SysAllocString to allocate it.
So, a regular string looks in memory like this:
S           o        m          e [space]    t        e          x       t    [null]
53 00 6f 00 6d 00 65 00 20 00 74 00 65 00 78 00 74 00 00
whereas a BSTR looks like this in memory
string length      S    o    m     e    [space]     t      e      x       t  [null]
09 00 00 00 53 00 6f 00 6d 00 65 00 20 00 74 00 65 00 78 00 74 00 00
Please See also this page
Tip courtesy :-Â Florin Crisan
Clicking menu item remotely (on same computer) through another application (Part2)?
Please read part 1 of same, before reading this. Because there are some shortcoming in part 1, which i am summerizing here:-
- The target window always in focus, i.e. if it loses the focus, the key press will deliver to window, which is in focus.
- Also this tip will not work, if your computer is locked.
Now how to achieve the same, without using keyboard api’s. The answer hidden in Windows message, we could send the WM_COMMAND message with parameter of Menu ID., so that it opens what ever you required. Now problem is that, from where to bring Menu ID of required menu, the SPY++ application will help you in that case. Just capture the Notepad application and hook WM_COMMAND message for Font menu. See, I am able to retrieve the Menu ID for Font menu id which is 33.
 (Please click on image, to see bigger image)
Now opening of Font Dialog of notepad could be achieve through just two lines of code, first finding the handle to window of notepad and second posting WM_COMMAND message with Menu ID in LOWARD parameter of wParam.
HWND hWnd = ::FindWindow(_T("Notepad"),NULL);
::PostMessage(hWnd,WM_COMMAND,MAKEWPARAM(33,0),0);
Clicking menu item remotely (on same computer) through another application (Part1)?
Virus??? No man no, I am trying to demonstrate the power on windows API /messages!. I will demonstrate same in two part,  first using keybd_event and another using raw Window Message Posting.
Let me demonstrate that by opening the FontDialog of Notepad application, for that you need handle to Main window of Notepad application. This tip will come handy for reterieving handle of windows. FontDialog menuitem is present in Format|Font menu, to reach there from accelerators key, you have to first press Alt+O followed by the F Key to open FontDialog. Now achieve same using code you have to rely on keybd_event api, which simulate the keyboard inputs. it is old api, this api isreplaced by SendInput api.
HWND hWnd = ::FindWindow(_T("Notepad"),NULL);
// Make the Notepad window on top
::SetForegroundWindow(hWnd);// open Format menu item
--------1-----------------
keybd_event(VK_MENU,0,0,0);
keybd_event(VkKeyScan('O'),0,0,0);
keybd_event(VkKeyScan('O'),0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
----------2--------------
// will cick the Font menuitem
keybd_event(VkKeyScan('F'),0,0,0);
See screen shot for above, I have put some sleep at Task 2, so that I could see what happening visually.
After Completion of Task 1
After Completion of Task 2
Please note, for above to work, Notepad should be always in focus, for Keyboard input, otherwise the keyboard input would be delivered to some other application.
How to create Single Instance application on Window OS where multiple user session are running using Terminal services?
If you developing desktop application, one or other day your requirement would be to develop the application, which only one instance run on particular computer. There are many articles available on this, on internet. But, just today I have to create application which would run on that computer where many parallel sessions of windows are running using terminal services. Usually, I follow this technique for creating single instance application:-
Â
// For handling Single Instance of Application
 HANDLE hSingleInstance = NULL;hSingleInstance = CreateMutex(NULL,FALSE,_T("SINGLEINSTANCE_APP"));
DWORD dwLastError = GetLastError();
 if((dwLastError == ERROR_ALREADY_EXISTS) ||
 (dwLastError == ERROR_ACCESS_DENIED))
 {
 AfxMessageBox("Application already running");
 CloseHandle(hSingleInstance);
 return 0;
 }// End handling Single Instance of Application
// Close the handle of hSingleInstance when application closes
CloseHandle(hSingleInstance);
Now making application, which have single instance for all session you have to include Global\ prefix before application mutex name or if you required that application restricted to single user session, you could use Local\ prefix (back slash included). Now let me change above for you:-
Global Single Instance:-
hSingleInstance = CreateMutex(NULL,FALSE,_T("Global\\SINGLEINSTANCE_APP"));
Local Single Instance:-
hSingleInstance = CreateMutex(NULL,FALSE,_T("Local\\SINGLEINSTANCE_APP"));
Note: Please don’t use another backslash when using Global and Local prefix and both Global and Local are case sensitive.
In how many ways you could include the library file in your project?
 There three known ways to include library file in your project
1. Directly adding it to your project work space, like this :-
(Image : VS 6)
2. Putting that in your project dependency, you have to set that in Project setting , either select that in Project | Project properties in VS2005 and Project! Settings in VS6 :-
3. using pragma to include that into your project for e.g.
if you want to include visualcpp.lib in your project, you can add this entry in your source code file.
#pragma comment (lib,_T(“visualcpp.libâ€))