The following minimalistic example works fine on IronPython (NodaTime is freely available at http://nodatime.org/):
import clr
clr.AddReference('NodaTime')
from NodaTime import SystemClock
print SystemClock.Instance.ToString()
print SystemClock.Instance.Now
It outputs, as expected:
NodaTime.SystemClock
2016-10-07T13:25:17Z
However, the following Delphi code using mscorlib_tlb (.NET interop layer) fails:
const
CLASS_CLRMetaHost: TGUID = '{9280188D-0E8E-4867-B30C-7FA83884E8DE}';
procedure CLRCreateInstance(const rclsid: TCLSID; const riid: TIID;
out pv); safecall; external 'mscoree';
var
CLR: ICLRMetaHost;
Runtime: ICLRRuntimeInfo;
Host: ICLRRuntimeHost;
CorHost: ICorRuntimeHost;
Domain: AppDomain;
Assmbly: Assembly;
Typ: Type_;
SystemClock: OleVariant;
V: IUnknown;
begin
CLRCreateInstance(CLASS_CLRMetaHost, IID_ICLRMetaHost, CLR);
Runtime := ICLRRuntimeInfo(CLR.GetRuntime('v2.0.50727', IID_ICLRRuntimeInfo));
Host := ICLRRuntimeHost(Runtime.GetInterface(CLASS_CLRRuntimeHost, IID_ICLRRuntimeHost));
Host.Start;
CorHost := ICorRuntimeHost(Runtime.GetInterface(CLASS_CorRuntimeHost, IID_ICorRuntimeHost));
CorHost.CreateDomain('', nil, V);
Domain := V as AppDomain;
Assmbly := Domain.Load_2('NodaTime');
Typ := Assmbly.GetType_2('NodaTime.SystemClock');
SystemClock := Typ.GetField_2('Instance').GetValue(Null);
WriteLn(Typ.GetMethod_6('ToString').Invoke_3(SystemClock, nil));
Typ.GetProperty_7('Now').GetValue(SystemClock, nil);
end.
It outputs:
NodaTime.SystemClock
Exception EOleException in module test.EXE at 000DD9A2.
Значение не попадает в ожидаемый диапазон.
(The exception code is actually 0x80070057 E_INVALIDARG "One or more arguments are not valid". Strange -- I do not pass any arguments.)
The problem is with the property NodaTime.SystemClock.Now that returns a struct (value type) NodaTime.Instant. It appears that NodaTime.Instant has IsLayoutSequential = True, so the struct should be unmanaged-compatible.
What is the right way to retrieve NodaTime.SystemClock.Now from unmanaged code?
Aucun commentaire:
Enregistrer un commentaire