Skip to content

Commit

Permalink
Return #N/A if a heavy function is called from a dialog box.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancidev committed Oct 17, 2015
1 parent 624fcca commit 9f147b2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
36 changes: 35 additions & 1 deletion XllConnector/Invoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,38 @@ ExcelVariant ExcelCall(int xlfn, const T&... args)
Excel12(xlFree, 0, 1, &result);
return vResult;
}
#endif
#endif

namespace XLL_NAMESPACE
{
// How to: Call XLL Functions from the Function Wizard or Replace Dialog Boxes
// https://msdn.microsoft.com/EN-US/library/office/bb687909.aspx

static BOOL CALLBACK IsDialogBoxOpen_Callback(HWND hwnd, LPARAM lParam)
{
bool *pFound = (bool*)lParam;

DWORD dwProcessId = 0;
GetWindowThreadProcessId(hwnd, &dwProcessId);
if (dwProcessId == GetCurrentProcessId())
{
WCHAR szClassName[100];
if (GetClassNameW(hwnd, szClassName, ARRAYSIZE(szClassName)))
{
if (_wcsnicmp(szClassName, L"bosa_sdm_XL", 11) == 0)
{
*pFound = true;
return FALSE; // stop iterating
}
}
}
return TRUE; // keep iterating
}

bool IsDialogBoxOpen()
{
bool bFoundDialog = false;
EnumWindows(IsDialogBoxOpen_Callback, (LPARAM)&bFoundDialog);
return bFoundDialog;
}
}
4 changes: 4 additions & 0 deletions XllConnector/Invoke.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ namespace XLL_NAMESPACE
explicit ExcelException(int errorCode);
const char* what() const override { return m_errorMessage; }
};

// Returns true if the Function Wizard or Replace dialog box is
// open in the current Excel session.
bool IsDialogBoxOpen();
}
6 changes: 6 additions & 0 deletions XllConnector/Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "FunctionInfo.h"
#include "Conversion.h"
#include "Marshal.h"
#include "Invoke.h"

//
// strip_cc, strip_cc_t
Expand Down Expand Up @@ -179,6 +180,11 @@ namespace XLL_NAMESPACE
{
try
{
if (IsHeavy && IsDialogBoxOpen())
{
return const_cast<LPXLOPER12>(&Constants::ErrNA);
}

LPXLOPER12 pvRetVal = AllocateReturnValue(IsThreadSafe);
HRESULT hr = CreateValue(pvRetVal,
func(ArgumentMarshaler<TArgs>::Marshal(args)...));
Expand Down
2 changes: 1 addition & 1 deletion XllExamples/ArithmeticExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace custom_ns
}
}

EXPORT_XLL_FUNCTION_AS(custom_ns::GetCircleArea, GetCircleArea);
EXPORT_XLL_FUNCTION_AS(custom_ns::GetCircleArea, GetCircleArea, XLL_LIGHT);

namespace
{
Expand Down

0 comments on commit 9f147b2

Please sign in to comment.