invalid memory access on api call?
Posted: Mon Jan 23, 2012 4:11 pm
I'm learning about adding login item on MACOS LION.
but these code crash with memory access error, within the line "CFPreferencesSetAppValue(CFStringCreateWithCString(0,pkey$,0),newArrayRef,CFStringCreateWithCString(0,plist$,0))"
what should i do with "newArrayRef"? thanks!
but these code crash with memory access error, within the line "CFPreferencesSetAppValue(CFStringCreateWithCString(0,pkey$,0),newArrayRef,CFStringCreateWithCString(0,plist$,0))"
what should i do with "newArrayRef"? thanks!
Code: Select all
Global plist$ = "loginwindow"
Global pkey$ = "AutoLaunchedApplicationDictionary"
appFileName$ = StringField(ProgramFilename(),1,".app")+".app"
ImportC "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
CFPreferencesSetAppValue(l1,l2,l3)
CFPreferencesCopyAppValue(l1,l2)
CFStringCreateWithCString(l1,s1.s,l2)
CFStringGetCStringPtr(l1,l2)
CFStringGetCString(CFStringRef.L, *StringBuffer, BufferSize.L, CFStringEncoding.L)
CFStringGetTypeID()
CFPreferencesAppSynchronize(l1)
CFGetTypeID(l1)
CFArrayGetTypeID()
CFArrayGetCount(CFArrayRef.l)
CFArrayGetValueAtIndex(CFArrayRef.L, Index.L)
CFDictionaryGetTypeID()
CFDictionaryGetValue(CFDictionaryRef.L, *KeyToFind)
CFDictionaryCreateMutable(l1,l2,l3,l4)
CFDictionaryAddValue(l1,l2,l3)
CFArrayCreateMutableCopy(l1,l2,l3)
CFArrayRemoveValueAtIndex(ArrayRef.l,Index.l)
CFArrayAppendValue(l1,l2)
CFRelease(obj.l)
EndImport
Procedure.l AddLoginItem(AppPath$)
Rtn.l = #False
ArrayRef = CFPreferencesCopyAppValue(CFStringCreateWithCString(0,pkey$,0),CFStringCreateWithCString(0, plist$,0))
If ArrayRef
If CFGetTypeID(ArrayRef) = CFArrayGetTypeID()
newArrayRef = CFArrayCreateMutableCopy(#Null,0,ArrayRef)
If newArrayRef
newDictRef = CFDictionaryCreateMutable(#Null,0,#Null,#Null)
If newDictRef
KeyRef1 = CFStringCreateWithCString(0, "Hide", 0)
KeyRef2 = CFStringCreateWithCString(0, "Path", 0)
CFDictionaryAddValue(newDictRef,KeyRef1,#False)
CFDictionaryAddValue(newDictRef,KeyRef2,CFStringCreateWithCString(0,AppPath$,0))
CFArrayAppendValue(newArrayRef,newDictRef)
CFPreferencesSetAppValue(CFStringCreateWithCString(0,pkey$,0),newArrayRef,CFStringCreateWithCString(0,plist$,0))
CFPreferencesAppSynchronize(CFStringCreateWithCString(0,plist$,0))
Rtn = #True
i = ItemCount
CFRelease(newArrayRef)
CFRelease(newDictRef)
EndIf
CFRelease(ArrayRef)
EndIf
EndIf
EndIf
ProcedureReturn Rtn
EndProcedure
Debug AddLoginItem(appFileName$)