It is currently Sun May 19, 2013 4:29 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: ImportC help
PostPosted: Fri Aug 31, 2012 7:27 pm 
Offline
New User
New User

Joined: Fri Aug 31, 2012 5:49 pm
Posts: 6
Hi,

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

Code:
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


Top
 Profile  
 
 Post subject: Re: ImportC help
PostPosted: Fri Aug 31, 2012 7:36 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1084
Location: Netherlands
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.


Top
 Profile  
 
 Post subject: Re: ImportC help
PostPosted: Fri Aug 31, 2012 8:02 pm 
Offline
New User
New User

Joined: Fri Aug 31, 2012 5:49 pm
Posts: 6
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


Top
 Profile  
 
 Post subject: Re: ImportC help
PostPosted: Fri Aug 31, 2012 9:04 pm 
Offline
New User
New User

Joined: Fri Aug 31, 2012 5:49 pm
Posts: 6
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:
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:
Quote:
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? :?:


Top
 Profile  
 
 Post subject: Re: ImportC help
PostPosted: Fri Aug 31, 2012 9:39 pm 
Offline
New User
New User

Joined: Fri Aug 31, 2012 5:49 pm
Posts: 6
Finally!!!!

Issue resolved:

Working code:
Code:
#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:
viewtopic.php?p=291717
viewtopic.php?f=19&t=46111

This is fun! :D :mrgreen:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye