How do I dynamically load dll outside project? - c#

How can I dynamically load a DLL?
Please don't suggest:
Don't suggest GAC, because the DLL is already made and can't be changed or updated.
Don't suggest hintpath, Assembly.LoadFrom(...) since this functionality works at debugging not outside the project.
If I copy that DLL/exe onto a test machine outside the project then it shows an error: could not load .dll.
I also asked a question at the MSDN forums.
Could anyone help me?
Thanks
Ashish

System.Reflection.Assembly.LoadFile() works just fine for this.
If you are getting BadImageFormatException, check whether the assembly you are trying to load is compiled for the same platform target (x86 or x64) as the assembly doing the loading.
Update based on comments
Sounds like you are trying to load a .NET 2.0 assembly into a .NET 4.0 application, and you have already put the following in your application configuration file:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
</configuration>
In that case, check to make sure your configuration file is getting deployed on your target machine alongside your executable. For example, if your application is called MyProgram.exe, your configuration file should be called MyProgram.exe.config - and it needs to be copied to the same folder as the executable on your target machine to have any effect - the framework will load this file when it starts up, if it exists.

1) Try using assembly.loadfile function instead of loadfrom. It will work outside the project also. 2) If u got assembly mixed mode error then, just need to copy xml file along with dll generated in bin folder.xml file contains app.config file data. Code in app.config file is...
also check link... http://social.msdn.microsoft.com/Forums/en/csharpide/thread/99691cc4-27df-48e7-b4aa-377f74109425

Related

Why my Windows form application not connecting to mysql in another computer? [duplicate]

