How to resolve PDM API incompatible MarshalAs when attempting to debug add-ins

Hello,

I am trying to debug my pdm API add-in but when the code enters the OnCmd method the debugger crashes with the error below:

InvalidMemberDeclaration was detected

Message: The following error occurred while determining how to marshal the parameters of member 'OnCmd' of type 'EdmLib.IEdmAddIn5': System.TypeLoadException: Could not load type 'EdmLib.EdmCmdData' from assembly 'pdmAddinVS2012, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. This is most likely due to an incompatible MarshalAs attribute on one of the parameters.

I have no idea what this means.

I run the add-in via notepad.exe and the debugger works OK and hits the breakpoint I set in the GetAddInInfo method, but when the code proceeds to the OnCmd method it throws the error above.

I have tried this on both win7 and win10, on both VS2012 and VS2015. I am running the pdm admin tool and Visual Studio "as admin", I cleared out the plugins folder, etc.

has anyone dealt with this problem before?

SolidworksApi macros
Answers 9
ST
ST 2018-01-08 Best Answer

This sounds like an Interop issue.

Make sure you reference EPDM.Interop.epdm.dll in C:\Program Files\SOLIDWORKS PDM

And be sure to set Embed Interop to False and Copy Local to True.

BC
BC 2018-01-08

Thanks alot...I thought I had performed your suggested steps already, but I repeated them, this time adding bot edmInterface.dll as well as EPDM.Interop.edm.dll, and did set interop to fals and copy local to true...and now it works!

BC
BC 2018-01-30

Another thing that is important is to "run everything as admin" on your development...visual studio, PDM admin tool, PDM itself...

AC
AC 2018-09-08

Simon, thanks so much!  I stared at this problem for 3 hours today.  Setting the Embed Interop to False & Copy Local to True did the trick for me.

PM
PM 2020-09-14
AT
AT 2020-09-14

Just a caution on tlbimp.exe and 'PDMWorks Enterprise 20nn Type Library' and auto-generated Interop.EdmLib.dll. This does not assign strong name to the dll and you will have nasty compatibility issues if you have other add-ins loaded in the vault referring different version of interop. As a rule of thumb always use PIA (EPDM.Interop.epdm.dll as suggested by Simon) as this is strong-named assmbly and you will not have any compatibility issues.

PM
PM 2020-09-14

Thanks Artem, i updated my post to reflect your opinion in which i agree. Why do you think it is not the options indicated here?

AT
AT 2020-09-14

Hi Paolo. There is no need for Interop.EdmLib, just EPDM.Interop.epdm. The issues related to how .NET Framework works. For unsigned assemblies it will only load one into app domain ignoring versions, so if you have 2 assemblies of different versions it will only load first one. So now an example where conflict could happen. Let’s say you have addin which refers Interop.EdmLib version 2018. All works good. SOLIDWORKS API and PDM API are backwards compatible so the same addin will work in PDM 2020. Now in 2020 you have any other addin which uses Interop.EdmLib 2020 and furthermore is using some API which were not available in 2018 thus not present in Interop.EdmLib 2018. Now you have 2 addins referring different versions of interops. As those are not strong named only one will be loaded. If you are lucky and Interop.EdmLib 2020 is loaded first then both addins will work correctly, but if 2018 loaded first than second addin will crash when newer API is called as loaded Interop does not contain the definition of this API. You can look at this article with video: How to fix the error of SOLIDWORKS add-ins sharing common libraries . It shows this issue with custom dll and SOLIDWORKS addin but it is exact same issue with interops and PDM. Just a summary this behaviour is not PDM or SW specific, rather this is how .NET Framework works. And it is only valid for assemblies in the same app domain such as addins. If you create stand alone you can use Interop.EdmLib and you will not have any issues.

PM
PM 2020-09-14