# SOLIDWORKS PDM Professional API Help

# Creating Add-in Hooks (C++)

This topic shows how to implement IEdmAddIn5::GetAddInInfo and IEdmAddIn5::OnCmd in your add-in to have SOLIDWORKS PDM Professional notify your add-in whenever a file is added, checked out, or checked in.

1. Call IEdmCmdMgr5::AddHook from your add-in's GetAddInInfo method for each activity you want your add-in to know about. Implement IEdmAddIn5::GetAddInInfo in your add-in as follows:

> STDMETHOD(GetAddInInfo)(EdmAddInInfo * poInfo, IEdmVault5 * poVault, IEdmCmdMgr5 * poCmdMgr)
> {
> //The AFX_MANAGE_STATE macro is needed for MFC applications but should not
> //be used for applications that are MFC-free
> AFX_MANAGE_STATE(AfxGetStaticModuleState());
> if (poInfo == NULL || poCmdMgr == NULL )
> return E_POINTER;
> //Return information to the Administrate Add-ins dialog box
> poInfo->mbsAddInName= SysAllocString( L"My first add-in" );
> poInfo->mbsCompany = SysAllocString( L"The name of my company" );
> poInfo->mbsDescription= SysAllocString( L"This is a very nice add-in." );
> poInfo->mlAddInVersion = 1;
> //SOLIDWORKS PDM Professional 5.2 is required to install this add-in
> poInfo->mlRequiredVersionMajor = 5;
> poInfo->mlRequiredVersionMinor= 2;
> //Notify when a file has been added
> poCmdMgr->AddHook( EdmCmd_PostAdd, NULL );
> //Notify when a file has been checked out
> poCmdMgr->AddHook( EdmCmd_PostLock, NULL );
> //Notify when a file is about to be checked in
> poCmdMgr->AddHook( EdmCmd_PreUnlock, NULL );
> //Notify when a file has been checked in
> poCmdMgr->AddHook( EdmCmd_PostUnlock, NULL );
> return S_OK;
> }

2. Implement IEdmAddIn5::OnCmd in your add-in as follows:

> STDMETHOD(OnCmd)(EdmCmd * poCmd, SAFEARRAY * * ppoData)
> {
> //The AFX_MANAGE_STATE macro is needed for MFC applications but should not
> //be used for applications that are MFC-free
> AFX_MANAGE_STATE(AfxGetStaticModuleState());
> if (poCmd == NULL ||ppoData == NULL)
> return E_POINTER;
> //Check the type of hook that triggered this call
> CString oName;
> switch(poCmd->meCmdType )
> {
> case EdmCmd_PostAdd: oName = "PostAdd"; break;
> case EdmCmd_PostLock: oName = "PostLock"; break;
> case EdmCmd_PreUnlock: oName = "PreUnlock"; break;
> case EdmCmd_PostUnlock: oName = "PostUnlock"; break;
> default: oName = "?"; break;
> }
> //Obtain a pointer to the array data
> if( SafeArrayGetDim( *ppoData ) != 1 )
> return E_INVALIDARG;
> EdmCmdData *poElements = NULL;
> HRESULT hRes = SafeArrayAccessData( *ppoData, (void**)&poElements );
> if( FAILED(hRes) )
> return hRes;
> //Append the paths of all files to a string
> CString oMessage = "The following files were affected by a " + oName + " hook:\n";
> int iCount=(*ppoData)->rgsabound->cElements;
> for( int i = 0; i < iCount; ++i )
> {
> oMessage += (LPCTSTR)bstr_t(poElements->mbsStrData1 );
> oMessage += "\n";
> ++poElements;
> }
> //Release the array data and display a message to the user
> SafeArrayUnaccessData( *ppoData );
> MessageBox((HWND)poCmd->mlParentWnd, oMessage, "SOLIDWORKS PDM Professional", MB_OK );
> return S_OK;
> }

> OnCmd is called for each of the hooks registered in GetAddInInfo. You can tell which hook triggered the call by inspecting the meCmdType member of the EdmCmd structure that is passed as poCmd in OnCmd. meCmdType contains an EdmCmdType constant that indicates the triggering hook.

> The second argument to OnCmd is an array of EdmCmdData structures. There is one element in the array for each file that is affected by the call. The contents of the structure members vary depending on the type of hook that triggers the call. See EdmCmdData for a complete list of members and their descriptions.

3. B uild the add-in DLL and add it to the file vault using the Administration tool .

4. Try adding, checking out, and checking in vault files.

NOTE: OnCmd is not called during check-in if the file is not modified before it is checked in. During check-in of unmodified files, SOLIDWORKS PDM Professional triggers an "undo check-out" event. To handle this "undo check-out" event, add hooks for EdmCmdType.EdmCmd_PreUndoLock and EdmCmdType.EdmCmd_PostUndoLock to your add-in's GetAddInInfo.

## See Also

Creating Add-ins (C++)

Creating Add-in Hooks (VB.NET)