首页 > 解决方案 > 如何从 Delphi 10.3 Rio 中的 iOS API 函数获取 CFStringRef 值

问题描述

无法CFStringRef从 Delphi 10.3 Rio 中的 iOS API 函数(框架)获取值

// external call bridge function to iOS:
function MIDIObjectGetStringProperty(obj: MIDIObjectRef;
            propertyID: CFStringRef;
            out str: CFStringRef):OSStatus; cdecl; external libCoreMidi name _PU + 'MIDIObjectGetStringProperty'; 

函数MIDIObjectGetStringProperty(iOS CoreMIDI 函数)str:CFStringRef以 MIDI 端口的名称返回...

如何CFString在 Delphi 中获取变量的值?在这个例子中str:CFStringRef值?

我在我的函数中尝试它:

function getDisplayName(obj: MIDIEndpointRef):string;
var 
    EndPointName: CFStringRef;
    i:integer;
begin
    //EndPointName:= nil; // when I assign nil value, function return i=-50 otherwise raise Access Violation error ...

    i := MIDIObjectGetStringProperty(obj, kMIDIPropertyDisplayName , EndPointName); --> AV error !!!

    //in EndPointName should be returned CFStringRef value from iOS 

    getDisplayName :=  CFToDelphiString(EndPointName); // convert to string
end;

可能EndPointName需要分配...否则我给出 AV 错误。请有人解决如何CFStringRef从 iOS 框架中获取任何值并转换为字符串?谢谢。

补充:

我通过 FireMonkey frameforks api 在 Delphi Rio 中构建跨平台(iOS、Android、W64)应用程序 - 对于 CoreMIDI,我使用这个接口https://github.com/FMXExpress/ios-object-pascal-wrapper/blob/master/iOSapi.CoreMIDI .pas

所以外部调用和常量是在 iOSapi.CoreMIDI 中定义的:

function MIDIObjectGetStringProperty (obj: MIDIObjectRef; propertyID: CFStringRef; str: CFStringRef) : OSStatus; cdecl; external libCoreMIDI name _PU + 'MIDIObjectGetStringProperty';

和 iOS 指针常量:

function kMIDIPropertyDisplayName: Pointer;
begin
  Result := CocoaPointerConst(libCoreMIDI, 'kMIDIPropertyDisplayName');
end;

基于此解决方案https://pjstrnad.com/reading-data-midi-keyboard-ios-probably-also-mac/

obj: MIDIObjectRef 是来自 source:= MIDIGetSource(ci) 的源指针;

问题是调用 API 函数 MIDIObjectGetStringProperty。在指针 str 中:CFStringRef (EndPointName) 应该是 MIDIportNAME 的 VALUE。我无法获取此值并解析为 delphi 字符串...

我尝试将此指针 CFStringRef 声明为:

var
EndPointName: pointer;
EndPointName1: array of Byte;
EndPointName2: TBytes;
EndPointName3: TPtrWrapper;
M: TMarshaller;

和建设为:

SetLength(EndPointName1, 255);
GetMem(EndPointName2,255);
EndPointName3 := M.AllocMem(255);

i := MIDIObjectGetStringProperty(obj, kMIDIPropertyDisplayName , @EndPointNameX);

--> 没有任何效果,AV 错误!!!

我认为它必须是如何获取 CFStringRef 并转换为 delphi 字符串的解决方案......

标签: iosdelphiframeworksfiremonkey

解决方案


kMIDIPropertyDisplayNameiOS 上已损坏,但您可以将其替换为 CFSTR('displayName')。这个对我有用。所以你的函数看起来像这样:

function getDisplayName(aEndPointRef: MIDIEndpointRef):string;
var 
  LEndPointName: CFStringRef;
  i:integer;
begin
  getDisplayName := 'MIDI endpoint';
  LEndPointName:= nil; 
  i := MIDIObjectGetStringProperty(aEndPointRef, CFSTR('displayName'), LEndPointName);
  if (i=0) and (LEndPointName<>nil) then
    getDisplayName := CFStringRefToStr(LEndPointName);
end;

Macapi.CoreFoundation将and添加Macapi.Helpers到您的 USES 子句以访问CFSTRandCFStringRefToStr


推荐阅读