<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues in Inventor Programming Forum</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865023#M41701</link>
    <description>&lt;P&gt;Thank you, will try and update below!&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jan 2022 19:50:23 GMT</pubDate>
    <dc:creator>mike_ponti</dc:creator>
    <dc:date>2022-01-07T19:50:23Z</dc:date>
    <item>
      <title>IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10863255#M41699</link>
      <description>&lt;P&gt;Hello all, hoping someone has run into this or something similar before. I have been working on an addin, off and on for the last year or so. Yesterday afternoon, everything was fine. I was building, deploying and debugging and everything was fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then, suddenly, none of my UI was visible. I dug in and found out that there was an exception being thrown in the below code.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    ButtonDefinition = commandManager.ControlDefinitions.AddButtonDefinition(
        DisplayName,
        InternalName,
        Classification,
        ClientId,
        Description,
        ToolTipText,
        standardIconPict,
        largeIconPict,
        ButtonDisplay
    );
    // EXCEPTION THROWN ON ABOVE STATEMENT
    // System.Runtime.InteropServices.COMException (0x8000FFFF): Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above code is called from a base class for ButtonCommands so it is used in a lot of places, not just this project. Most of the items are just properties, the exception being the IPictureDisp parameters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suspected that these were the issue so changed the values for the icons to null and now the buttons loaded, but of course had no icons for the buttons.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My normal procedure is to store the png's for the buttons as embedded resources. The base class loads the embedded resource in as a bitmap object and then passes it to the converter to get the IPictureDisp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The converter is probably pretty familiar as it came from a blog post from ex-ADN Phillipe L which is below.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public class PngConverterOle
{
    [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true, PreserveSig = false)]
    private static extern stdole.IPictureDisp OleCreatePictureIndirect([MarshalAs(UnmanagedType.AsAny)] object picdesc, ref Guid iid, [MarshalAs(UnmanagedType.Bool)] bool fOwn);
    
    static Guid iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;

    private static class PICTDESC
    {
        //Picture Types
        public const short PICTYPE_UNINITIALIZED = -1;
        public const short PICTYPE_NONE = 0;
        public const short PICTYPE_BITMAP = 1;
        public const short PICTYPE_METAFILE = 2;
        public const short PICTYPE_ICON = 3;
        public const short PICTYPE_ENHMETAFILE = 4;

        [StructLayout(LayoutKind.Sequential)]
        public class Bitmap
        {
            internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Bitmap));
            internal int picType = PICTDESC.PICTYPE_BITMAP;
            internal IntPtr hbitmap = IntPtr.Zero;
            internal IntPtr hpal = IntPtr.Zero;
            internal int unused;

            internal Bitmap(System.Drawing.Bitmap bitmap)
            {
                this.hbitmap = bitmap.GetHbitmap();
            }
        }
    }

    public static stdole.IPictureDisp ToIPictureDisp(System.Drawing.Bitmap bmp)
    {
        PICTDESC.Bitmap pictBmp = new PICTDESC.Bitmap(bmp);
        var result = PngConverterOle.OleCreatePictureIndirect(pictBmp, ref iPictureDispGuid, false);
        return result;
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The crazy thing is that this code hasn't changed since summer 2021 and the icons haven't changed since before that.&lt;/P&gt;&lt;P&gt;Things I have tried:&lt;BR /&gt;- updated Inventor&lt;BR /&gt;- tried different versions of Autodesk.Inventor.interop&lt;BR /&gt;- tried using AxHost to do the conversion (doesn't work in MTA thread apartment)&lt;BR /&gt;- verified that png's are loading and correct size&lt;BR /&gt;- tried Inventor 2019, 2021 and 2022&lt;/P&gt;&lt;P&gt;- rebooted (desperation)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am going to try and uninstall and re-install the different Inventor versions, but in the meantime, wanted to get this out there in case someone has see this before.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks all!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 01:44:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10863255#M41699</guid>
      <dc:creator>mike_ponti</dc:creator>
      <dc:date>2022-01-07T01:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10864288#M41700</link>
      <description>&lt;P&gt;Try a different framework too!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 14:30:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10864288#M41700</guid>
      <dc:creator>bradeneuropeArthur</dc:creator>
      <dc:date>2022-01-07T14:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865023#M41701</link>
      <description>&lt;P&gt;Thank you, will try and update below!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 19:50:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865023#M41701</guid>
      <dc:creator>mike_ponti</dc:creator>
      <dc:date>2022-01-07T19:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865055#M41702</link>
      <description>&lt;P&gt;I finally finished the uninstalls and updates. Uninstalled 2019, 2021 and 2022 and installed with latest updates 2020, 2021 and 2022. No change in the behavior. Still throws on the AddButtonDefinition with the same catastrophic failure. I was unable to knock it back to an earlier framework as it relies on a library that was built against 4.8.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think at this point, I am going to try and create a brand new add in, no dependencies except for my own inventor helpers library and see what happens.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your suggestion&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/473476"&gt;@bradeneuropeArthur&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 20:04:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865055#M41702</guid>
      <dc:creator>mike_ponti</dc:creator>
      <dc:date>2022-01-07T20:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865062#M41703</link>
      <description>&lt;P&gt;Compiling 64 or 32 bit version?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 20:12:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865062#M41703</guid>
      <dc:creator>bradeneuropeArthur</dc:creator>
      <dc:date>2022-01-07T20:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865207#M41704</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/473476"&gt;@bradeneuropeArthur&lt;/a&gt;&amp;nbsp;, I am compiling "Any CPU". Hasn't been a problem in the past.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I created a new addin and ran it and it setup without any problem. One very interesting observation ... I originally went to using the DllImport of "OleCreatePictureIndirect" because my years of using the AxHost control to do the conversion didn't work in an "Multi-Thread-Apartment" (MTA) process.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had a "Debug.Writeline" statement hanging around that showed what "Thread Model" the addin was running in. Here we can see that when my new test addin is loading, it runs under an STA thread model.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="thread-test-addin.jpg" style="width: 433px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1008756iAAAA37D0AE71EBB2/image-size/large?v=v2&amp;amp;px=999" role="button" title="thread-test-addin.jpg" alt="thread-test-addin.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And now we have the problem addin which shows that it is running under MTA:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="thread-problem-addin.jpg" style="width: 366px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1008757i37C3D344EBDD922E/image-size/large?v=v2&amp;amp;px=999" role="button" title="thread-problem-addin.jpg" alt="thread-problem-addin.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am no COM expert, so just theorizing here. I am guessing that one of my other dependencies is causing the addin to load under an MTA model. How that happens is ... (more than a little) unclear to me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I guess the next step is to try and isolate out these dependencies, my first choice is "MaterialDesignsThemes.Wpf" that I use to help me out with some presentation stuff. It is the first time I have tried using it and had never had any of these issues before.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate all that are following along on this!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 21:45:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865207#M41704</guid>
      <dc:creator>mike_ponti</dc:creator>
      <dc:date>2022-01-07T21:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865428#M41705</link>
      <description>&lt;P&gt;I guess this is turning into a bit of a live blog or something....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My hunch about the STA vs MTA seems to be correct. I have a function in my StandardAddinServer class that checks the app store entitlement endpoint for subscription status. Something in there changes things from STA to MTA. If I comment out the entitlement check, it never switches to MTA and the UI with icons is created.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The entitlement check uses async functions, is this what switches to MTA? Next check will be to make the entitlement check synchronous. Or I can move it into the command execution directly. Making it synchronous will be probably the easiest thing so will try that first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to be continued ...&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 01:09:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865428#M41705</guid>
      <dc:creator>mike_ponti</dc:creator>
      <dc:date>2022-01-08T01:09:49Z</dc:date>
    </item>
    <item>
      <title>Re: IPictureDisp and CommandManager.ControlDefinitions.AddButtonDefinition issues</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865464#M41706</link>
      <description>&lt;P&gt;As usual ... it is all my fault.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I put an async function indirectly into the "StandardAddinServer.Activate". On reflection, can see that wasn't a great idea, I was more focused on getting entitlement figured out and passed to the actual commands.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason why it worked before is because I had stubbed in the entitlement check and when I put the real one in I just automatically used the "HttpClient" which is completely async. If you want non-async HTTP rest calls you need to use HttpWebRequest, HttpWebResponse instead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks all and especially&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/473476"&gt;@bradeneuropeArthur&lt;/a&gt;&amp;nbsp;for your suggestions.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 01:49:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/ipicturedisp-and-commandmanager-controldefinitions/m-p/10865464#M41706</guid>
      <dc:creator>mike_ponti</dc:creator>
      <dc:date>2022-01-08T01:49:14Z</dc:date>
    </item>
  </channel>
</rss>

