Can a loaded DLL know where it is located? - vb.net

I am building a DLL that is used by Office. When Office runs with it, I would like to identify where it is located. Is that possible?
ex. of code within the DLL when it is run within Office:
// should return C:\tmp\officeaddin.dll,
// currently C:\Program Files\Microsoft Office\Office 12
MessageBox.Show(Application.StartupPath)

The following should work even if running inside of Office:
Getting the path of the current assembly
(It basically boils down to Assembly.GetExecutingAssembly().Location, but see the link above for more detailed information.)

If it is a .NET library, you should use Assembly.GetExecutingAssembly().Location.
Application.StartupPath shows the path to the main app.

This works for me:
Environment.CurrentDirectory
So you could just set a string to it such as:
Dim location as String = Environment.CurrentDirectory & "\"

When the DLL loads, It calls DllMain with the instance handle. If you implement this function, you can then record the instance handle of the DLL. From this, you can then call GetModuleFileName.
In .NET this is taken care of for you. See this question and selected answer for details.

Related

vb.net NAudio.dll - Type 'AudioFileReader' is not defined

(Visual Studio 2019)
It seems I have run into a snag.
My sample code is this.
Imports NAudio.Wave
Partial Class _Default
Inherits System.Web.UI.Page
Dim filename As String
filename = "G:\VS_Lessons\Media\Read\01. Got to Choose.mp3
Dim reader As New AudioFileReader(filename)
Dim duration As String = reader.TotalTime.ToString("mm\:ss")
reader.Dispose()
Label1.Text = duration
End Sub
End Class
This only happens when I am running the site LIVE and NOT from VS.
Running it through VS, it works as it supposed to.
Running it from a live URL it gives the following error.
Type 'AudioFileReader' is not defined
On this line
Dim reader As New AudioFileReader(filename)
Now, I tried to register the NAudio.dll using. (The NAudio.dll file is in the BIN folder of the website)
gacutil -i NAudio.dll
However, I get the following line
Failure adding assembly to the cache: Attempt to install an assembly without a strong name
I followed the steps in this article here
How to add Strong Name to an existing DLL and install to the GAC
But I get a FAILURE when trying to create the DLL file on STEP 5. (Maybe I am doing it wrong because I am not the owner of the file. Not really sure, this is the first time I have ever done anything like this and was trying everything I could find online)
Any assistance on this would be mighty grateful.
Wayne
The website has to be converted into an Application.
IIS | Web Site |
Right-click on the web site name or sub-folder name
And choose [Convert to Application]
Run the script, and it works.
Now, I have to do this on the live site, but first, I will do the local site, as it runs the same script, and I will get all the bugs out of it, then I can do the LIVE IIS site.
It Could Be Due To Partial Class Instead Of Public Class. Or You Haven't Downloaded Genuine DLL Version Of NAudio.
If Its Problem 1 ( Due To Use Of Partial Class ) Then Change To Public Class
If Its Problem 2 ( Use Of Non-Genuine NAudio DLL ) Then Download NAudio From Here
If Problem Still Persists, Then It Would Be For Windows Form Application.

VBScript can't find object in .NET dll [duplicate]

