COM DLL method using referenced enumeration - vb.net

I've created a VB.NET application in VS2015 using x86 compiler and .net target framework 4.5.2
I created a RCW for a COM dll written in VB6 using tlbimp, called "NETLib.dll" and added this interop as a reference to my project.
This COM dll holds the following method:
Function DoSomething(sData As String, iData As Integer, eData As RefLib.enumDat) As Integer
within the class claSomeClass.
My issue: As soon as I use this method in my VB.NET project like the following
Public Class MyClass
Private oNetLibClass As NetLib.claSomeClass
Sub New()
oNetLibClass = New NetLib.claSomeClass
Dim eVal = RefLib.enumDat.Foo
Dim lReturn = oNetLibClass.DoSomething("DoIt", 1, eVal)
End Sub
End Class
I get an error:
BC30652: Reference required to assembly 'RefLib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null' containing the type 'enumDat'.
Add one to your project.
for the line Dim lReturn = oNetLibClass.DoSomething("DoIt", 1, eVal) even though I added RefLib (which also is a COM dll) as an interop to my VB.NET project.
Why do I get this error?
P.S: The line Dim eVal = RefLib.enumDat.Foo is not throwing any error, so I assumed the reference was added correctly...

Related

VS2019 class constructor with different signatures requires all signatures types be included as references by calling program

This has been reported via VS2019 feedback mechanism, but I need a solution quicker. Code snippets are below. Apologies for not knowing how to format this better.
Library class has three constructors, the calling test program in same solution which is .NET V4, is required to have a reference to System.Web when it is not needed. When System.Web is referenced and imported, problem occurs that VS2019 V16.6 thinks Newtonsoft.Json V9.0.1 is being referenced. This is on the critical path for a WS2008R2 (VS2010) to WS2019 (VS2019) migration. Problem not seen on WS2012 or WS2016, both with VS2010.
Severity Code Description Project File Line Suppression State
Warning The primary reference “C:\Users\grollman\Documents\Visual Studio 2019\Projects\WriteToQB\WriteToQB\bin\Debug\WriteToQB.dll” could not be resolved because it has an indirect dependency on the assembly “Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed” which was built against the “.NETFramework,Version=v4.5” framework. This is a higher version than the currently targeted framework “.NETFramework,Version=v4.0”. TestWriteToQB C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2081
Library class
Public Sub New()
m_Page = Nothing
dicWinFormsCache = New Dictionary(Of String, String)(StringComparer.OrdinalIgnoreCase)
InitCRCTable()
End Sub
Public Sub New(objPage As System.Web.UI.Page)
m_Page = objPage
InitCRCTable()
End Sub
Public Sub New(dicIn As Dictionary(Of String, String))
dicWinFormsCache = dicIn
InitCRCTable()
End Sub
calling test program error statement:
Severity Code Description Project File Line Suppression State
Error BC30652 Reference required to assembly ‘System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ containing the type ‘Page’. Add one to your project. WriteToQB C:\Users\grollman\Documents\Visual Studio 2019\Projects\WriteToQB\WriteToQB\clsWriteToQB.vb 25 Active
calling test program
Dim m_objclsDataFunctions As clsDataFunctions.clsDataFunctions
Public dicSettings As New Dictionary(Of String, String)
Private ReadOnly Property objclsDataFunctions As clsDataFunctions.clsDataFunctions
Get
If IsNothing(m_objclsDataFunctions) Then
'Dim applicationCache As System.Web.Caching.Cache = System.Web.HttpRuntime.Cache
m_objclsDataFunctions = New clsDataFunctions.clsDataFunctions(dicSettings)
End If
Return m_objclsDataFunctions
End Get
End Property

VS 2017: Reference required to assembly

