Click here to Skip to main content
15,068,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to set the .dll file path to the function DllImport[(filepath)] from app.config file instead of Dllimport[("C://dll.dll")]

Please help me in this regard.

Thanks in Advance.
Posted
Updated 12-Jun-11 23:07pm
v2

C#
using System;
using System.Runtime.InteropServices;

class Example
{
    // Use DllImport to import the Win32 MessageBox function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

    static void Main()
    {
        // Call the MessageBox function using platform invoke.
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}

For more details...
See this link
and also see this link
   
v2
Comments
   
So what? It does not solve the problem and does not explain anything.
Please see my solution.
--SA
You cannot to it. Here is why: let's look at the System.Runtime.InteropServices.DllImportAttribute MSDN help page http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.dllimportattribute.aspx[^]: there is only one constructor with only one parameter: a DLL name. This can be not just the file name, but also a full assembly name in case if an unmanaged DLL file is included in an assembly — please look at the code sample.

That's it. I cannot be anything else. (I did not discuss positional parameters used in attribute application, they are actually properties, not call parameters.)

Now, the statement for the application of attribute (in this case, the the extern method declaration) is not a "real" method call. In particular, parameters can be only the constants (or types via typeof). It can be an immediate constant or a explicitly declared constant (much better), but a real constant, not a read-only variable.

For that reason, it cannot be anything read from the file, a configuration file or any other file, as data read from file cannot be constant.

Conclusion: not only it is impossible, but I've proved that.

Now, I'm curious: why doing that?

Perhaps you need different DLLs to be pluggable. No, this is not how it works. A linked DLL name is compiled into assembly executable module, that's it. If you need to have a pluggable unmanaged DLLs (depending on configuration or other factor) you need completely different approach: P/Invoke Windows APIs LoadLibrary and GetProcAddress and load DLLs during run time.

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900