Calling an object from a C function

  • I have a C function from which I would like to call a method. I can't
    find any documents the cover how to do this.
  • Hi Gilbert.

    You just have to make sure that your C function is in a .m file (not
    a .c file) and have the proper header included. Then you just call the
    method.

    -Kenny

    On Aug 19, 2008, at 2:53 PM, Gilbert Mackall wrote:

    >
    >
    > I have a C function from which I would like to call a method. I
    > can't find any documents the cover how to do this.
  • On Aug 19, 2008, at 2:53 PM, Gilbert Mackall wrote:

    > I have a C function from which I would like to call a method. I
    > can't find any documents the cover how to do this.

    You call it just like you call any other method. Your C function will
    need to be compiled with the Obj-C compiler however. Easiest way to do
    this is to name the source file with a .m extension.
    --
    David Duncan
    Apple DTS Animation and Printing
  • What is the syntax? For example, how do I invoke the method

    - (int) myMethod: (int) int;

    In object myObject

    from within a C (not Objective C) function and make use of the result?

    In Objective C I would invoke

    [myObject myMethod: myInt];

    Even better, how do I invoke

    - (myObject) myMethod: (myObject *) myObjectArg;

    an make use of the result in a C function?

    On Aug 19, 2008, at 6:52 PM, David Duncan wrote:

    > On Aug 19, 2008, at 2:53 PM, Gilbert Mackall wrote:
    >
    >> I have a C function from which I would like to call a method. I
    >> can't find any documents the cover how to do this.
    >
    > You call it just like you call any other method. Your C function
    > will need to be compiled with the Obj-C compiler however. Easiest
    > way to do this is to name the source file with a .m extension.
    > --
    > David Duncan
    > Apple DTS Animation and Printing

    Charlie Dickman
    <3tothe4th...>
  • On Aug 19, 2008, at 4:28 PM, Charlie Dickman wrote:

    > from within a C (not Objective C) function and make use of the result?
    >
    > In Objective C I would invoke
    >
    > [myObject myMethod: myInt];

    You invoke it exactly the same way. There is no difference. But you
    need to compile as Obj-C.

    int foo(id bar) { [bar foobaz]; }
    --
    David Duncan
    Apple DTS Animation and Printing
  • Example:

    void cFunction(MyObject *myObject) {
        int myInt;
        int result;

        myInt = initializeMyInt();
        result = [myObject myMethod:myInt];
        printf("result:%i", result);
    }

    On Aug 19, 2008, at 4:28 PM, Charlie Dickman wrote:

    > What is the syntax? For example, how do I invoke the method
    >
    > - (int) myMethod: (int) int;
    >
    > In object myObject
    >
    > from within a C (not Objective C) function and make use of the result?
    >
    > In Objective C I would invoke
    >
    > [myObject myMethod: myInt];
    >
    > Even better, how do I invoke
    >
    > - (myObject) myMethod: (myObject *) myObjectArg;
    >
    > an make use of the result in a C function?
    >
    > On Aug 19, 2008, at 6:52 PM, David Duncan wrote:
    >
    >> On Aug 19, 2008, at 2:53 PM, Gilbert Mackall wrote:
    >>
    >>> I have a C function from which I would like to call a method. I
    >>> can't find any documents the cover how to do this.
    >>
    >> You call it just like you call any other method. Your C function
    >> will need to be compiled with the Obj-C compiler however. Easiest
    >> way to do this is to name the source file with a .m extension.
    >> --
    >> David Duncan
    >> Apple DTS Animation and Printing
    >
    > Charlie Dickman
    > <3tothe4th...>
  • did you mean: (im not sure to understand your problem) ???!!!

    #import <Foundation/Foundation.h>

    int foo(int a, int b) {
    return (a + b);
    }

    @interface myObject: NSObject

    -(SInt32)aIntegertFunction:(SInt32)a plus:(SInt32)b;

    @end

    @implementation myObject

    -(SInt32)aIntegertFunction:(SInt32)a plus:(SInt32)b
    {
      return foo(a,b);
    }

    @end

    int main (
      int argc, const char * argv[])
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      myObject *obj = [[myObject alloc] init];

      NSLog(@" %i", [obj aIntegertFunction:10 plus:10]);

      [obj release];

      [pool drain];

      return 0;
    }

    On Tue, Aug 19, 2008 at 4:38 PM, David Duncan <david.duncan...> wrote:
    > On Aug 19, 2008, at 4:28 PM, Charlie Dickman wrote:
    >
    >> from within a C (not Objective C) function and make use of the result?
    >>
    >> In Objective C I would invoke
    >>
    >> [myObject myMethod: myInt];
    >
    >
    > You invoke it exactly the same way. There is no difference. But you need to
    > compile as Obj-C.
    >
    > int foo(id bar) { [bar foobaz]; }
    > --
    > David Duncan
    > Apple DTS Animation and Printing
    >

    --
    -mmw
  • Hi Charlie,
    The format is like any normal C function.

    int MyStaticCFunction(int someArg) {

        //invoke myMethod and return the int the myMethod returns
        return [objcObject myMethod:someArg];
    }

    You wrote:

    What is the syntax? For example, how do I invoke the method

    - (int) myMethod: (int) int;

    In object myObject

    from within a C (not Objective C) function and make use of the result?

    There are no Objective-C functions. Only C functions. There are Objective-C
    methods.

    In Objective C I would invoke

    [myObject myMethod: myInt];

    Even better, how do I invoke

    - (myObject) myMethod: (myObject *) myObjectArg;

    an make use of the result in a C function?

    MyObject * MyStaticCFunction(MyObject *myObjectArg) {

        //assuming MyObject is an Objective C instance
        [myObjectArg myMethod:myObjectArg];
        return myObjectArg;
    }

    Hope this helps,

    Kiel
  • Thanks to all who responded. I must have had a mental block against
    this.

    Now, how do I define things like 'self' and 'super' to a C program?

    On Aug 19, 2008, at 7:38 PM, David Duncan wrote:

    > On Aug 19, 2008, at 4:28 PM, Charlie Dickman wrote:
    >
    >> from within a C (not Objective C) function and make use of the
    >> result?
    >>
    >> In Objective C I would invoke
    >>
    >> [myObject myMethod: myInt];
    >
    >
    > You invoke it exactly the same way. There is no difference. But you
    > need to compile as Obj-C.
    >
    > int foo(id bar) { [bar foobaz]; }
    > --
    > David Duncan
    > Apple DTS Animation and Printing
    >

    Charlie Dickman
    <3tothe4th...>
  • --- On Tue, 8/19/08, Charlie Dickman <3tothe4th...> wrote:

    > Now, how do I define things like 'self' and
    > 'super' to a C program?

    You mean outside of an object? You don't. What would it even mean? There's no concept of "selfness" in a function. If you want there to be a self, you should create an Objective-C method.

    Cheers,
    Chuck
  • hi charlie give your code or something clearer
    what are you trying to do? a C object runtime? or a wrapper obj-c to
    C? (kidding)

    maybe you could have the right answers, when I read you, my feelings
    is that you don't go in the right direction

    On Tue, Aug 19, 2008 at 5:06 PM, Charlie Dickman <3tothe4th...> wrote:
    > Thanks to all who responded. I must have had a mental block against this.
    >
    > Now, how do I define things like 'self' and 'super' to a C program?
    >
    > On Aug 19, 2008, at 7:38 PM, David Duncan wrote:
    >
    >> On Aug 19, 2008, at 4:28 PM, Charlie Dickman wrote:
    >>
    >>> from within a C (not Objective C) function and make use of the result?
    >>>
    >>> In Objective C I would invoke
    >>>
    >>> [myObject myMethod: myInt];
    >>
    >>
    >> You invoke it exactly the same way. There is no difference. But you need
    >> to compile as Obj-C.
    >>
    >> int foo(id bar) { [bar foobaz]; }
    >> --
    >> David Duncan
    >> Apple DTS Animation and Printing
    >>
    >
    > Charlie Dickman
    > <3tothe4th...>
    >

    --
    -mmw
  • On 20 Aug 2008, at 01:06, Charlie Dickman wrote:

    > Now, how do I define things like 'self' and 'super' to a C program?

    Put like this the question doesn't make sense. But maybe this is
    useful: If you have a C function that _conceptually_ is part of an
    object and that needs to access 'self' like the object's methods, then
    you should pass in a pointer to self to those functions (which is
    essentially what happens in method calls, albeit invisibly).

    As a real world example, this is how you would handle the situation if
    you define a C callback function:

    Say you want to scan the contents of a PDFPage. Quartz provides the
    CGPDFScanner that will parse the contents of a PDF page and allows you
    to define callback functions that it calls as it encounters the
    various building blocks of a PDF page:

    You set up a table that will contain the callbacks for the scanner to
    use:
    CGPDFOperatorTableRef table = CGPDFOperatorTableCreate();

    You add the callbacks to the table:
    // End text object:
    CGPDFOperatorTableSetCallback(table, "ET", operator_ET);

    You create the scanner, passing in self as the pointer to data you
    want passed to your callback functions:
    CGPDFScannerRef scanner = CGPDFScannerCreate(contentStream, table,
    self);

    You define the callback function, casting the data pointer to the type
    of your object:
    void operator_ET(CGPDFScannerRef scanner, void *info)
    {
    MyObject *self = info;

    [[self textObjects] addObject:[self currentTextObject]];
    [self setCurrentTextObject:nil];
    }

    You could call  the variable "self" anything else, but "self" is of
    course a very convenient name to use.

    I don't know how this would scale to 'super'. I don't think you can
    pass in a pointer to super, as that is not how the mechanism works.
    While "self" is a variable name "super" is a flag to the compiler
    telling it where to begin searching for the method to perform. That
    wouldn't work outside of the object's context like in a C function.
    You would have to find a way access the superclass's methods within
    the callback function. I suppose it would involve calling
    objc_msgSendSuper or objc_msgSendSuper_stret directly, but that is
    unknown territory to me. I'd be interested myself in how that is done
    if anyone is willing to supply the answer.

    António

    ----------------------------------------------------
    Energy is like a muscle,
    it grows stronger through being used.
    ----------------------------------------------------
  • On Aug 20, 2008, at 4:21 AM, Antonio Nunes wrote:

    > I don't know how this would scale to 'super'. I don't think you can
    > pass in a pointer to super, as that is not how the mechanism works.
    > While "self" is a variable name "super" is a flag to the compiler
    > telling it where to begin searching for the method to perform. That
    > wouldn't work outside of the object's context like in a C function.
    > You would have to find a way access the superclass's methods within
    > the callback function. I suppose it would involve calling
    > objc_msgSendSuper or objc_msgSendSuper_stret directly, but that is
    > unknown territory to me. I'd be interested myself in how that is
    > done if anyone is willing to supply the answer.

    Well, you can try to achieve this result with the Objective-C
    runtime.  The better approach would be to have the C function be a
    very simple wrapper around an Objective-C method (as you illustrated)
    and then have that method invoke 'super' if necessary.

    If you really wanted to do it directly, you could grab the
    implementation for the method, a function pointer, and call through
    that:

    Class theSuperClass = [anObject superclass];
    SEL theSelector = @selector(someMethod);
    if ([theSuperClass instancesRespondToSelector:theSelector])
    {
      IMP f = [theSuperClass instanceMethodForSelector:theSelector];
      f(anObject, theSelector);
    }

    Cheers,
    Ken
  • On 20 Aug 2008, at 11:16, Ken Thomases wrote:

    > Well, you can try to achieve this result with the Objective-C
    > runtime.  The better approach would be to have the C function be a
    > very simple wrapper around an Objective-C method (as you
    > illustrated) and then have that method invoke 'super' if necessary.

    Gosh, there's nothing quite like keeping it simple, is there?! So
    obvious once someone rubs your nose in it. :-)

    To turn your suggestion into code, using the callback to jump straight
    back into the object, passing in any info that the callback provided,
    we could do something like:

    void operator_Tj(CGPDFScannerRef scanner, void *info)
    {
    [(MyObject *)info operator_Tj_WithScanner:scanner];
    }

    or, more generically:

    void myCallbackFunction(<some argument list>, void *myObject)
    {
    [(id)myObject myObjectMethodToHandleThisCallbackWithArg1:arg1
    arg2:arg2 etc:etc];
    }

    Cool!

    António

    ----------------------------------------------------
    There is nothing as strong as real gentleness, and
    there is nothing as gentle as real strength.
    ----------------------------------------------------
previous month august 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Go to today