Ctypes如何设置需要字符但定义为无符号int(OSX)的OSType

2024-09-27 00:14:19 发布

您现在位置:Python中文网/ 问答频道 /正文

所以使用CTypes我试着转换下面的代码。它使用OSType,OSType被定义为

FourCharCode = ctypes.c_uint32
OSType = FourCharCode
QTPropertyClass = OSType

我们在这里看到它是一个usngiend字符。但我们在下面的片段中看到,他们将其定义为

const OSType    kFinderSig = 'MACS';

因此,在ctypes中,将变量设置为'MACS'将不会是cUInt32类型,但它将是char no?你知道吗

请告知。你知道吗

谢谢

https://stackoverflow.com/a/8895297/1828637

OSStatus    SendFinderSyncEvent( const FSRef* inObjectRef )
{
    AppleEvent  theEvent = { typeNull, NULL };
    AppleEvent  replyEvent = { typeNull, NULL };
    AliasHandle itemAlias = NULL;
    const OSType    kFinderSig = 'MACS';

    OSStatus    err = FSNewAliasMinimal( inObjectRef, &itemAlias );
    if (err == noErr)
    {
        err = AEBuildAppleEvent( kAEFinderSuite, kAESync, typeApplSignature,
            &kFinderSig, sizeof(OSType), kAutoGenerateReturnID,
            kAnyTransactionID, &theEvent, NULL, "'----':alis(@@)", itemAlias );

        if (err == noErr)
        {
            err = AESendMessage( &theEvent, &replyEvent, kAENoReply,
                kAEDefaultTimeout );

            AEDisposeDesc( &replyEvent );
            AEDisposeDesc( &theEvent );
        }

        DisposeHandle( (Handle)itemAlias );
    }

    return err;
}

Tags: 定义ctypesnullerrmacsconstostypefourcharcode
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:19
const OSType    kFinderSig = 'MACS';

'MACS'是字符常量,不是字符串!这段代码正在使用multi-character character constants的(可疑的)GCC扩展。你知道吗

您可以使用(内置)struct模块在Python中再现结果:

import struct
kFinderSig = struct.unpack(">L", "MACS")[0]

或者,只需将值硬编码为0x4d414353,并留下表示“MACS”的注释。你知道吗

相关问题 更多 >

    热门问题