I have a method in Rubberduck (an in-process VBE add-in) that is assigning a Picture and Mask to a CommandBarControl
(Button). It works well when adding images to the VBE's of Excel, Word, etc., but it fails with an Access Violation when adding images to the SolidWorks VBE.
public static void SetButtonImage(CommandBarButton button, Image image, Image mask)
{
button.FaceId = 0;
if (image == null || mask == null)
{
return;
}
try
{
button.Picture = AxHostConverter.ImageToPictureDisp(image);
button.Mask = AxHostConverter.ImageToPictureDisp(mask);
}
catch (COMException exception)
{
Logger.Debug("Button image could not be set for button [" + button.Caption + "]\n" + exception);
}
}
I know from KB286460 - How To Set the Mask and Picture Properties for Office XP CommandBars
The Mask and Picture properties are defined as type IPictureDisp, which is a member of the Stdole library. IPictureDisp uses methods that cannot be marshalled across process boundaries. Therefore, the Mask and Picture properties can only be called in-process (VBA macros, Automation Add-ins, and ActiveX DLLs run in-process).
I know from How to handle AccessViolationException that I could decorate the method with an HandleProcessCorruptedStateExceptions
attribute, but I don't think I want to carry on with a Corrupted State. Maybe this corrupted State is benign, and I can carry on?
Interestingly, Visual Studio's Autos window handles the Picture
access exception without issue:
It seems that the SolidWorks VBE is somehow not treating/hosting Rubberduck as being in the same process as the CommandBarButtons. That suggests that other VBE hosts might do the same.
EDIT I'm unsure if it is relevant, but I'm running an older version of SolidWorks 2006 under Windows 10, and in order to make it run, I have configured it to run with Administrative privileges.
I know that accessing the Picture
/Mask
properties will cause an AccessViolation, but how can I determine if the property is readable/writable before actually trying to access it?
I could check if the host application is SolidWorks, but I'd rather a more generic approach that works for any host that has the same behavior.