I wrote a DLL in .NET and I want to access it in VBScript. I don't want to add it to the assembly directory.
Is there a way to point too the DLL and create an instance of it?
I just had to do this myself, my findings were:
Making types visible to COM:
Ensure your class is public, non-static and has a public default constructor i.e. not arguments.
Ensure your method is public, non-static.
Ensure you have the following set on your assembly - typically in AssemblyInfo.cs
[assembly: ComVisible(true)]
After building your DLL, from SDK command line run:
regasm yourdll.dll
This should respond:
Types registered successfully
If you get
RegAsm: warning RA0000: No types were registered
then you need to set ComVisible or have no public, non-static types.
From PowerShell
$a = New-Object -comobject Your.Utils.Logging
$a.WriteError2("Application", "hello",1,1)
From vbs
Set logger = CreateObject("Your.Utils.Logging")
logger.WriteError2 "Application", "hello from vbs",1,1
huseyint's answer was on the money, however, I wanted to add a little to it. Here is some sample code I used for this very issue, perhaps it can speed you along...
// bind a variabe to WScript.Shell
Set WshShell = CreateObject("WScript.Shell")
// define the path to the regasm.exe file
RegAsmPath = "c:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe"
// register the dll
WshShell.run "cmd /c " & RegAsmPath & " c:\temp\cbsecurity.dll /codebase /nologo /s", 0, True
// bind a variable to the dll
Set cbUtil = CreateObject("CBSecurity.Utilities")
I had included an IsAlive method in the dll...
Public Function IsAlive() As Boolean
Return True
End Function
...and could check that it registered correctly using the syntax:
//check if dll is available to your code
msgbox "cbUtil is alive: " & cbUtil.IsAlive
Hope this helps someone...
You can register that .NET dll with regasm utility by specifying /codebase parameter. This parameter is not encouraged to use with unsigned assemblies but it works when you can not put your assembly into GAC.
regasm your.dll /codebase
Please note that you should not change your .dll's path after this operation since it inserts this path into the Windows registry.
In case someone needs to debug/step-into the .Net dll that's called from VBScript only:
On the .Net dll project debug setup screen, set the "start external program" by browsing to the wscript.exe program (located in C:\WINDOWS\system32\wscript.exe).
On the "Command Line Arguments", set the file name and path location of the VBScript file (C:\Test\myTest.vbs). Make sure the vbs file and dll file are in the same location.
Finally, in the .Net project DLL source code just set the break point and hit the "start debug"
Not directly. You'll need a COM Callable Wrapper to any .NET library you'll calling from COM (and hence, VBScript). Therefore, you should either directly create a CCW to the DLL or you can create a CCW for a proxy DLL which provides generic methods to load a .NET DLL and provide methods for you that call the actual methods on the component and return the result. It's really not clean at all. So, in general, the answer is no.

File or assembly name ****.dll or one its dependenies not found

I have added a new service reference to a vb.net dll after excluding the previous one. While debugging, When I am sending a request through a form, the new reference is not able to create a object.exception is at this line:
Dim ObjPricingService As New PricerWebService.MedicalProductService_V2_0
MedicalProductService_V2_0 is the new service reference. The exception is:
File or assembly name ****.dll or one of its dependencies not found.
Source:mscorlib
the name of the *.dll changes every time I debug..so its a random one. the code is built in Visual Studio 2003.
Please provide a solution.
It sounds strange, that the Library changes the name every time you debug. The best thing todo, is to try to set an standard name (if you wrote the library). Also you can try to
Import PricerWebService
and then to use only
Dim ObjPricingService As New MedicalProductService_V2_0
If this will be not fix your problem, try to upgrade your framework + Visual Studio Version (VS2003 what?).
BTW: It would be great if you can send us the error text ;)

System.IO.Directory.Getfiles Silverlight 4 is not working

I'm using Silverlight 4 OOB & elevated trust.
I need to get all the filenames on specific directory & populate an object List(Of String)
The compiler throws an error "Method not found" on .Getfiles() method.
Dim files() As String = System.IO.Directory.Getfiles(Path) 'this line is failing..
Help!
The GetFiles is marked as "Security Critical" and therefore cannot be used from your code.
You will want to use the EnumerateFiles method instead. GetFiles is sooo .NET 1.0, EnumerateFiles is much slicker, even in the full framework you'd want avoid this older Array returning API if you can.
As far as I know you cannot directly access the whole hard drive using Silverlight OOB.
Quoting from Silverlight site:
When running in a trusted environment, you can access only files in
user folders, specifically the MyDocuments, MyMusic, MyPictures, and
MyVideos folders. Although this makes sense from a security point of
view, it’s limiting. You want to enable the user to drag their data
from any location. As it stands right now, if you try to drop a file
from a location other than stated above, Silverlight will throw a
security error.
Please refer to this link for details on how to work with the hard drive using Silverlight OOB:
http://www.silverlight.net/learn/overview/out-of-browser-applications/advanced-silverlight-out-of-browser-introduction#Exercise3

VB check inside a folder to see if there are files

I am trying to get my vb.net application to look inside a folder on the web server and then let me know whether there are files in there or if the folder is empty...Would anybody know where i would begin? Thanks
Use the DirectoryInfo.EnumerateFiles() method.
Dim myDir as DirectoryInfo = new DirectoryInfo(pathToDir)
If (myDir.EnumerateFiles().Any())) Then
' Got files in direcotry!
End If
If you are also interested in finding out if there are directories within this one, there is also DirectoryInfo.EnumerateDirectories().
I would suggest you have a look at Directory.GetFiles
If your program is running on the web server, you can simply check whether Directory.GetFileSystemEntries(path) returns anything.
If your program is running on a client, you'll need to make a server-side script that calls Directory.GetFileSystemEntries and returns a value to the client.