The problem you are facing is that the Excel function wizard will call the function again while you enter the parameter values.
To get around this, your function must detect the presence of the Function Wizard and act accordingly.
++, . , #. Windows API. , Excel; Excel2013.
typedef struct _EnumStruct
{
bool wizard;
DWORD pid;
} EnumStruct, FAR* LPEnumStruct;
BOOL CALLBACK EnumProc(HWND hwnd, LPEnumStruct pEnum)
{
static const char szFunctionWizardClass[] = "bosa_sdm_XL";
static const char szFunctionWizardCaption[] = "Function Arguments";
char szClass[sizeof(szFunctionWizardClass)];
char szCaption[sizeof(szFunctionWizardCaption)];
if (GetClassName(hwnd, (LPSTR)szClass, sizeof(szFunctionWizardClass))){
if (CompareString(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, (LPSTR)szClass, (lstrlen((LPSTR)szClass) > lstrlen(szFunctionWizardClass)) ? lstrlen(szFunctionWizardClass) : -1, szFunctionWizardClass, -1) == CSTR_EQUAL){
DWORD pid = NULL;
GetWindowThreadProcessId(hwnd, &pid);
if (pid == pEnum->pid){
if (::GetWindowText(hwnd, szCaption, sizeof(szFunctionWizardCaption))){
if (CompareString(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, (LPSTR)szCaption, (lstrlen((LPSTR)szCaption) > lstrlen(szFunctionWizardCaption)) ? lstrlen(szFunctionWizardCaption) : -1, szFunctionWizardCaption, -1) == CSTR_EQUAL){
pEnum->wizard = TRUE;
return FALSE;
}
}
}
}
}
return TRUE;
}
bool Excel12::calledFromFunctionWizard()
{
EnumStruct enm;
enm.wizard = FALSE;
enm.pid = GetProcessId(GetCurrentProcess());
EnumWindows((WNDENUMPROC)EnumProc, (LPARAM)((LPEnumStruct)&enm));
return enm.wizard;
}