It seems, that in VS 2017, a project using an overloaded method, needs to have references to all the types of all the overloaded methods, even if it uses just one of these overloaded methods.
For example, consider ClassLibrary1 with the classes ServicedComponent1 and SharedMethods:
Public Class ServicedComponent1
Inherits EnterpriseServices.ServicedComponent
End Class
Public Class SharedMethods
Public Shared Sub DoDispose(ByRef obj As Messaging.Message)
obj.Dispose()
End Sub
Public Shared Sub DoDispose(ByRef obj As EnterpriseServices.ServicedComponent)
obj.Dispose()
End Sub
End Class
In ConsoleApp1, which references ClassLibrary1 and System.EnterpriseServices, I have the following Module:
Module Module1
Sub Main()
Dim obj1 = New ClassLibrary1.ServicedComponent1
ClassLibrary1.SharedMethods.DoDispose(obj1)
End Sub
End Module
When I try to compile ClassLibrary1, I get the following error:
1>C:\temp\TestReferences\ConsoleApp1\Module1.vb(5,9): error BC30652: Reference required to assembly 'System.Messaging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' containing the type 'Message'. Add one to your project.
In VS 2010, ConsoleApp1 compiles successfully, because I do not use the first overloaded method that uses System.Messaging.Message. I only use System.EnterpriseServices.ServicedComponent for which I have a reference in ConsoleApp1. It seems, that VS 2010 is smarter than VS 2017, because it finds out that the reference to System.Messaging is not needed.
Why does VS 2017 require a reference to System.Messaging?
Is there an option, so that VS 2017 behaves in this respect as VS 2010?

VB.net Class Not Registered Error

