locked
move dll from the build dir to other dir RRS feed

  • Question

  • Hi all,

     

    I am trying to implement the following functionality in c# code:

    1)import a f.dll(compiled fortran dll ) 

    2)put the dll from default build dir to a shared dir (c:/dlls)

    3)call the dll entry function (void entry())

    My question is:

    how can I know that if I call entry() function, the f.dll that in shared dir get executed?

    since the f.dll reads input file from the same dir of itself, and write output file to the same dir as itself. I want to make sure that the input file need to be put into the shared dir.

     

    Thanks a lot!

    Boyu

    Tuesday, February 8, 2011 5:05 PM

Answers

  • You can delete the DLL from the current directory prior to loading "f.dll".  This will prevent it from being loaded.

     

    Otherwise, you can explicitly call LoadLibrary via P/Invoke to load the "shared" DLL prior to using it.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, February 8, 2011 5:27 PM
  • Use:

     

     [DllImport("f.dll")]
     public static extern int dllFun();


    Then call LoadLibrary on startup, before you use the the type that defines the function.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, February 9, 2011 10:48 PM

All replies

  • You can delete the DLL from the current directory prior to loading "f.dll".  This will prevent it from being loaded.

     

    Otherwise, you can explicitly call LoadLibrary via P/Invoke to load the "shared" DLL prior to using it.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, February 8, 2011 5:27 PM
  • You can delete the DLL from the current directory prior to loading "f.dll".  This will prevent it from being loaded.

     

    Otherwise, you can explicitly call LoadLibrary via P/Invoke to load the "shared" DLL prior to using it.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Hi Reed,

    Thanks for the reply. I used loadlibrary and getprocaddress, but I don't know how to call the function in dll after getprocaddress. Here is my code:

     

    Class myClass{
      [DllImport("kernel32.dll", SetLastError = true)]
      public static extern IntPtr LoadLibrary(string IpFileName);
      [DllImport("kernel32.dll")]
      public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
      [DllImport("kernel32.dll")]
      public static extern bool FreeLibrary(IntPtr hModule);
      internal delegate int DllRegisterServerInvoker();
    ....
    
     void run(){
       string desPath = @"C:\dlls\f.dll"; \\the one with dllFun inside
       IntPtr hDll = LoadLibrary(desPath);
       IntPtr hFun = GetProcAddress(hDll, "dllFun");
       DllRegisterServerInvoker drs = (DllRegisterServerInvoker) Marshal.GetDelegateForFunctionPointer( hCharmmFun, typeof(DllRegisterServerInvoker) );
    
       int result = drs();
    
       }
    }

     

    The result is that the function "dllFun" in the dll in C:\dlls/f.dll is not called.

     

    My questions are:

    1)with the LoadLibrary() statement, does it mean that the f.dll specified in desPath is the one that will be used?

    2)how to call the actual function "dllFun" after call GetProcAddress? Since the return value of GetProcAddress is a IntPtr.

     

    Thanks a lot!

    Boyu

    Wednesday, February 9, 2011 9:40 PM
  • You can still use P/Invoke to call the function.  Just have your Main() call LoadLibrary (not GetProcAddress, though) at your program's startup.

     

    When you go to use your P/Invoke method, the CLR will see that DLL loaded in the address space of the process already, and just use it.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, February 9, 2011 9:53 PM
  • if I only use

      public static extern int dllFun();

     string desPath = @"C:\dlls\f.dll"; \\the one with dllFun inside
      IntPtr hDll = LoadLibrary(desPath)

     int result = dllFun();

    without Dllimport. it will not find the dllFun().
    I am a starter in c sharp and dll, really appreciated the help. Thanks.
    Boyu

    Wednesday, February 9, 2011 10:38 PM
  • Use:

     

     [DllImport("f.dll")]
     public static extern int dllFun();


    Then call LoadLibrary on startup, before you use the the type that defines the function.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, February 9, 2011 10:48 PM
  • using this code could work but not the way I want it to be

     

            [DllImport("f.dll")]

            public static extern int dllFun();

     

         string desPath = @"C:\dlls\f.dll"; \\the one with dllFun inside
          IntPtr hDll = LoadLibrary(desPath)

          int result = dllFun();

    it is able to find the dllFun and run.

    The problem is: dllFun takes as input a a.in file in the same directory of f.dll, produces a b.out file as output in the same directory of f.dll.
    I loaded the dll from C:\dlls\f.dll, but the dllFun looks the directory ~project/bin/debug/ to search the a.inp and produces b.out in the ~project/bin/debug/ too.
    what I want it to do is: look into c:\dlls for a.inp file and produce b.out to c:\dlls.

    Any idea why this happens and how to do that?

    Thanks!
    Boyu

    Wednesday, February 9, 2011 10:48 PM
  • figure out how to do that, I changed the environment.currentdirctory before call the dllFun and change it back after the function returns, thanks!!

     

    Boyu

    Wednesday, February 9, 2011 11:00 PM