none
Issue accessing Class Library Projects from VSTO RRS feed

  • Question

  • The solution has two projects VSTO for Excel (written in VB.NET) and a class Library project written in C#. The entry point is the VSTO project. I have created a ribbon (designer based). When I click the Icon Botton, the event should call the instance of a class library project. Unfortunately, the no action is taking place upon clicking the button. 

    Below is the overview of the VSTO code in the ribbon class

    Imports ReadInput
    Imports Microsoft.Office.Tools.Ribbon
    
    Public Class Ribbon1
         Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    End Sub
    
      Private Sub NasH5_Click(sender As Object, e As RibbonControlEventArgs) Handles NasH5.Click
          Dim testread As readfiles = New readfiles()
          testread.Readfls()
    
      End Sub
    End Class

    Below is the class library code written in C#

    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace ReadInput
      {
      public class readfiles
        {
    
        public void Readfls()
          {
           ...
           }
        }
      }

    I even tried with Any CPU, x64, x86 configurations. Can someone suggest me what is the way forward.

    I get the following errors 

    Exception thrown: 'System.BadImageFormatException' in Microsoft.Office.Tools.Common.Implementation.dll
    Exception thrown: 'System.Reflection.TargetInvocationException' in Microsoft.Office.Tools.Common.Implementation.dll

    BR,

    Raghu





    • Edited by Raghu_IITM Wednesday, July 4, 2018 4:49 AM
    Tuesday, July 3, 2018 10:15 PM

All replies

  • Hello Raghu_IITM,

    What's the h5read? Why did you set a readfiles object as a new h5read?

    Is there any other design in your class library which may cause the issue?

    Could you access your class library project from some other project? For instance, a simple winform project?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 4, 2018 2:17 AM
  • Hi Terry, 

    It was a typo in my post. The corrected statement is below.

    Dim testread As readfiles = New readfiles()

    The code works fine when I run the projects as a simple windows form. But, it fails when I try access it from VSTO project. The class library has a dll file targeting .Net2.0 and the VSTO is targetting .Net4.6.1. Not sure if that is the issue because it worked under windows form targeting .Net4.6.1. 

    Another strange thing is when I comment the class instantiation and replace with a Message box, the ribbon callback works. I don't know where I am going wrong.

    Imports ReadInput
    Imports Microsoft.Office.Tools.Ribbon
    
    Public Class Ribbon1
         Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    End Sub
    
      Private Sub NasH5_Click(sender As Object, e As RibbonControlEventArgs) Handles NasH5.Click
          'Dim testread As readfiles = New readfiles()
          'testread.Readfls()
          Msgbox("Success!")
    
      End Sub
    End Class

    Thanks,

    Raghu





    • Edited by Raghu_IITM Wednesday, July 4, 2018 8:27 AM
    Wednesday, July 4, 2018 4:42 AM
  • Hello Raghu_IITM,

    Have you check if changing to target framework as 4.6.1 could resolve the issue?

    I would suggest you backup your VSTO and dll project and delete unrelated code and any sensitive information. Zip these two projects and share it to us so we could try to use it to reproduce your issue.

    For sharing file, you could share the file via Cloud Storage, such as One Drive, and then put link address here.

    Thanks for understanding.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 4, 2018 9:32 AM
  • Hi Terry,

    I have removed the sensitive parts of the HDF code. I tried to copy the link here but it is suggesting me to verify my account before posting the images or links. Can you please help me on this. 

    BR,

    Raghu

    Wednesday, July 4, 2018 10:24 AM
  • Hello Raghu,

    You could try to just put the link address here. There is no need to insert a hyperlink. We could copy the address to browser manually.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 4, 2018 10:27 AM
  • Hi Terry,

    Here is the link to One Drive.

    https://1drv.ms/u/s!Au_teUZM-WBlkm9bYdDiS6ZxgVL-

    BR,

    Raghu

    Wednesday, July 4, 2018 10:30 AM
  • Hello Raghu_IITM,

    >>I even tried with Any CPU, x64, x86 configurations. Can someone suggest me what is the way forward.

    I note that the VSTO project's target CPU is AnyCpu and the class library project's target is X64. After changing the class library's target to AnyCpu too, the class library could be initialized for me. Which project's target cpu did you changed?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 5, 2018 6:53 AM
  • Hi Terry,

    I changed the Class library to x64 as I was struggling with Any CPU. Now I changed it back to Any CPU. I could instantiate the class but it is failing inside class code right after the variable declaration with the error

    BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B). 

    It it little complicated and I am a bit lost here. 

    Thanks,

    Raghu


    • Edited by Raghu_IITM Thursday, July 5, 2018 11:58 AM
    Thursday, July 5, 2018 10:51 AM
  • Hello Raghu,

    What's the 32/64 version of your system? What's the result to force both of them as X86 or X64 to run?

    Have you tried to change the dll project's target framework to 4.6.1? Could it work?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 9, 2018 7:22 AM
  • Hi Terry,

    Mine is 64 bit OS. I have tried all the options x64, x86 and Any CPU but of no use. 

    It looks like a problem with accessing some static constants inside the dll. The constants in HDF.PInvoke doesn't seems to support multithreading as you can see from github repository. 

    https://github.com/HDFGroup/HDF.PInvoke/blob/master/HDF5/H5Pglobals.cs

    I raised this with issue with HDF5 Forum but awaiting response. Is Microsoft still supporting HDF.PInvoke repository in github?

    Best Regards,

    Raghu

    Monday, July 9, 2018 7:33 AM
  • Hello Raghu_IITM,

    No. This forum is for discussing issue related to VSTO and HDF is a third-part application.

    I would suggest you back up your dll project and then uninstall the HDF.PInvoke and any other reference to the HDF. Try to use some simple code in ImportNastranh5 and then check if it will cause the error.

    Please try to do the test and share us the result.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 9, 2018 8:01 AM