I am working on Desktop application with c# and Data base MySQL. When I install its installer on my machine it works fine but when I install it on other machine its give following exception when try to access DB. I am using MySQL.Data.dll to communicate with MySQL.
Could not load file or assembly 'MySql.Data, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified.
and MySql.Data.dll file in present in Project's folder in Program files folder
Actually when I run it from its folder in Program file it run fine with no error but When i try to run it from its shortcut in Start Menu it gives that error.
It sounds i am 2 years late answering this post but it might be helpful for those who are still facing this issue, so here is my finding dated 1st April 2012 5pm EST:
I had the same issue with one of my web application. And I found the said issue arises when you do:
Copy & Paste the MySql.Data.dll somewhere in a folder.
You have a copy of any version of MySql.Data.dll in GAC
Though application works fine on your development machine as it can see the files but when you deploy it on some other machine it actually brings the run time error.
In my case, the VS2008 always pointed me with the same error as you mentioned. I then did this:
Removed the local copy reference of the dll
Referenced the DLL found in GAC
And set the property "Copy Local" to "True" of the DLL by right-clicking->properties.
Edit:
Somebody asked "Where is GAC?":
http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.110).aspx
Does the shortcut in the Start Menu set the working directory correctly? (I suspect that this is the most likely answer)
Is there a different/incorrect version of MySql.Data.dll installed in the GAC (Global Assembly Cache)? I've seen this give similar error messages before.
Is MySQL.data.dll present in the same directory as the .exe file ?
If so does that MySQL.data.dll have the proper version/public key that the .exe file is looking for ?
When this thing happens to me it is usually one out of two things:
Make sure that MySql.Data is present on the machine where you get the error. (It unbelievable how often a files turns out to be missing :-) )
If MySql.Data is a mixed mode (native and managed code) 32 bit DLL. And you executable specifies "Any CPU". On a 64 bit machine with 64 bit .NET this will fail with error message you got. A solution is to specify "x86" as target for the executable.
Tommy's reason is very valid:
My project was referencing to an older version of the MySql.Data.dll compared to what was actually installed on my development machine. This will result in the same error.
Check you .config file:
And compare that verisonNr to the versionNr of the file when you would add a new reference to it.
Solution:
1) remove the line from your config file and re-add the reference
2) or uninstall the MySql .net connector and install the version which your project is referencing to.
I had this issue too, for me it was recreating the connection strings in project settings. They were configured for a previous version of the MySQL connector.
I was having the same problem in a web application (C#).
I managed to solve it by changing the web.config file, it was referencing version 6.2.2.0, and my dll was version 8.0.25.0.
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,
Version=8.0.25.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
Make sure that the MySql.Data DLL you put in the Project's folder is the correct version (6.2.2.0 in this case).

Converting from framework 3.5 to 4 app.config location

I had a project whose framework is 3.5 and i converted it to 4 and when i faced a problem of mixed mode. i changed the useLegacyV2RuntimeActivationPolicy from this answer
Now when i install my application to program files and try to run it, it crashes but when i place the config file beside my exe it runs.
Can anyone tell my the reason of that crash?
but when it was 3.5 i didn't need to place the app.config file beside the exe
That was required in 3.5 as well. It is the only way that the CLR can find the .config file. I'd have to guess that you somehow got away with it before and it just didn't matter. But now it is critical to let the CLR find the .config file since you really need that attribute to allow the mixed-mode assembly to load.
An obvious way ahead is to rebuild the C++/CLI assembly and have it target v4 as well. So you won't need the .config file anymore. If it is not yours then ask the vendor or author of the assembly for an update.

How to use a custom name for app.config or hard-code config values in my assembly

I have written a class library in C# (mydll.dll) which accesses a third party dll (3rdpdll.dll). Mydll.dll, in turn gets consumed via COM interop by a non-.NET process (myapp.exe).
Mydll.dll is .NET 4.0 but 3rdpdll.dll has been built using v2.0.50727, so when I run myapp.exe, I get the following error:
Mixed mode assembly is built against version 'v2.0.50727' of the
runtime and cannot be loaded in the 4.0 runtime without additional
configuration information...
This gets fixed if I create a file called "myapp.exe.config" next to myapp.exe containing the following XML:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
Problem is myapp.exe gets updated a lot and contains version information in the name, such as myapp.v2.063.exe. This would require the config file to be renamed with every new iteration of the app (myapp.v2.063.exe.config
).
Is there a way for me to declare a static name for the config file, such as "app.config" so that it does not need to be renamed and reshipped along with every new iteration of myapp.exe? Or better yet, could I hard-code the useLegacyV2RuntimeActivationPolicy attribute in mydll.dll so that I can avoid using a config file altogether?
In Visual Studio 2010 you can rename the app.config file with:
in Project -> Project Properties, select Build Events tab and edit the post-build event command-line to rename the .config file, example:
rename "$(TargetDir)$(TargetFileName).config" "$(TargetFileName).versionX"
I may be wrong but just thinking, if you recompile your .net 4.0 assembly in .net 2.0 framework, as .backward compatibility is there. Then i am thinking this error of mixedmode assembly should go and you may not need this config.
I went with Joe's idea:
If MyApp.exe gets updated a lot, could you prevail on it to rename or
even autogenerate the config file with a matching name before its
first call into any .NET code? – Joe Jan 12 at 17:38

Call DLL files located in another directory

I have a console application (NGameHost) running in a specific directory (C:\Program Files\NetworkGame3\api\). It uses the files available in that directory and the console application works well when run on its own. It also exposes various methods that use the DLLs (and other files such as config files) from that directory. I now have another console application (located elsewhere) that tried to call those methods and return the results. I've set Copy Local: False so that it executes within that directory instead of creating a local version. However I get the error "Could not load file or assembly ... or one of its dependencies. The system cannot find the file specified."
How can I call the methods from a console application located in another directory?
When you set copy local = false what you're saying is that do not copy the assembly to the local directory in which case assembly will available in one of the places where the runtime looks for it.
See How the runtime locates assembly
Your assembly has to be either in GAC or in one of the probing locations.
You can use the GAC, a config approach or an assembly resolve event.
This KB covers it in more detail:
http://support.microsoft.com/kb/837908
Also look into probing paths:
http://msdn.microsoft.com/en-us/library/15hyw9x3(v=vs.71).aspx
I've recently been bashing my head against a brick wall trying to resolve a very similar problem. I've a DLL I want to load at runtime, and it has it's own dependencies. I couldn't for the life of me figure out why it would load, but when calling a class's constructor in the loaded assembly, that no error was raised, and execution simply stalls, leaving you wondering what's going on.
Turns out that a dependency of the loaded assembly was in a different .NET version, and there is an App.Config on that code to enable mixed mode assemblies. So I naturally had to bring that into my code too, as I'm calling an assembly that calls an assembly in a different version of .NET.
The error didn't present itself until I copied all the dependent's DLL's to my application in development. Now I can remove them again! In doing so, I get a DLL Loader Lock warning message. If I suppress it or ignore it, my code works.
In my case then, the resolution was :
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>

Why doesn't .NET find the OpenSSL.NET dll?

EDIT (the whole question, it was too unclear)
I want to use OpenSSL.NET
The OpenSSL.NET install instructions page: INSTALL
Make sure you have libeay32.dll and ssleay32.dll in the current working
directory of your application or in your PATH. DONE
In your .NET project, add a reference to the ManagedOpenSsl.dll assembly. DONE
I have put libeay32.dll and ssleay32.dll in both my bin/Debug and bin/Release directories. I have also put them in system32.
Here is my FULL code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
OpenSSL.Crypto.RSA rsa = new OpenSSL.Crypto.RSA();
}
catch (Exception e)
{
Console.WriteLine(e.InnerException.Message);
}
Console.Read();
}
}
}
I get the following error:
Unable to load DLL 'libeay32' http://localhostr.com/files/a719c5/Error.gif
(Unable to load DLL 'libeay32')
Here is the Process Monitor log (upon request):
alt text http://localhostr.com/files/726a46/ProcMon.gif
What am I doing wrong? Why isn't the DLL found?
Try the latest version of OpenSSL.NET (0.4.1) which should now include prebuilt libeay32.dll and ssleay32.dll binaries that link to the CRT statically. Alternatively, you can build these libraries yourself or use an 'official' build from openssl.org.
Without looking at your code exactly, I get that error when I:
do not have the dlls in the path of the executable (not where your sln resides, but where the .exe is made, typically in bin/debug or bin/x86/debug or whatever).
do not have the proper signature of the calling function (ie, I left out an integer parameter, the return types don't match, etc).
am not marshalling the types properly (ie, BOOL is marshalled as a bool, while bool is marshalled as a unsigned single byte integer, etc)-- while this last one may not cause the exception, it can cause decidedly funky behavior.
am on a 64 bit platform and am calling a 32 bit dll. The pointer sizes will be all different, and the dll will probably just crash and cause that exception.
EDIT: When all else fails, try dependency walker, because it sounds like your dlls are calling other dlls that aren't in your path or in the directory of the executable.
For anyone else out there still experiencing this issue (and have verified that the necessary prerequisites exist in their correct locations:
Check the OpenSSL.NET installation documentation and ensure its prerequisites are installed. In my case, a user was missing the Microsoft Visual C++ 2010 Redistributable Package (x86) dependency which is called out in the OpenSSL.NET documentation.
Your problem is related with this question:
DllNotFoundException, but DLL is there
Verify if all depencencies are in same folder of your application or are registred.
Try using probing. You need to create an XML config file named as the application's executable complete name (or named as the assembly that requieres your non-managed dll) with a .config extension. E.g. if your applications is name myapp.exe, the config file will be named myapp.exe.config
The config file must be located in the same directory as the executable / assembly .
The config file is a simple xml file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyuBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="PATH" />
</assemblyuBinding>
</runtime>
</configuration>
Now the application will search in PATH when loading the assemblies. PATH is relative to the config /assembly file.
Not sure if it will work for non-managed dlls, but is worth the try.
The .NET way of doing this is to install your assembly in the global assembly cache.
Each computer where the common
language runtime is installed has a
machine-wide code cache called the
global assembly cache. The global
assembly cache stores assemblies
specifically designated to be shared
by several applications on the
computer.
As a last resort, if nothing else works:
It may be useful to know where the application (.net or not) is looking for the DLLs. Just use Process Monitor and filter for the file name of the DLL. Then copy it to a location where the application is looking for it.
You're probably missing the VC++ redistributables. I'm assuming OpenSSL.NET is x86 only, so you can grab the VS2008 version x86 redistributable if they're release builds.
Otherwise, if they're debug builds (you'll see Microsoft.VC90.DebugCRT in EventViewer or the sxstrace logs) then you'll need to either:
Rebuild them as release
Install or copy the debug redistributables from another machine
Install Visual C++ into Visual Studio (or, probably, Visual C++ Express)
I found a solution.
Unfortunately the VS2008 C++ Redistributable package didn't work - I had to install the SP1 version AND VC++2008. The author said in a comment on its website that it was a mistake on its side, and not mine. He is currently recompiling the DLLs to be statically linked. Thank you to all of those who helped me :)
Try changing the Platform target for your project to x86 instead of "any cpu".
In my case, when we develop a web site with open ssl on x64 win 2008 platforms, we must check with application pool : allow 32 applications : true
Create New Folder Named x86 in your application path and then put libeay32.dll,ssleay32.dll in x86 folder.

Categories