c# - Dynamically loaded assembly in Silverlight calles DllImprot which locks the file -



c# - Dynamically loaded assembly in Silverlight calles DllImprot which locks the file -

i've quite specific problem silvelight 5 application: i've application dynamically loads silverlight libraries (could many) , libraries callings methods via dllimport.

the reason: application different scanning devices. silverlight library logic, c++ library producer , connects device. many silverlight libraries , must load in runtime.

problem: after loading silverlight library , scanning - can't access file c++ library. why must ? if user tries pick scanner, calculate hash code of library check if there's newer version on server.

my implementation: viewmodel calling method current scanner class:

using (var stream = new isolatedstoragefilestream(scanner.directoryname + path.directoryseparatorchar + scanner.scannerlibraryname, system.io.filemode.open, store)) { assemblypart assemblypart = new assemblypart(); assembly assembly = assemblypart.load(stream); homecoming (scanner)assembly.createinstance(scanner.classname); }

later there's scanning:

this.userscanner.loadproducerlibrary(); result = userscanner.scandocument(); this.userscanner.freeproducerlibrary();

loadproducerlibrary calls:

[dllimport("kernel32", setlasterror = true, charset = charset.unicode)] public static extern intptr loadlibrary(string lpfilename);

scandocument calls 3rd part methods via dllimport.

freeproducerlibray calls:

[dllimport("kernel32.dll", setlasterror = true)] [return: marshalas(unmanagedtype.bool)] /* unnecessary, isn't it? */ public static extern bool freelibrary(intptr hmodule);

after that, i've method in different viewmodel tries open producer library file calculate hash code , "cannot access file, because beingness used process".

i've tried calculate hash code in scanner class (dynamically load assembly) , right suspect process. why freelibrary not plenty ?

could point me advises ? should seek dispose somehow userscanner field ?

scandocument calls 3rd part methods via dllimport.

you left of import code out of question, statement plenty diagnose problem. pinvoke marshaller calls loadlibrary() on dll. reference count dll two. freelibrary() phone call not unload , file stays locked.

you can hack around calling freelibrary() 1 more time. will unload dll arms time-bomb in program. cannot safely create these calls again. pinvoke marshaller still convinced dll loaded not re-generate stubs generated imported functions. point addresses functions used loaded, not loaded now. seem work, particularly when in artificial test. sooner or later, dll cannot reloaded @ same address 1 time again because address space used else , programme fail hard crash.

if want create work cannot utilize [dllimport] these methods. must instead work pinvoke marshaller , phone call getprocaddress() every entrypoint utilize , create callable marshal.getdelegateforfunctionpointer(), using delegates declared match native function signature. works, not pretty.

the improve alternative utilize separate helper process makes these calls , interop it. safe, letting helper process terminate ensures pinvoke stubs gone , dll unlocked. not sure what's practical in silverlight, wasn't designed process interop high on feature list. no process class, puts chink in approach.

c# c++ silverlight

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -