c# - Wrapping a static unmanaged library in an instance class -
i'm using unmanaged library (written in c++) in code (c#). methods provided library static. utilize p/invoke communicate library. here's way library works:
there's initialization method need phone call before calling other methods (this takes 1 parameter , parameter can set during initialization). there's settings method alter different settings library, need run if need alter way library works (more fine-tuning library). settings can changed time. there method takes array of numbers, , returns array. let's phone call intermediate operation. operation takes while, i'd cache , prevent calculating every time. the array returned previous method (intermediate operation) input lastly method creates result. let's phone call main operationi'd have different instances of library, 1 settings a , other 1 settings b. library static, can't.
how can wrap library in instance class? probably, every instance of class need load new instance of library in memory (as every instance of library static, , can't have 2 different setting in 1 instance of library).
is there workaround this, other re-writing library (as don't have command on how it's written)?
and i'd able handle parallel calls operation methods.
here's illustration implementation of what's been said in comments question:
public class library { private static class native { [dllimport("foobar.dll")] public static extern void initialize(); [dllimport("foobar.dll")] public static extern void settings(int param1, string param2); [dllimport("foobar.dll")] public static extern float[] intermediate(float[] input); [dllimport("foobar.dll")] public static extern void mainmethod(float[] main); } private static readonly object sync = new object(); private int param1; private string param2; static library() { native.initialize(); } public void settings(int param1, string param2) { this.param1 = param1; this.param2 = param2; } public float[] intermediate(float[] input) { lock (sync) { native.settings(param1, param2); homecoming native.intermediate(input); } } public void mainmethod(float[] input) { lock (sync) { native.settings(param1, param2); native.mainmethod(input); } } }
the of import things
settings not phone call "real" settings immediately, stores values when intermediate , mainmethod called apply settings store previously both calls synchronized on static object avoid calls different instances of library running on different threads interfere. c# c++ static pinvoke
No comments:
Post a Comment