ActiveX Assembly ReflectionTypeLoadException - vb.net

I created an activeX exe (registered with regasm.exe) that loads a DLL with
Dim a As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(FullPath)
This DLL works with an Interface "PSInterface". As normal exe it works, but when i call that activex object i am getting a ReflectionTypeLoadException in a.GetTypes. When i look into that error i get the following:
Could not load file or assembly 'PSInterface, Version=1.0.0.0, Culture = neutral, PublicKeyToken=null' or one of its dependencies. The System cannot find the file specified.
The PSInterface.dll is in the same folder as the exe and dll.
What can i do?

To load the Assemblies from the same folder, I set AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf LoadFromSameFolder and the Function
Private Shared Function LoadFromSameFolder(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
Dim folderPath As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
Dim assemblyPath As String = Path.Combine(folderPath, New AssemblyName(args.Name).Name & ".dll")
If Not IO.File.Exists(assemblyPath) Then
assemblyPath = Path.Combine(folderPath, New AssemblyName(args.Name).Name & ".exe")
If Not IO.File.Exists(assemblyPath) Then
Return Nothing
End If
End If
Dim assembly As Assembly = Assembly.LoadFrom(assemblyPath)
Return assembly
End Function
Thanks #Craig for your help.
More Here: How to add folder to assembly search path at runtime in .NET?

Related

Check folder is open or not

how should i make this go open folder only if only folder it is not open? the folder should only open if it is not open. and put an if, and an else.
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Dim folderpath As String
Dim foldername As String
'Process.Start(System.Environment.CurrentDirectory)
folderpath = My.Application.Info.DirectoryPath + ("\Check")
foldername = System.IO.Path.GetFileName(folderpath)
If FindWindow(vbNullString, foldername) = 0 Then
Process.Start("explorer.exe", folderpath)
End If
Windows has a function built in to do this. https://learn.microsoft.com/en-us/windows/win32/shell/ishelldispatch-windows and look at the windows property.
When Internet Explorer 4 Desktop Update was released there was no difference between local files and internet files. Therefore it lists Internet Explorer (but no other browser) and Windows Explorer windows.
The win in AllWindows is actually an Internet Explorer object - see https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752084(v=vs.85)
'ListOpenShellWindows.vb
Imports System.Runtime.InteropServices
Public Module MyApplication
Sub Main()
Dim ObjShell as Object
Dim AllWindows as Object
objShell = CreateObject("Shell.Application")
AllWindows = objShell.Windows
For Each win in AllWindows
Msgbox(win.LocationUrl & " - " & win.LocationName)
Next
End Sub
End Module
To compile copy both files into same folder and double click the batch file.
REM ListOpenShellWindows.bat
REM This file compiles ListOpenShellWindows.vb to ListOpenShellWindows.exe using the system VB.NET compiler
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\ListOpenShellWindows.exe" "%~dp0\ListOpenShellWindows.vb"
pause
Edit
If interacting with the Shell it is best to use the Shell functions. You use ObjShell.Open to open a folder. See https://learn.microsoft.com/en-us/windows/win32/shell/ishelldispatch-open.
If the goal is to not open multiple explorer instances for the same directory, then you can simply pass a new ProcessStartInfo object to the Process.Start(...) function. Assign the directory path to the ProcessStartInfo.FileName property and the "open" command to the ProcessStartInfo.Verb property. This way, an already open instance will be activated rather than opening a new one for the same dir.
' Some caller...
Dim dirInfo = New DirectoryInfo(Path.Combine(My.Application.Info.DirectoryPath, "Check"))
Dim psi As New ProcessStartInfo With {
.FileName = dirInfo.FullName,
.Verb = "open"
}
Process.Start(psi)
On the other hand, if you still need to find out whether a directory is already open in the explorer, then you could pinvoke the FindWindowByCaption function which returns a handle to a window if any.
Dim dirInfo = New DirectoryInfo(Path.Combine(My.Application.Info.DirectoryPath, "Check"))
Dim p = FindWindowByCaption(IntPtr.Zero, dirInfo.Name)
If p = IntPtr.Zero Then
Process.Start(dirInfo.FullName)
Else
Console.WriteLine("Already Open!")
End If
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function FindWindowByCaption(zero As IntPtr, lpWindowName As String) As IntPtr
End Function
Of course, the target directory should exist in the first place. Just in case, see the DirectoryInfo.Exists property and the DirectoryInfo.Create method.

I am getting error to use C DLL in VB.net

I am getting error “System.DllNotFoundException: 'Unable to load DLL 'swedll32.dll': Invalid access to memory location. (Exception from HRESULT: 0x800703E6)'” while Button1.Click. That dll is a native or unmanaged dll written in C. I am using 64 bit Windows 10 and visual studio 2019.
Please help. Thanks
Public Class Form1
Public Declare Sub swe_set_ephe_path Lib "swedll32.dll" Alias "_swe_set_ephe_path#4" (ByVal path As String)
Public Shared Function swe_get_planet_name(ByVal ipl As Int32) As String
Dim Pointer As IntPtr
Dim ReturnParam As New StringBuilder(255)
Dim Result As String
Pointer = _swe_get_planet_name(ipl, ReturnParam)
Result = Marshal.PtrToStringAnsi(Pointer)
Return Result
End Function
Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
swe_set_ephe_path(System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\SwEph")
End Sub
End Class

Cannot find the interlop type that matches the embedded type ' ' are you missing an assembly reference... ERROR

here is my code:
Imports System.Windows.Forms
Imports Microsoft.Office.Tools.Word
Imports Microsoft.Office.Interop.Word
Public Class Form1
'Imports Microsoft.Office.Interop.Word
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim openfile As OpenFileDialog = New OpenFileDialog()
If openfile.ShowDialog() Then
Dim file As String = openfile.FileName
Dim outfile As String = "C:\\file.pdf"
Dim extension As String = System.IO.Path.GetExtension(file)
If extension = ".doc" Or extension = ".docx" Then
Dim app As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
Dim doc As Microsoft.Office.Tools.Word.Document = Nothing
doc = app.Documents.Open(file, Type.Missing, False)
doc.ExportAsFixedFormat(outfile, Word.WdExportFormat.wdExportFormatPDF)
doc.Close(False, Type.Missing, Type.Missing)
app.Quit(False, False, False)
System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
End If
End If
End Sub
End Class
i am trying to make a program which had the adobe pdf reader, you open a word file and it converts to pdf and shows it in the reader.
But i am getting this error: Cannot find the interlop type that matches the embedded type ' ' are you missing an assembly reference... ERROR. I have put embed interlop type to false. I am not sure what to do. Please help.
Thanks in advance.
i researched a lot but i couldnt find anything so then I came here.

How to create a multilingual project

I'm new to VB.net and I'm trying to do a simple multilingual project.
So far I've created 2 resource files:
en-US.resx
pt-PT.resx
in both of them I have the same ID's and diferent values (strings only)
(these strings will be used across multiple forms)
When I change the laguage I do:
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
or
Thread.CurrentThread.CurrentCulture = New CultureInfo("pt-PT")
Based on the language I want to see.
But I dont know how to access my resource files properly, Doing:
Dim assembly As System.Reflection.Assembly
assembly = Me.GetType.Assembly
Dim resourceManager As New System.Resources.ResourceManager("My.Resources", assembly)
MsgBox(resourceManager.GetString("TEST"))
Gives me an exception System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
What am I missing?
edit after the first sugestion:
This example requires the text-based resource files listed in following table. Each has a single string resource named DateStart.
Culture | File name | Resource name | Resource value
en-US DateStrings.txt DateStart Today is
pt-PT DateStrings.pt-PT.txt DateStart hoje é
This code uses the GetString(String, CultureInfo) method to retrieve culture-specific resources. The example's default culture is English (en), and it includes satellite assemblies for the Portuguese (Portugal) (pt-PT) culture.
Module Example
Public Sub Main()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("pt-PT")
Dim cultureNames() As String = { "en-US", "pt-PT" }
Dim rm As New ResourceManager("DateStrings",GetType(Example).Assembly)
'Access to resource file
For Each cultureName In cultureNames
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
Dim dateString As String = rm.GetString("DateStart", culture)
Console.WriteLine("{0}: {1} {2}.", culture.DisplayName, dateString,
Date.Now.ToString("M", culture))
Console.WriteLine()
Next
End Sub
End Module
Well I just stop using the resource manager. Apparently the resource manager is not needed and now its working. I hope this helps someone else when the tutorials seem to fail :\
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US")
MsgBox(My.Resources.MStrings.TEST)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("pt-PT")
MsgBox(My.Resources.MStrings.TEST)
End Sub
End Class

VB.NET / C# code to access target path of link (lnk) files produces some wrong paths

I found this code:
Public Shared Function GetLnkTarget(ByVal lnkPath As String) As String
Dim shl = New Shell32.Shell()
' Move this to class scope
lnkPath = System.IO.Path.GetFullPath(lnkPath)
Dim dir = shl.[NameSpace](System.IO.Path.GetDirectoryName(lnkPath))
Dim itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath))
Dim lnk = DirectCast(itm.GetLink, Shell32.ShellLinkObject)
Return lnk.Target.Path
End Function
It works for some .lnk files, but for example if I add my Skype.exe desktop link it produces:
C:\Windows\Installer\{...}\SkypeIcon.exe
Is there a fix for this?
Try this:
Function GetTargetPath(ByVal FileName As String)
Dim Obj As Object
Obj = CreateObject("WScript.Shell")
Dim Shortcut As Object
Shortcut = Obj.CreateShortcut(FileName)
GetTargetPath = Shortcut.TargetPath
End Function
Private Sub Teste_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(GetTargetPath("C:\ProgramData\Microsoft\Windows\Start Menu\BitTorrent.lnk"))
'here you chose the location of .lnk file
End Sub
CreateShortcut() doesn't work as expected with certain shortcuts that have a greyed out target in the properties, like Adobe Reader and Microsoft Word. The targetpath ends up being something under c:\windows\installer (icon?).