Performing PInvoke based on a .h file only - pinvoke

Is it possible to do P/I if I only have the .header interface file with methods exposed?

If you mean code and compile your PInvoke call, yes you can do it with the header only.
Of course if you want to run it you'll need the DLL too.

Related

Assigning method from h file

Hi I am new to the object-c and this might be silly question. I studied that I have to assign the name of the method on the header file(.h file) before I make a logic in m file. But I found some of examples assigning method only in the m file not from h file. And it works ok. I'm slightly confused what to follow now. Please explain me the difference.
All methods that shall be publicly available in your project go to the .h-file. If you want the method to be kind of private, don't add it to the h.-file but define it in the .m-file.
The newest versions of Xcode include a compiler that allows you to skip the declaration in the header file if you just are going to use the method in the same .m file in which it is defined.
That is probably what you have seen.
The same newer versions of Xcode also allow you to declare ivars in the .m file, which also contributes to simpler header files and a higher degree of locality in the implementation files.

Mixing C++ and Objective C

Where can i find a concrete document or a dos and donts documentation on using C++ with Objective-C?
Apple seems to have removed that document from their website and i am all puzzled with collating bits of information from blogs and questions posted here.
Anyone can guide about the same.
When do we use .mm file, while mixing syntax or while using an object in .m file which belongs to a C++ class ?
While passing objects between functions belonging to two different language like passing objective-c object to a function in cpp file is it necessary to collect it in void * or can I use (ObjectiveC inteface)*?
You need to use Objective-C++ whenever you are either #include/#importing or directly writing both Objective-C and C++ code in the same file. It's usually obvious with explicit code; the #includes are often less so, and you need to take care to avoid "leaking" one of the languages into too much of the other. Your example of #importing a C++ header file is clear-cut: you can only do that with Objective-C++. Note that if your Cplusplus was a struct type, you could forward-declare it as such instead of #importing a C++ header.
If you do this in a header, it will "poison" the header to only work in that mode. You'll need to actively avoid this, or your whole project will soon end up with only .mm files. I have documented some techniques in this article and previously, in this earlier article. For newer versions of Objective-C, you can also add ivars to classes in category extensions. This means you can define C++-typed ivars in your .mm file, not the header, which allows .m files to #import it.
For your second question (Please only ask one question at a time): the id type is defined in the objc.h header file in terms of C and thus will work in both Objective-C and C++. Likewise, the Objective-C runtime API is exposed in terms of C functions, which will work from C++, too. If you actually want to send messages and access properties on Objective-C objects with the canonical syntax from C++ code, you'll need to switch that file to Objective-C++.
Use .mm files when you have a c++ syntax in your code or when including file(s) which contain c++ code.
Mixing C++ with objective-c may be a bit confusing but if you think pointer-wise than it is not a big deal. Treat C++ object instance methods as you would in C++ and the same goes for objective c objects.

Create frameworks for iOS bundled with unity data and its library

This is regarding creating the framework in iOS, as I have a bundle of unity which I want to create a framework, with data with-holding and linking library from Unity as libiPhone-lib.a. So without adding any library in the bundle target, the compilation works fine, if I include libiPhone-lib.a file, it generates a warning as:
warning: implicit declaration of function 'UnitySendMessage'
The UnitySendMessage is a function which is being called from the dedicated libiPhone-lib.a framework.
Any suggestions regarding this concern will be really appreciated.
Thanks.
This error means that, in the file where it occurred, UnitySendMessage was called without the compiler having seen a declaration for the function. You need to edit the file and #import the header that contains UnitySendMessage (or #include if it is a .c file)
.
The Unity folks seem to have neglected to include a header declaring this function. You can either declare it yourself, or just ignore the warning.

Is there a generic method to test for an #imported file in Objective-C

I'm working on an extension of Core Data functionality. I've got a block of code where I'd like to test if a user's NSApplicationDelegate implements the templated managedObjectContext accessor. But I don't want to require the AppKit framework or NSApplication (I might use the functionality in command-line applications), so I'd like to wrap the block in an #ifdef.
Looking at NSApplication.h, there are #defines for NSAppKit versions (e.g. NSAppKitVersionNumber10_0). I could test for an arbitrary one of those, but that doesn't feel quite right. Is there a generic way in the preprocessor to test whether the current compilation environment includes a framework or specific header?
No, there is not, because:
The C preprocessor does not keep track of the files it has included — it just includes them and moves on
The preprocessor has no concept of "frameworks," per se, and they aren't even brought in until the linking stage anyway
The test for an AppKit version is the idiomatic way to do this sort of thing in the preprocessor.
However, i don't see why you need the preprocessor for this.
BOOL delegateImplementsAccessor = [[[NSClassFromString(#"NSApplication") sharedApplication] delegate] respondsToSelector:#selector(managedObjectContext)];

Dynamic Functions

Ok, well I have sorta of an odd situation. I have a two applications. One is the main application and the other is a helper bundle that is loaded during run time. What I want to do is to call a function defined within the main application from the bundle so that code does not have to be copied over. I have tried setting the header declaration for the function
NSString *TXReadableTime(NSTimeInterval date, BOOL longFormat);
within the helper bundle, but it still fails to compile. This is because one of my selectors is calling the function and the compiler is not finding it within the code. Only the header reference.
So I guess what my real question is, is there a way to have dynamic functions? One that is promised to the compiler, but is handled by a separate process. The helper bundle itself is allocated into memory so it has access to selectors of the main application, but I do not want to rewrite the function into a selector because it would require a lot of work.
Use -bundle_loader linker flag to specify the executable which will load the plugin. See ld man page, another Apple doc, and this informative blog post.