I'm in the process of writing a VB application but whenever I run the application I get the error listed below:
"An error occurred creating the form. See Exception.InnerException for details. The error is: Retrieving the COM class factory for component with CLSID {11219420-1768-11D1-95BE-00609797EA4F} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."
I have tried registering the shell32.dll file, I have looked in the registry for the entry {11219420-1768-11D1-95BE-00609797EA4F} but it does not exist, and I have tried compiling this application for x86.
Imports Shell32
Imports System.IO
Public Class frmIconChanger
Dim sh As ShellLinkObject = New ShellLinkObject
Private Sub btnBackupAndChange_Click(sender As Object, e As EventArgs) Handles btnBackupAndChange.Click
For Each desktopIcon In My.Computer.FileSystem.GetFiles("C:\Users\" + getUserName().ToString + "\Desktop")
Dim fileExtension As String = Path.GetExtension(desktopIcon)
If (fileExtension = ".lnk") Then
MsgBox(sh.GetIconLocation(desktopIcon).ToString)
End If
Next
End Sub
Private Function getUserName() As String
Return System.Environment.UserName.Trim
End Function
End Class
Other information: I am on a Windows 7 64-bit machine. I have .Net 4.0 and previous versions and am working in Visual Studio 2012. Also I have shell32.dll added as a reference in my project. Any help resolving this issue would be much appreciated.
Thanks.
Refer to this link Creating Links. Take a look at the "C" code and look for constants
CLSID_ShellLink
IID_IShellLink
Those are the values you need for the class and interface.
This other link is useful: IShellLink interface
This should be the GUIDs you need:
CLSID := "{00021401-0000-0000-C000-000000000046}"
IID := "{000214F9-0000-0000-C000-000000000046}"

How to enable early binding of VBA object variable to COM classes created in .NET

I seem to be having trouble getting my COM class library (.NET-based) to support early binding.
I am creating a class library in VB.NET 2003 for use in Office 2003 VBA (and later for Office 2010). Documentation # StackOverflow and elsewhere has brought me to this bit of code:
Imports System.Runtime.InteropServices
<InterfaceType(ComInterfaceType.InterfaceIsDual), _
ComVisible(True), _
Guid("<some valid GUID>")> _
Public Interface _TestCOMClass
Function Test() As String
End Interface
<ClassInterface(ClassInterfaceType.None), _
ComVisible(True), _
Guid("<another valid GUID>"), _
ProgId("TestCOMDLL.TestCOMClass")> _
Public Class TestCOMClass
Implements _TestCOMClass
Public Function Test() As String Implements _TestCOMClass.Test
Return "Test value"
End Function
End Class
The solution is set to compile with COM Interop. It builds succesfully and the ProgID then appears in the References list in VBA.
I use this in VBA by setting the appropriate reference and then declaring variables of the appropriate type and instantiating my class. Here is where it gets mysterious.
Dim EarlyBird As TestCOMDLL.TestCOMClass
Dim LateBird As Object
Set EarlyBird = New TestCOMDLL.TestCOMClass
' This is my preferred instantiation method, but results in a
' "Does not support Automation or expected interface" error
Set EarlyBird = CreateObject("TestCOMDLL.TestCOMClass")
' This is my 2nd best instantiation method,
' but results in a Type Mismatch error
Set LateBird = CreateObject("TestCOMDLL.TestCOMClass")
MsgBox LateBird.Test
' This works, but has all the disadvantages of late binding
So I can reference my library, declare object variables of the appropriate type, and instantiate my class, but I cannot assign the instantiated object reference to my typed variable, only to a variable of type Object. Also, instantiating the New keyword appears to be supported (Intellisense offers the library and class as options), but fails at runtime.
What is lacking in my VB.NET code or build settings that keeps early binding from working?
PS: Another way of putting my problem is that I've tried the solution in this StackOverflow thread and found that what AnthonyWJones says
You can also reference the dll and use early binding:
is not true in my case... :-(

Typecast exception from ShellBrowserWindow object to ShellFolderView object

Looking for help trying to figure out why this typecast is not working on my machine.
This code was provided as an answer to another question I had and it's not working for me. It works for the answer poster on their machine, but I'm get a an exception on the line trying to typecast from ShellBrowserWindow to ShellFolderView.
I am using Visual Studio Express 2013, running on Windows 7 Pro X64 Sp1. The target framework for the project is .Net Framework 4. I've added references to Microsoft Internet Controls and Microsoft Shell Controls and Automation and I've added the Imports statements for Shell32 and SHDocVw. DLL versions are as follows: shell32.dll = 6.1.7601.18517 and shdocvw.dll = 6.1.7601.1822 I'm not sure what I could be missing.
The code looks like this. (This code is in a form object)
Imports EdmLib
Imports Shell32
Imports SHDocVw
Public Class BlankForm
Private Sub BlankForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim bar As String() = GetExplorerSelectedFiles()
Exit Sub
'The rest of my program is below this line - I'm just trying to test this one function right now...
End Sub
'Uses the windows shell to get the files selected in explorer
Public Function GetExplorerSelectedFiles() As String()
Dim ExplorerFiles As New List(Of String)
Dim exShell As New Shell32.Shell
Dim SFV As Shell32.ShellFolderView
For Each window As SHDocVw.ShellBrowserWindow In DirectCast(exShell.Windows, SHDocVw.IShellWindows)
If (window.Document).GetType.Name <> "HTMLDocumentClass" Then
SFV = CType(window.Document, ShellFolderView) '<--This is where it fails!!
For Each fi As FolderItem In SFV.SelectedItems
ExplorerFiles.Add(fi.Path)
Next
End If
Next
Return ExplorerFiles.ToArray
End Function
End Class
The line SFV = CType(window.Document, ShellFolderView) results in the following message:
An unhandled exception of type 'System.InvalidCastException' occurred
in LaunchTemplateEPDM.exe
Additional information: Unable to cast COM object of type
'System.__ComObject' to interface type 'Shell32.ShellFolderView'. This
operation failed because the QueryInterface call on the COM component
for the interface with IID '{29EC8E6C-46D3-411F-BAAA-611A6C9CAC66}'
failed due to the following error: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
I've taken a screenshot of a quickwatch on the window object. A quickwatch on the window.document object shows an error saying it's either undefined or inaccessible.
I ran the query Microsoft.VisualBasic.Information.TypeName(window.document) and it returns "IShellFolderViewDual3".
I fixed it.
Not sure why this happens on my system and not yours.
What I found was that GetType.Name always just returns "System.__ComObject", regardless of whether the object is of type ShellFolderView, HTMLDocumentClass or something else. So what was happening was no matter what the actual type of the object was, it was passing the <>"HTMLDocumentClass" test because it was always evaluating to "System.__ComObject". Then when we tried to run the CType function on an object that didn't implement the ShellFolderView interface, it would throw that exception.
I eventually stumbled upon this article which led me to experiment with the TypeName Function which seems to return the actual type, and so I ended up with the working code below:
Public Function GetExplorerSelectedFiles() As String()
Dim ExplorerFiles As New List(Of String)
Dim exShell As New Shell32.Shell
For Each window As SHDocVw.ShellBrowserWindow In DirectCast(exShell.Windows, SHDocVw.IShellWindows)
If TypeName(window.Document) Like "IShellFolderViewDual*" Then
For Each fi As FolderItem In DirectCast(window.Document, ShellFolderView).SelectedItems
ExplorerFiles.Add(fi.Path)
Next
End If
Next
Return ExplorerFiles.ToArray
End Function