2 Replies Latest reply on Nov 14, 2016 10:21 AM by Mikhail Vashkevich

    ICommandGroup::Activate crashes in C++ AddIn

    Mikhail Vashkevich

      Hi everybody!

      Could anyone give me a hand in understanding what I'm doing wrong in the following part of code, written in C++?

      The matter is that during the execution the line of code "pCommandGroup->Activate(&vbResult);" causes a crash.

       

      extern "C" void __declspec (dllexport) Test_command()
      {
      }

       

      extern "C" int __declspec (dllexport) OnUpdateTest()
      {

           return 1;
      }

       

      HRESULT MyAddIn::ConnectToSW(LPDISPATCH ThisSW,long Cookie,VARIANT_BOOL *IsConnected)
      {
            *IsConnected=VARIANT_FALSE;

            CComQIPtr <ISldWorks> pSldWorks=ThisSW;
            if (!pSldWorks)
                return S_FALSE;

       

            CComPtr <ICommandManager> pCommandManager;
            pSldWorks->GetCommandManager(Cookie,&pCommandManager);
            if (!pCommandManager)
                return S_FALSE;

       

            long lErrors=0;
            CComPtr <ICommandGroup> pCommandGroup;
            pCommandManager->CreateCommandGroup2(0,L"Title",L"Tooltip",L"Hint",-1,VARIANT_TRUE,&lErrors,&pCommandGroup);
            if (!pCommandGroup)
                return S_FALSE;

       

            long lCommandID1=0;
            pCommandGroup->AddCommandItem2(L"Command1",0,L"",L"",-1,L"Test_command",L"OnUpdateTest",0,swMenuItem,&lCommandID1);

            pCommandGroup->put_HasMenu(VARIANT_TRUE);

            VARIANT_BOOL vbResult=FALSE;
            pCommandGroup->Activate(&vbResult); // It crashes during the exacution

       

            *IsConnected=VARIANT_TRUE;
            return S_OK;
      }

       

      My assumption is that could be due to incorrect siganure for the callbacks in the method ICommandGroup::AddCommandItem2.  As a matter of fact, if I put there empty strings for the callbacks like this

      pCommandGroup->AddCommandItem2(L"Command1",0,L"",L"",-1,L"",L"",0,swMenuItem,&lCommandID1);

      the crash doesn't occur.

      Unfortunately, all samples how to use this function are only available for C# and VB.

      Thank you for any assistance!

        • Re: ICommandGroup::Activate crashes in C++ AddIn
          Simone Padovan

          Hello Mikhail,

          you forgot the call to ISwAddin::SetAddinCallbackInfo2. This method provides the information about the menu callback functions, so Solidworks will know about function entries. Moreover set callback functions "more COM compliant" returning an HRESULT value:

          STDMETHOD (Test_command)();

          For "enabled method" use an out parameter to enable / disable the attached command:

          STDMETHOD (OnUpdateTest)(long* status);

          Don't forget to expose from your COM object the IDispatch interface: I use ATL so, COM_INTERFACE_ENTRY(IDispatch).

          I hope this help you.

          Regards,

          Simone

            • Re: ICommandGroup::Activate crashes in C++ AddIn
              Mikhail Vashkevich

              Hello Simone!

              Thanks a lot for your reply!

              A long time passed since I asked my question.

              After struggling for a while with the problem I noticed by myself that I forgot to call ISwAddin::SetAddinCallbackInfo2.

              After I corrected the mistake my code stopped to crash but unfortunately it still didn't work as it was expected.

              I provided my own implementation of IDispatch interface to pass it to ISwAddin::SetAddinCallbackInfo2 that should handle the callbacks but for some reason SolidWorks didn't even try to call any of its method (neither IDispatch::Invoke, nor IDispatch::GetIDsOfNames, nor anything else). Actually only one method IUnknown::QueryInterface was called but it didn't ask for any special interface that was not supported.

              After fighting for some time with that strange behavior and having no success I decided to write a small application in C#  that should handle the creation of Command Manager tabs (that was my initial goal) and dispatch callbacks to my C++ module. Not hard to guess that code written in C# began to work just fine!

              Well, for me that was enough. Unfortunately I didn't understand what was wrong in the same thing written in C++.