Requesting Notification Before and After Run

The BeforeRun and AfterRun methods request notification before the host IDE enters run mode and after it leaves run mode. If you set the DESIGNERFEATURE_BEFORERUN and DESIGNERFEATURE_AFTERRUN flags in the registry, the host calls these methods before entering and after leaving run mode. These methods are defined as follows:

HRESULT BeforeRun(void *ppvData);
HRESULT AfterRun(void pvData);

The ppvData parameter points to data in a designer-specified format; the data are output by BeforeRun and input to AfterRun. The designer can use the parameter to save information needed before and after the project is in run mode. The run-time object does not have access to the data.

In its BeforeRun method, the visual designer sets up the data to be returned after run. In addition, it performs any other tasks it may require before running the created object, such as setting default directories for internal requests or establishing network connections.

In the following example, a designer saves the name of a temporary file that it will delete after running the created object:

STDMETHODIMP MyDesigner::BeforeRun(LPVOID FAR* ppvData)
{
// Store the path of the temporary file.
   *ppvData = CoTaskMemAlloc(m_nTempFileLen + 1);
   strcpy((char *)*ppvData, m_pszTempFile);
   return S_OK;
}

In the example, the global variable m_pszTempFile is a zero-terminated string that contains the full path of the temporary file. The integer global variable m_nTempFileLen contains the length of this string. The designer calls CoTaskMemAlloc to allocate the ppvData parameter, copies m_pszTempFile into it, and returns S_OK to indicate success.

In its corresponding AfterRun routine, the designer deletes the temporary file:

STDMETHODIMP MyDesigner::AfterRun(LPVOID pvData)
{
 if (pvData != NULL)
     RemoveTempFile((char *)pvData);
  return S_OK;
}

In the example above, the designer calls a local routine to delete the temporary file if the value of the parameter is not NULL. If the designer has specified the DESIGNERFEATURE_AFTERRUN registry flag, but not the DESIGNERFEATURE_BEFORERUN flag, the host calls only the AfterRun routine, and passes a NULL pointer in the parameter.

The designer need not free the memory allocated for the pvData parameter; the host frees it after AfterRun returns. However, if pvData contains pointers to other dynamically allocated data, the designer is responsible for freeing those nested pointers.