c# - Implement a [DllImport] like mechanism -
Here's my problem: Within a PCL library, I'm going to call unmanaged code from C + + DLL. This DLL comes in two versions (x86 and x64) and for performance reasons, the correct library should be referenced on the platform where the PCL library is embedded.
Since the [DllImport]
attribute requires a constant string in the form of a library name, it is useless in a very solid way, as the correct library is set to runtime There are some "husband" modes to manually load functions ( LoadLibrary
, GetProcaddress
and GetDelegateForFunctionPointer
) but I make it more convenient Going to programmers.
So, declaring an external function is not a problem. Well, the C # compiler detects the external and is worried about the fact that the missing [DllImport]
attribute With, when the type is loaded then the external solution can not be. OK, I have defined the
At the runtime, I definitely get a type of load upbeat beacuse. My external is really unresolved, it raises two questions:
1) Why the compiler is happy with any specialty is? Does the loader have any magic in relation to using that feature to solve the pending external? This feature can be done directly by providing interface to implement. Thus runtime will try to see the characteristics of external search for people who implement the "magic" interface.
2) How can I implement my type of loader, how can I hold TypeLoadException? The loader will be replicated with all the externally given type, read the [MyImport] attribute and thus solve the external.
There can be any idea from there if one of the two ideas can possibly be implemented or another
Christian
< / P>
Actually I Went along, in the comments section and below.
As he mentioned, the whole problem is rather the problem of deployment and there is little to address with the development of the solution. Therefore, I gave two platform versions the same name and kept them in separate directories - such as:
SystemTera.MyPCL.dll x86 \ SystemTera.Platform.dll x64 \ SystemTera. Platform. DLL
At the startup, I point the loader to the correct platform version:
public Static class platform {[DllImport ("kernel32.dll", SetLastError = true)] [Return: Marshall AS (Unmanaged Type Bowl)] Fixed External Bull Setdell Directory (string pathname); Public Fixed Zero Setup (if (Environment.Is64BitProcess) SetDllDirectory ("./x64 /"); Else // The default Win32 SetDll directory ("./x86 /");}}
< P> When I refer to front-stage libraries, loader / jitter works right: public class of myslas {[DllImport ("SystemTera .Platform.dll")] Fixed External Zero MyPlatformFunction (); Public Zero DoTheJob () {MyPlatformFunction ();}}
This is a fine solution by using the existing concepts provided by the framework.
Comments
Post a Comment