Cast is invalid on object

0

I have this problem, when I try to cast the "batch" object, I am raised exception "Invalid cast". Can you help me out with why? The vault5 object exists and is correct.

public void batch(IEdmVault5 vault5)
{
    edmVault11 = (IEdmVault11)vault5;
    IEdmBatchRefVars batch = default(IEdmBatchRefVars);
    batch = (IEdmBatchRefVars)edmVault11.CreateUtility(EdmUtility.EdmUtil_BatchRefVars);

    //some code
}

Edit 1: Error message:

System.InvalidCastException HResult=0x80004002 Messaggio=Cast specificato non valido

c#
solidworksapi
solidworkspdmapi
asked on Stack Overflow Feb 16, 2020 by koss • edited Aug 6, 2020 by Amen Jlili

2 Answers

0

I ran into this today. The solution was to initialize COM for the current thread in STA mode.

  • For a WinForms app, this is already done for the main/UI thread (so that probably does not apply).
  • For a console app, consider adding [STAThread] to your Main() method.
  • If you're running this on background threads or tasks, you'll need to manipulate the thread state directly.
answered on Stack Overflow Oct 5, 2020 by tcostin
-2

I would suggest you try out this change , also

 public void batch(IEdmVault5 vault5)
{
    IEdmVault7 edmValult11 = null;
    if (vault5 == null)
    {
           vault5 = new EdmVault5();
    }
    edmVault11 = (IEdmVault11)vault5;
    IEdmBatchRefVars batch = default(IEdmBatchRefVars);
    batch = (IEdmBatchRefVars)edmVault11.CreateUtility(EdmUtility.EdmUtil_BatchRefVars);

    //some code
}

Besides given the nature of Solidworks API make sure that the installation and license are valid .

answered on Stack Overflow Feb 16, 2020 by Atmanirbhar Bharat • edited Feb 16, 2020 by Atmanirbhar Bharat

User contributions licensed under CC BY-SA 3.0