int main(int argc, char *argv[])
{
strncpy(argv[1], "fookBar", 7);
return NSApplicationMain(argc, (const char **)argv);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSArray *args = [[NSProcessInfo processInfo] arguments];
}
the args array in applicationDidFinishLaunching doesnt show the chages made to argv[1] in main. ?. Why ?
According to the apple doc
"NSApplicationMain itself ignores the argc and argv arguments. Instead, Cocoa gets its arguments indirectly via _NSGetArgv, _NSGetArgc, and _NSGetEnviron (see )."
Thats the reason why even when you make changes to the argv in main, it does not reflect in applicationDidFinishLaunching
Related
I'm creating a small C-program and would like a char pointer array holding only the arguments the executable was started with.
Currently this code also outputs all environment variables:
int main (int argc, const char * argv[]) {
while(argv) {
NSLog(#"Parameter %s\n", *argv);
argv++;
}
}
Instead of doing the cycle the way you do, use argc. The size of argv array is argc, with the first value argv[0] being how the name of the program being executed.
int main (int argc, const char * argv[]) {
for (int i = 1; i < argc; ++i) {
NSLog(#"Parameter %s\n", argv[i]);
}
}
Your code is also dumping the environment variables because they are supplied as an additional parameter after argv. In fact you are accessing memory out of bounds for argv and it is pure luck this works.
Change while(argv) to while(*argv). That will give you just the arguments.
main() is actually called like this main(int argc, char **argv, char **environ)
What is happening is you are going past argv and into environ. This behavior
is undefined should not be relied on. Your code, as it is, will also keep on going past environ
and won't stop, you'll be printing garbage.
You can, of course, do it the other way:
for(int i = 0; i < argc; i++) {
NSLog(#"Parameter %s\n", argv[i]);
}
argv[0] contains the program name, the rest are the arguments.
I wrote a small CLI program to delete specific Safari cookies for me. Functionally it's fine, but it's throwing up warnings about objects being "autoreleased with no pool in place". My project has ARC enabled, hence why I don't have any autorelease pools.
Here's my code:
// NSLog replacement from http://stackoverflow.com/a/3487392/1376063
void IFPrint (NSString *format, ...) {
va_list args;
va_start(args, format);
fputs([[[NSString alloc] initWithFormat:format arguments:args] UTF8String], stdout);
fputs("\n", stdout);
va_end(args);
}
int main(int argc, const char * argv[])
{
NSString *urlSearchString;
if (argc > 1) {
urlSearchString = [[NSString alloc] initWithUTF8String:argv[1]];
}
else {
IFPrint(#"No URL provided, quitting.");
return 1;
}
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSString *filterString = [[NSString alloc] initWithFormat:#"domain ENDSWITH '%#'", urlSearchString];
NSPredicate *filter = [NSPredicate predicateWithFormat:filterString];
NSArray *matchedCookies = [cookieStorage.cookies filteredArrayUsingPredicate:filter];
for (int i = 0; i < matchedCookies.count; i++) {
[cookieStorage deleteCookie:[matchedCookies objectAtIndex:i]];
}
IFPrint(#"Removed %li cookies", matchedCookies.count);
return 0;
}
The message I get is:
objc[15502]: Object 0x107b2bf00 of class NSThread autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
Which appears in the Xcode debugger or when running the release binary directly (slight digression: shouldn't these messages be stripped out of the "release" build?). The line that causes it seems to be:
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
Similarly, if I run it without passing an argument, I get a similar message:
objc[15630]: Object 0x100114ed0 of class __NSCFString autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[15630]: Object 0x100114f80 of class __NSCFData autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
Which appears to come from the IFPrint function I'm using (however this doesn't show up when I use the IFPrint when I provide a proper argument).
I'm a bit out of my depth here, can anyone show me where (and how) I've gone wrong?
ARC still requires an autorelease pool. Methods like [NSPredicate predicateWithFormat:filterString] continue to release an autoreleased object (though you no longer need to concern yourself all that much since ARC handles it). Furthermore the internal implementation of any library method you call may create arbitrarily many autoreleased objects while running.
You should wrap your code in an autorelease pool via the #autoreleasepool mechanism.
Wrap the entire body of main with #autoreleasepool like so:
int main(int argc, const char * argv[])
{
#autoreleasepool
{
// your code
}
}
All you need to do is add an autoreleasepool in your main.
int main(int argc, const char * argv[])
{
#autoreleasepool
{
//Your code
}
}
I have created a Command line tool application ( Xocde --> New App --> Command line tool) and its running without any problem,
Now i want to run it through terminal and pass some command line argument, something like this
int main(int argc, const char * argv[])
{
std::cout << "got "<<argc<<" arguments";
for ( int i = 0; i<argc;i++){
std::cout << "argument:"<<i<<"= "<<argv[i];
}
//// some other piece of code
}
if i type on the terminal
>open VisiMacXsltConverter --args fdafsdfasf i am getting output
got 1 argumentsargument:0= /Applications/VisiMacXsltConverte
I want to know through command line what is the way to pass the argument
If you use just one - (hyphen) those values will go into a volatile UserDefaults dictionary (will also override other keys for the life of the process).
./program -Param 4
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSLog(#"param = %#", [[NSUserDefaults standardUserDefaults] valueForKey:#"Param"]);
}
return 0;
}
or you can just pass these in how ever you want and use the following which will give you an NSArray of NSStrings.
[[NSProcessInfo processInfo] arguments];
Why you want to run it with open?
I would run it with (if you have it in you $PATH you should omit the './'):
./VisiMacXsltConverter arg1 arg2 arg3 arg4
Hope I have not misunderstood you question.
Apologies if this is a stupid/easy question, but still getting used to everything in Mac land.
Dave was kind enough to answer a question for me here:
Modify NSEvent to send a different key than the one that was pressed
which resulted in the following code, which works great:
#import <Cocoa/Cocoa.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//0x0b is the virtual keycode for "b"
//0x09 is the virtual keycode for "v"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x0B) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x09);
}
return event;
}
int main(int argc, char *argv[]) {
//return NSApplicationMain(argc, (const char **)argv);
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
if (!eventTap) {
NSLog(#"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
Thanks to some helpful error messages (can't have two "main" methods) I figured out that I had to put this code in the main.m file (right?). That means I'm overwriting the default method:
int main(int argc, char *argv[]) {
return NSApplicationMain(argc, (const char **)argv);
}
Which means none of my other Objective-C based code is firing. But if I uncomment that bit (or make any other attempt to call NSApplicationMain) then the main.m run loop is what doesn't run.
I imagine this is fairly simple for a seasoned Mac guy, but I'm having a hard time wrapping my head around it. Thanks.
I don't see any reason why the same idea won't work elsewhere in your code. Can you put it in the -applicationDidFinishLaunching: method of your app delegate? If you do, you will not need the CFRunLoop() call, since the run loop will already be running. Nor will you need the autorelease pool bit.
Here's my code:
#import <Foundation/Foundation.h>
void PrintPathInfo() {
const char *path = [#"~" fileSystemRepresentation];
NSLog(#"My home folder is at '%#'", path);
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
PrintPathInfo();
[pool drain];
return 0;
}
And here's my problem:
Program received signal: “EXC_BAD_ACCESS”.
I really think the problem is my NSLog but I don't know how to solve it.
Could someone help me please? Thanks!
path is not an NSString, which is why that crashes. %# in a formatting string expects an object, and asks it for a description to get a string to print... because you are using a C style string, you need to use the standard C string formatters OR convert the const char * back to an NSString using the initWithCString:encoding: class method of NSString.
Staying with a const char *, you can use:
NSLog(#"My home folder is at '%s'", path);
which would work.
%# is for objects. (Like NSString). for const char* you will want the good old %s from the c's printf format codes.
See http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
For the format specifies and their meanings