ImportC help

Mac OSX specific forum
kermit
New User
New User
Posts: 6
Joined: Fri Aug 31, 2012 5:49 pm

ImportC help

Post by kermit »

Hi,

I am trying to understand how imports work, so I 'm trying to make a sample app. Here 's my code

Code: Select all

OpenWindow(0, 0, 0, 220, 160, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ImportC "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
  CFUserNotificationDisplayNotice(Timeout.i, AlertLevel.i, IconUrl.s, SoundUrl.s, LocalizationUrl.s, Title.s, Message.s, DefBtnTitle.s)
EndImport

CFUserNotificationDisplayNotice(0,1, #NULL$,#NULL$, #NULL$, "test1", "test2", "zzz")
It fails with "Invalid memory access". What am I missing?

Ref: https://developer.apple.com/library/mac ... playNotice
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: ImportC help

Post by wilbert »

You are passing the wrong kind of arguments

SInt32 CFUserNotificationDisplayNotice (
CFTimeInterval timeout,
CFOptionFlags flags,
CFURLRef iconURL,
CFURLRef soundURL,
CFURLRef localizationURL,
CFStringRef alertHeader,
CFStringRef alertMessage,
CFStringRef defaultButtonTitle
);


CFTimeInterval is a double, not an integer and CFURLRef and CFStringRef are Core Foundation objects, not PureBasic strings.
kermit
New User
New User
Posts: 6
Joined: Fri Aug 31, 2012 5:49 pm

Re: ImportC help

Post by kermit »

Thank you very much for your reply wilbert! :!:

If I understand well, the double issue can be solved by replacing .i with .d . But what about the CFURLRef and CFStringRef? :shock:

Ref: https://developer.apple.com/library/ios ... rence.html
kermit
New User
New User
Posts: 6
Joined: Fri Aug 31, 2012 5:49 pm

Re: ImportC help

Post by kermit »

Update: I think I 'm starting to understand CFStringRef and CFURLRef are pointers and by using .d it seems to not break with error.

Nevertheless, I 'm not sure do I pass a pointed for my string to the function? :o

Here 's my latest attempt:

Code: Select all

OpenWindow(0, 0, 0, 220, 160, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ImportC "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
  
  CFStringCreateWithCString(alloc.d, String.s, enc.i)
  
  CFUserNotificationDisplayNotice(Timeout.d, AlertLevel.i, IconUrl.d, SoundUrl.d, LocalizationUrl.d, Title.d, Message.d, DefBtnTitle.d)
  
EndImport

title.d = CFStringCreateWithCString(#Null, "test1", 0)
msg = CFStringCreateWithCString(#Null, "test2", 0)
CFUserNotificationDisplayNotice(0,0, #Null,#Null, #Null, title, msg, #Null)

Repeat
  Event = WindowEvent() 
ForEver
Now, I 'm getting an invalid memory error at CFStringCreateWithCString() :(

But I can't see why, given the definition:
CFStringCreateWithCString

Creates an immutable string from a C string.
CFStringRef CFStringCreateWithCString (
CFAllocatorRef alloc,
const char *cStr,
CFStringEncoding encoding
);
Parameters
alloc

The allocator to use to allocate memory for the new string. Pass NULL or kCFAllocatorDefault to use the current default allocator.
cStr

The NULL-terminated C string to be used to create the CFString object. The string must use an 8-bit encoding.
encoding

The encoding of the characters in the C string. The encoding must specify an 8-bit encoding.
Ref: https://developer.apple.com/library/mac ... 201-F11124

Any ideas? :?:
kermit
New User
New User
Posts: 6
Joined: Fri Aug 31, 2012 5:49 pm

Re: ImportC help

Post by kermit »

Finally!!!!

Issue resolved:

Working code:

Code: Select all

#kCFAllocatorDefault = 0
#kCFStringEncodingMacRoman = 0

ImportC "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
  
  CFStringCreateWithCString(alloc.l, CString, enc.l)
  
  CFUserNotificationDisplayNotice(Timeout.d, AlertLevel.i, IconUrl.l, SoundUrl.l, LocalizationUrl.l, Title.l, Message.l, DefBtnTitle.l)
  
EndImport

title.s = "I 'm the title"
msg.s = "I 'm the message!"

titleRef = CFStringCreateWithCString(#kCFAllocatorDefault, @title, #kCFStringEncodingMacRoman)
msgRef = CFStringCreateWithCString(#kCFAllocatorDefault, @msg, #kCFStringEncodingMacRoman)

CFUserNotificationDisplayNotice(0,1, #Null,#Null, #Null, titleRef, msgRef, #Null)
Thanks for pointing me to the right direction and also props to these posts:
http://www.purebasic.fr/english/viewtopic.php?p=291717
http://www.purebasic.fr/english/viewtop ... 19&t=46111

This is fun! :D :mrgreen:
Post Reply