|
how to know which procces['s] have access to a file in vb.net?
thnx
|
|
|
|
|
There are no classes or methods in the .NET Framework that will tell you this. This requires knowledge of calling Win32 API functions and how Windows internals work.
|
|
|
|
|
I am trying to load an application into a new domain. I have this c# code that runs fine:
AppDomain newDomain = AppDomain.CreateDomain("newDomain");
FileStream fs = new FileStream("c:\\TestClass.dll", FileMode.Open);
byte[] rawAssembly = new byte[(int)fs.Length];
Console.Write(fs.Length);
fs.Read(rawAssembly, 0, rawAssembly.Length);
Assembly asm = newDomain.Load(rawAssembly, null);
However the translation into vb.net does not:
Dim newDomain As AppDomain = AppDomain.CreateDomain("newDomain")
Dim fs As FileStream = New FileStream("c:\\TestClass.dll", FileMode.Open)
Dim rawAssembly() As Byte = New Byte((CType(fs.Length, Integer)) - 1) {}
fs.Read(rawAssembly, 0, rawAssembly.Length)
Dim asm As Assembly = newDomain.Load(rawAssembly, Nothing)
produces this error on the final line:
Could not load file or assembly 'TestClass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Both versions read the same number of bytes into the filestream.
thanks
|
|
|
|
|
I would have thought the error message made the problem quite clear.
cstrader232 wrote: Dim fs As FileStream = New FileStream("c:\\TestClass.dll", FileMode.Open)
Take out the second backslash in the filepath. In C#, two backslashes are treated as one in the final string. In VB, it's still two backslashes. This is because C# uses "\" as an escape character, whereas VB doesn't.
The declarion of the rawAssembly byte array could also be simplified.
' Beware! The CType call is a truncating conversion.
' Using this technique blindly, wihtout checking the source
' value for fit inside the targets bounds can result in a
' hard to find bug in your code!
Dim rawAssembly(CType(fs.length, Integer) - 1) As Byte
|
|
|
|
|
Thanks Dave, but that isn't the problem
I fixed the double slash to single slash, and rewrote the dim of the assembly().
The stream seems to be read OK at 3200 bytes
newdomain.Load still creates an error however.
Thanks
Dim newDomain As AppDomain = AppDomain.CreateDomain("newDomain")
Dim fs As FileStream = New FileStream("c:\TestClass.dll", FileMode.Open)
Dim rawAssembly(CType(fs.Length, Integer) - 1) As Byte
fs.Read(rawAssembly, 0, fs.Length)
Console.WriteLine(fs.Length)
Dim asm As Assembly
asm = newDomain.Load(rawAssembly, Nothing)
|
|
|
|
|
Where did this code come from? Did it come from an MSDN page? Were you looking at the correct version of the page??
Is TestClass.dll a .NET assembly that you compiled, or a DLL file you copied from somewhere else?
|
|
|
|
|
I created the dll and know exactly what's in it.
I don't think I copied the code -- I just tried it.
I appreciate your attention -- this is killing me. Can you help me with an alternative?
I can easily load the assembly into the main .exe, but I need to load it into a new Appdomain, so that I can unload it, modify it, and reload it.
I can modify the .dll as needed to allow that.
I have struggled for days with code such as this, and read everything I could find about it, all to no avail:
Dim ni As Object = newDomain.CreateInstanceFromAndUnwrap("c:\ClassLibrary1.dll", "Indicators.Indicator")
Any way to do accomplish this would be appreciated.
thanks!
Here is the .dll
Imports System
Imports System.Data
Imports System.Reflection
Namespace Indicators
<serializable()> _
Public Class Indicator
Inherits MarshalByRefObject
Public Function Calculate()
Return 99
End Function
End Class
End Namespace
|
|
|
|
|
OK. I wen't back to what you were doing, and it's not exactly like it is in the code you linked to.
You have two seperate projects, one supplying the class library, the other creating the AppDomain and loading the class library into it.
You have to supply the fully qualified name, including the namespace, to CreateInstanceAndUnwrap. The name of the DLL is also part of the namespace! You're code should look like:
Sub Main
Dim ad As AppDomain = AppDomain.CreateDomain("New Domain")
Dim as As Assembly = Reflection.Assembly.LoadFrom("C:\ClassLibrary1.dll")
Dim remoteCode As Indicator = DirectCast( _
ad.CreateInstanceAndUnwrap(as.FullName, "ClassLibrary1.Indicators.Indicator"), _
Indicator)
Console.WriteLine("Value returned: {0}", remoteCode.Calculate())
|
|
|
|
|
OK, thanks again, but I'm still not there. I still get an error on the CreateInstanceAndUnwrap line.
Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
However the .dll does seem to be read OK and the fullname of the assembly seems right.
I'm also unsure how I need to define the Indicator class type in my main code.
Dim ad As AppDomain = AppDomain.CreateDomain("New Domain")
Dim as1 As Assembly = Reflection.Assembly.LoadFrom("C:\ClassLibrary1.dll")
Console.WriteLine(as1.FullName)
Dim remoteCode As Indicator = DirectCast( _
ad.CreateInstanceAndUnwrap(as1.FullName, "ClassLibrary1.Indicators.Indicator"), _
Indicator)
I simplified the .dll too:
Namespace Indicators
<serializable()> _
Public Class Indicator
Inherits MarshalByRefObject
Public Function Calculate()
Return 99
End Function
End Class
End Namespace
thanks
|
|
|
|
|
cstrader232 wrote: Dim as1 As Assembly = Reflection.Assembly.LoadFrom("C:\ClassLibrary1.dll")
That line will only work if you give it the complete path to the .DLL you want to load. I take it you compiled your .DLL into a file called ClassLibrary1.DLL and copied it to the root of your C: drive?
|
|
|
|
|
|
...and I can read the file just fine. I can load it into my main app easily. I just cannot figure out how to get that assembly to load into a new domainapp.
|
|
|
|
|
Post the code for your Class Library project and for you SEPERATE (important!!) project that creates the app domain. Every character of both projects. There's something you're not showing me or what you're telling me doesn't match what the code is saying.
|
|
|
|
|
OK, thanks for your patience and help.
The project code that creates the separate dll:
Namespace Indicators
Public Class Indicator
Inherits MarshalByRefObject
Public Function Calculate()
Return 99
End Function
End Class
End Namespace
The project code that tries to read the .dll, excluding only the designer code.
(Right now however it won't run because I copied the code you supplied me but I don't know how to define the Indicator type)
Imports System.Reflection
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ad As AppDomain = AppDomain.CreateDomain("New Domain")
Dim as1 As Assembly = Reflection.Assembly.ReflectionOnlyLoadFrom("C:\ClassLibrary1.dll")
Console.WriteLine(as1.FullName)
Dim remoteCode As Indicator = DirectCast( _
ad.CreateInstanceAndUnwrap(as1.FullName, "ClassLibrary1.Indicators.Indicator"), _
Indicator)
End Sub
End Class
|
|
|
|
|
Oh heck, I think I did it. The problem I think was that I moved the .dll away from the project folder and it couldn't find a reference. I think I've done it -- many thanks to you and Luc.
|
|
|
|
|
That's cool. Even if it takes a few weeks, all it does take is perseverance.
|
|
|
|
|
I haven't done this myself, but a simple Google (load assembly in appdomain) gave many including this one[^].
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thanks Luc.
Yes there are hundreds of them, and over the past weeks I've downloaded dozens of sample projects. Although each one has helped me, none have allowed me to do what I need to do.
For instance, some say that you need MarshalByRefObject, others just use serialization, and others don't require either. Another problem is that most are written in C#... I'm not so familiar with that although I'm thinking I may have to learn it. There is much less using vb.net
This morning I thought I had it because I found a C# code that worked, and I just translated that to vb.net. But no...
I've been looking at these for weeks and I simply cannot crack this problem. I'm optimistic that Dave will help me get over the hurdle here.
MSFT doesn't provide good sample code for these methods.
chuck
|
|
|
|
|
Im developing a system using web developer 2005 express and sql server 2005 express
while i connecting to the database to login to the system i get this error "A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)"
This line is highlighted -> conMbr.Open()
Would anyone here explain to me please.Im just a beginner.
Help is very much appreciated.Thanks a lot in advance
My code would as:
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim conMbr As New SqlConnection
Dim cmdMbr As New SqlCommand
Dim sqlReader As SqlDataReader
Dim strVerifyMbr As String
conMbr = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\GpsCinema.mdf';Initial Catalog=GpsCinema;Persist Security Info=True;User ID=sa;Password=password;MultipleActiveResultSets=True;Connect Timeout=30;User Instance=False;Context Connection=False")
conMbr.Open()
strVerifyMbr = "SELECT MemberId FROM datMember WHERE MemberUsername=@txtUsername.text AND MemberPassword=@txtPwd.Text"
cmdMbr = New SqlCommand(strVerifyMbr, conMbr)
Dim checkUsr As String = ""
sqlReader = cmdMbr.ExecuteReader
While sqlReader.Read
If txtUsername.Text = Convert.ToString(sqlReader("MemberUsername")) And txtPwd.Text = Convert.ToString(sqlReader("MemberPassword")) Then
Response.Write("Login success")
Response.Redirect("Default.aspx")
End If
End While
End Sub
Life is up and down
Nevertheless,never give up hope and be confident to yourself
|
|
|
|
|
Is Google not working in Malaysia today?
A quick search on the web for that error message yielded the following article[^].
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Hi,
I having a column named Steps which has more than five steps in it with line break now i want to read it line by line.
Example:
Steps has the following data in one field.
1.Type the valid URL in the address bar. 2.Enter the valid Username in the username textbox. 3.Enter the valid Password in the password textbox. 4.click log in button. 5.click ok button.
i need to read it as
1.Type the valid URL in the address bar.
2.Enter the valid Username in the username textbox.
3.Enter the valid Password in the password textbox.
4.click log in button.
5.click ok button.
Can anybody help me please very urgent...
kokila B
|
|
|
|
|
Replace the end of line (Environment.NewLine ) with a <br/>
|
|
|
|
|
Thanks for your immediate help..
Storing the data is not through Program its from another DB.
so i cant concenrate on insert part..
Kokila B
|
|
|
|
|
Ya I got the result
Dim a() As Char = {"" & Chr(10) & ""}
ds = bl.GetStepDS(record(0))
Dim strtest As String = Nothing
strtest = ds.Tables(0).Rows(0).Item(0).ToString()
Dim strarray() As String = strtest.Split(a)
You will get each line in array strarray.
Thanks,
Kokila
|
|
|
|
|
Hi
Please advise me on below.what is wrong with the code.
i want to check where the supplier is available or not
based on the name,country,currency
Inside my table if i add the supplier for 3 different
currency then 3 rows with same supplier ID is inserted.
<br />
<br />
For co = 0 To LstCurr.Items.Count - 1<br />
If LstCurr.Items(co).Selected Then<br />
strSql = "select supplier_name,country_name,currency from supplier_mst,country_mst where supplier_mst.country_id=country_mst.country_id and supplier_name = '" & Trim(TxtSupplierName.Text) & "' and country_name='" & Trim(DpCountry.SelectedItem.Text) & "' and currency='" & Trim(LstCurr.Items(co).Text) & "' "<br />
command.CommandText = strSql<br />
command.Connection = connection<br />
dataReader = command.ExecuteReader<br />
End If<br />
Next<br />
While (dataReader.Read())<br />
Supplier = True<br />
End While<br />
Catch ex As Exception<br />
Finally<br />
dataReader.Close()<br />
connection.Close()<br />
End Try<br />
|
|
|
|