Having a hard time with C interop (callbacks)


(Rick M) #1

I'm trying to use an API with a complicated callback structure. You pass a struct with a set of pointers to callbacks to a function, along with a pointer to a buffer it fills out. Here's an elided version of the C and Swift code:

  https://pastebin.com/k3VYJTjB

This compiles, but I get a runtime error in the unsafeBitCast() call (mismatched sizes) on line 56.

I realize unsafeBitCast() is dangerous, but I was relying Quinn's post in the Apple Dev Forums suggesting it was the best way to go. However, I'm pretty sure I'm using it incorrectly here.

I can't seem to get Xcode to show me its Swift interpretation of the header file. Sometimes it does, sometimes it doesn't.

But can someone please help me get the pointer conversions right for this?

Thank you very much.

···

--
Rick Mann
rmann@latencyzero.com


(Rick M) #2

Ah, turns out the passed-in type is UnsafePointer<lgs_result_info_t>, and I can just call .pointee on that. That seems to have fixed my issues. Not sure if I'm doing the rest of it wrong, but it's not crashing now, at least.

···

On Apr 18, 2017, at 16:18 , Rick Mann via swift-users <swift-users@swift.org> wrote:

I'm trying to use an API with a complicated callback structure. You pass a struct with a set of pointers to callbacks to a function, along with a pointer to a buffer it fills out. Here's an elided version of the C and Swift code:

  https://pastebin.com/k3VYJTjB

This compiles, but I get a runtime error in the unsafeBitCast() call (mismatched sizes) on line 56.

I realize unsafeBitCast() is dangerous, but I was relying Quinn's post in the Apple Dev Forums suggesting it was the best way to go. However, I'm pretty sure I'm using it incorrectly here.

I can't seem to get Xcode to show me its Swift interpretation of the header file. Sometimes it does, sometimes it doesn't.

But can someone please help me get the pointer conversions right for this?

Thank you very much.

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com