views:

115

answers:

5

ok... now this is something new for me..

I have a utility.dll file which is in my bin folder and i am accessing it in my current application. This part is working fine....

public partial class Reports1 : System.Web.UI.Page
{
[DllImport("Utility.dll")]
    public static extern bool GetErrorString(uint lookupCode, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buf, uint bufSize);
}

But now i have to use .dll from a folder not in the bin but some other folder in the C:/

I tried using registery key in which i stored the path of the folder in a registery key and get that path and put it in place of Utility.dll but this did not work.... I got an error message An attribute must be a constant expression of an attribute parameter type....

 public partial class Reports1 : System.Web.UI.Page
{

    private static string PathName
    {
        get
        {
            using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software/Copium"))
            {
                return (string)registryKey.GetValue("BinDir");
            }
        }
    }


    [DllImport(PathName)]
    public static extern bool GetErrorString(uint lookupCode, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buf, uint bufSize);

Naybody with any suggestions... Thanks.

A: 

Could you use an environmental variable as part of the DllImport string argument constant? That way you can "parameterize" it from outside the application.

Also, it might be that as long as the DLL file is somewhere in the current execution scope's PATH (environmental path, not file path). (C:\windows\system32 for example.)

Peter
A: 

If it's a DLL that you are going to be using in several different programs you could register the DLL in the Global Assembly Cache. See here

TLiebe
A: 

Check out this article to see the order that the loader searches for DLL's.

You can also add your own path to the search folder using SetDllDirectory, but that probably won't work with DLLImport.

jheddings
A: 

Possibly the Value property of the DllImport attribute sheds some light on this when it states

You can provide a full or relative path. If you provide no path, the DLL must be in the current path at run time, unless the DLL is loaded by some other means. Be aware, however, that using a fully qualified path can introduce inaccuracy if the DLL is moved.

However seeing the property is read-only I have to assume it's referring to path allowances for the attribute's construction in which case you would try

[DllImport(@"C:\mydir\yourdir\theirdir\Utility.dll")]

This is just a shot in the dark because I have not tried it.

Edit: And wherever that DLL is sitting, make sure it has the correct file permissions to be used.

John K
the thing is i cannot hardcode the path... because the dll may be in different folders...
A: 

Another way of accomplishing this is to perhaps use a combination of AppDomain.AssemblyResolve event and the Assembly.LoadFrom() method as noted here: http://codebetter.com/blogs/patricksmacchia/archive/2007/07/19/prevent-corrupted-installation-in-production.aspx

I personally haven't tried this out, but it looks and sounds like it's work for you.

pdalbe01