Question : CertSelectCertificate hook Callback Function does not get called

I am trying to use a callback function in order to center the certificate window, but it can't seem to get it to work.

I have set the pfnHook pointer with
certSelect.pfnHook = (PFNCMHOOKPROC)&CertHookCallback;

I have  set the flag saying that a hook function should be called using:      
certSelect.dwFlags = CSS_ENABLEHOOK;

The selectCert dialog is displayed but it does not hit my breakpoint in the callback function nor does it print, so i assume it is never reached. What have i missed?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
static UINT_PTR CALLBACK CertHookCallback( 
	HWND hDlg,
	UINT uiMsg,
	WPARAM wParam,
	LPARAM lParam)
{
          Trace(TRC_LOGIC, ("Callback function reached.\n"));
	if ( uiMsg == WM_SIZE) {

            // Center window and bring it to front
            Center(hdlg); 

         }
}

GetCert()
{
CERT_SELECT_STRUCT certSelect;
	PCCERT_CONTEXT pSelCert = NULL;
	// set cert dialog data and call function

	memset(&certSelect, 0, sizeof(CERT_SELECT_STRUCT));

	DLGTEMPLATE dlgTemplate;

	dlgTemplate.x = 0;
	dlgTemplate.y = 0;
	//certSelect.pTemplateName =&dlgTemplate;

	certSelect.dwFlags = CSS_ENABLEHOOK;
	certSelect.dwSize = sizeof(CERT_SELECT_STRUCT);
	certSelect.hwndParent = GetForegroundWindow();
	certSelect.pfnHook = (PFNCMHOOKPROC)&CertHookCallback;
	certSelect.cCertStore = 1;  
	certSelect.arrayCertStore = &hTmpStore;
	certSelect.cCertContext = 1;  
	certSelect.arrayCertContext = &pSelCert;


	DWORD rc = pCertSelectCertificate(&certSelect);


}

Answer : CertSelectCertificate hook Callback Function does not get called

Using code from Sign.CPP (a sample file in the SDK), I was able to reproduce the problem.  However, the return code was 1 (true)  -- I was able to select a cert, but the hook did not get called.

There is a possble clue:  
Note that the docs refer you to "See Hooks" which is at
    http://msdn.microsoft.com/en-us/library/ms632589(VS.85).aspx
and is all about Windows Hooks... which seems odd to me.  This is really a callback, not a hook, per se.

However, one item in there might apply.  Note that in SetWindowsHookEx http://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx  the docs now say that you need 64-bit hook code to inject into a 64-bit process.  Like I say, it does not seem like it should apply, but it might.

Are you having this problem on a Win7 or Vista 64-bit machine?

====================

If you can't fix this, then a possible workaround would be to spin-off a thread that would watch for the dialog to appear then move it.  See Simple Multithreading in Visual C++

Random Solutions  
 
programming4us programming4us