简体   繁体   中英

Refresh dll charged by DllImport

I'm using an API for my C# project. I also have an installer that setup the executable (named uEye) on version 4.7.

My problem is:

  • I have the uEye version 4.3 on my computer
  • I start my application, I had an error "wrong version"
  • I start my setup
  • Setup detect that i don't have the 4.7 version
  • It installs the 4.7 version
  • I start my application, I still had the error until I reboot

I used a decompiler on the ddl provided by the API. Function to get version is :

public static int GetDLLVersion(out int s32Version)
{
    s32Version = IntPtr.Size != 8 ?        
        uEye.Info.System.ApiWrapper.GetDLLVersion_32() : 
        uEye.Info.System.ApiWrapper.GetDLLVersion_64();
    return 0;
}

[DllImport("ueye_api_64.dll", EntryPoint = "is_GetDLLVersion")]
private static extern int GetDLLVersion_64();

I wonder if DllImport does not use a kind of cache ? In this case how can i refresh it ?

Thank you

I wonder if DllImport does not use a kind of cache?

There is indeed a cache. The unmanaged DLL is loaded the first time that your application calls a function in that library. At that point the DLL is loaded in your .net process and it will stay loaded until that process terminates. When you restart the process, the unmanaged DLL is again loaded when the first function is called that requires that DLL.

The issue that you are encountering is that the DLL is loaded in another process when the install program attempts to update it. When a DLL is loaded in a process, its file cannot be modified. Your install program is therefore unable to modify the file on disk immediately and instead postpones that update until the next reboot. If your install program is well written then it should prompt the user that a reboot is needed to complete installation.

In short, the behaviour here is as expected and there is really very little else that can be done. The DLL simply cannot be updated whilst your program has it open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM