Passing objc block to function - objective-c

have been strugling with this over 2 days, I am not very skilled in C. So, have an objc function mapped to C function with the following syntax
extern int32_t createWallet(void (*fn)(int32_t handle, int32_t errCode)
but dont know how to pass a block like function. Have been trying to pass
void (^ createWalletCallback)(int32_t t, int32_t e) = NULL;
createWalletCallback = ^void(int32_t t, int32_t e){
/// some code here
}
but no success. Could you pls at least point me what to change? Thanks

This seems to be a duplicate of Is there a way to wrap an ObjectiveC block into function pointer?, where the advice is "don't do it".
Instead, can you not use a plain C function pointer? Define a function
void createWalletCallback(int32_t t, int32_t e) {
// some code here, maybe referencing global variables
// (including a semaphore, if other code needs to wait on the response)
}
and then just call
createWallet(&createWalletCallback);

Related

(C++/CLI) How to get callbacks from Native Code to Managed Code in C++ CLI?

RANT-BEGIN
Before jumping right into already answered band wagon, please read this paper about SE outdated answers https://ieeexplore.ieee.org/document/8669958
Things changes after a time, and I am afraid Computer science is one of the most if not the most field out there where APIs and Interfaces change radically very very fast. Needless to say that a solution that might worked last month might not after latest feature added to a platform/framework. I humbly request you to not mark this question as answered with decade old post when many mainstream things did not even existed. If you dont know latest solution dont bother about it and leave question for someone else who might.
For a community representative of Computer Science where innovations is everyday thing, it is very toxic, new comer unfriendly and conservative.
END-RANT
This question has already been answered by me and will be accepted tomorrow (SE policy). Thank you for your interest.
Many times you have function pointers in unmanaged context which are called by some kind of events, We will see how it can be achieved with Top-Level Functions and also with member functions of a managed class.
Again, Please dont mark it as answered by linking to a decade old posts.
PS:
So many edits due to unstable internet in third world country, yeah bite me!
unmanaged.cpp
#pragma unmanaged
// Declare an unmanaged function type that takes one int arguments and callbacks
// our function after incrementing it by 1
// Note the use of __stdcall for compatibility with managed code
// if your unmanaged callback uses any other calling convention you can
// UnmanagedFunctionPointerAttribute (check msdn for more info) on your delegate
typedef int(__stdcall* ANSWERCB)(int);//Signature of native callback
int TakesCallback(ANSWERCB fp, int a) {
if (fp) {
return fp(a+1);//Native Callback
}
// This code will be executed when passed without fp
return 0;
}
#pragma managed
managed.cpp
using namespace System;
using namespace System::Runtime::InteropServices;
namespace Callbacks {
// Following delegate is for unmanaged code and must match its signature
public delegate void MyNativeDelegate(int i);
// This delegate is for managed/derived code and ideally should have only managed parameters
public delegate void MyManagedDelegate(int i);
public ref class TestCallback {// Our demo Managed class
private:
GCHandle gch;// kept reference so that it can be freed once we are done with it
void NativeCallbackListener(int i);//unmanaged code will call this function
public:
void TriggerCallback(int i); // Its here for demo purposes, usually unmanaged code will call automatically
event MyManagedDelegate^ SomethingHappened;//plain old event
~TestCallback();//free gch in destructor as its managed.
};
};
void Callbacks::TestCallback::NativeCallbackListener(int i) {
// Callback from Native code,
// If you need to transform your arguments do it here, like transforming void* to somekind of native structure.
// and then pass SomethingHappened::raise with Managed Class/Struct
return SomethingHappened::raise(i); // similar to SomethingHappened.Invoke() in c#
}
void Callbacks::TestCallback::TriggerCallback(int i)
{
MyNativeDelegate^ fp = gcnew MyNativeDelegate(this, &TestCallback::NativeCallbackListener);
// use this if your nativecallback function is not a member function MyNativeDelegate^ fp = gcnew MyNativeDelegate(&NativeCallbackListener);
gch = GCHandle::Alloc(fp);
IntPtr ip = Marshal::GetFunctionPointerForDelegate(fp);
ANSWERCB cb = static_cast<ANSWERCB>(ip.ToPointer());// (ANSWERCB)ip.ToPointer(); works aswell
// Simulating native call, it should callback to our function ptr NativeCallbackListener with 2+1;
// Ideally Native code keeps function pointer and calls back without pointer being provided every time.
// Most likely with a dedicated function for that.
TakesCallback(cb, i);
}
void Callbacks::TestCallback::~TestCallBack() {
gch.Free();//Free GCHandle so GC can collect
}
implementation.cpp
using namespace System;
void OnSomethingHappened(int i);
int main(array<System::String^>^ args)
{
auto cb = gcnew Callbacks::TestCallback();
cb->SomethingHappened += gcnew Callbacks::MyManagedDelegate(&OnSomethingHappened);
cb->TriggerCallback(1);
return 0;
}
void OnSomethingHappened(int i)
{
Console::WriteLine("Got call back with " + i);
}

Use of THIS-> in C/C++ [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm going through a tutorial on audio for the iphone and it uses C/C++. I'm not familiar with the use of THIS->. It seems to refer to a pointer to global variable. Here is the tutorial - iPhone Core Audio Part 3 – Audio Callback.
The statement I am trying to understand is the THIS-> part of the statement:
// Pass in a reference to the phase value, you have to keep track of this
// so that the sin resumes right where the last call left off
float phase = THIS->sinPhase;
The tutorial indicate that THIS-> is used to get a to access AudioController variables. It seems that sinPhase is global variable.
Please explain why "phase" reference is created instead of just referring directly to the global variable "sinPhase". Keep in mind I am an objective C programming trying to understand this C/C++ code.
In this example, THIS is not a reference to a global variable; it is defined above in the function, as a cast of the void pointer inRefCon:
static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
// Get a reference to the object that was passed with the callback
// In this case, the AudioController passed itself so
// that you can access its data.
AudioController *THIS = (AudioController*)inRefCon;
This is a fairly common pattern in C; in order to pass a callback in to some API, so that it can later call your code, you pass both a function pointer and a void pointer. The void pointer contains whatever data your function pointer will need to operate on. Within your callback, you will need to cast it back to a pointer to the actual type, so you can access the data within it. In this case, the author of the example is naming that cast pointer THIS, probably to make this look more object-oriented, even though this is just C and THIS has no special meaning.
You ask why they assign it to a local variable rather than just using THIS->sinPhase everywhere. There's no reason you couldn't use THIS->sinPhase everywhere; they likely just assigned it to a local variable phase to save on typing. There's a small chance that the optimizer could do a better job on a local variable than on one passed in via a pointer, because it can make more assumptions about the local variable (in particular, it can assume that no one else is updating it at the same time). So the loop might run slightly faster using a local variable, though I wouldn't be certain without testing; the most likely reason is just to save typing and make the code more readable.
Here's a simplified example of how a callback API like this works; hopefully this should make it easier to understand how a callback API works, without trying to understand the rest of what's going on in Core Audio at the same time. Let's say I want to write a function that will apply a callback to an integer 10 times. I might write:
int do_ten_times(int input, int (*callback)(int)) {
int value = input;
for (int i = 0; i < 10; ++i) {
value = callback(value);
}
return value;
}
Now I could call this with different functions, like the following add_one() or times_two():
int add_one(int x) {
return x + 1;
}
int times_two(int x) {
return x * 2;
}
result = do_ten_times(1, add_one);
result = do_ten_times(1, times_two);
But say I want to be able to add or multiply by different numbers; I could try writing one function for each number that you wanted to add or multiply by, but then you would run into a problem if the number wasn't fixed in the code, but was based on input. You can't write one function for each possible number; you are going to need to pass a value in. So let's add a value to our callbacks, and have do_ten_times() pass that value in:
int do_ten_times(int input, int (*callback)(int, int), int data) {
int value = input;
for (int i = 0; i < 10; ++i) {
value = callback(value, data);
}
return value;
}
int add(int x, int increment) {
return x + increment;
}
int times(int x, int multiplier) {
return x * multiplier;
}
result = do_ten_times(1, add, 3);
result = do_ten_times(1, times, 4);
But what if someone wants to write a function that varies by something other than an integer? For instance, what if you want to write a function that will add different numbers depending on whether the input is negative or positive? Now we need to pass two values in. Again, we could extend our interface to pass in two values; but we will eventually need to pass in more values, values of different types, and the like. We notice that do_ten_times really doesn't care about the type of the value we're passing in; it just needs to pass it to the callback, and the callback can interpret it however it likes. We can achieve this with a void pointer; the callback then casts that void pointer to the appropriate type to get the value out:
int do_ten_times(int input, int (*callback)(int, void *), void *data) {
int value = input;
for (int i = 0; i < 10; ++i) {
value = callback(value, data);
}
return value;
}
int add(int x, void *data) {
int increment = *(int *)data;
return x + increment;
}
int times(int x, void *data) {
int multiplier = *(int *)data;
return x * multiplier;
}
struct pos_neg {
int pos;
int neg;
};
int add_pos_neg(int x, void *data) {
struct pos_neg *increments = (struct pos_neg *)data;
if (x >= 0)
return x + increments->pos;
else
return x + increments->neg;
}
int i = 3;
result = do_ten_times(1, add, &i);
int m = 4;
result = do_ten_times(1, times, &m);
struct pos_neg pn = { 2, -2 };
result = do_ten_times(-1, add_pos_neg, &pn);
These are all, of course, toy examples. In the Core Audio case, the callback is used to generate a buffer of audio data; it is called every time the audio system needs to generate more data in order to keep playing smoothly. The information passed via the void *inRefCon is used to track how exactly where in the sine wave you have gotten to in the current buffer, so the next buffer can pick up where the last one left off.
If it's not declared in scope (i.e. local variable declared in that context), I see two options:
either it's a define, and it actually refers to the instance: #define THIS this.
either a global variable, as you suggested.
THIS doesn't have any inherent meaning in C++, so it can be either. Whichever it is though, it's pretty awful.
The reason it's copied in a different variable, in case it's a global, and not used directly, can be either for clarity or to not accidentally modify it.

How to pass a objective-c method to c function as callback?

I've already read several articles about this question, but none of them solve my problem.
Briefly, in my NSApplicationDelegate, I called a function pcap_loop, pcap_loop needs a function pointer as callback, the callback signature is
void got_packet(u_char *, const struct pcap_pkthdr *, const u_char *)
I want to pass one of the NSApplicationDelegate's method as callback, so I can use these data in my app, somebody suggest me using "SEL + IMP", but I can't pass the self to callback, also somebody suggest "block", not work.
So, any idea?
Thanks!
The essence of what you require can be found in this answer.
In your case you are calling pcap_loop:
int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
where pcap_handler is:
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
Now the last argument to pcap_loop, user, is passed as the first argument to got_packet, args and can be anything you wish. This corresponds to the argument cb_arg/anon in the previous answer. Now just follow the previous answer, passing a block which invokes your NSApplicationDelegate method as user and the C function to invoke that block as callback.

Is there a way to wrap an ObjectiveC block into function pointer?

I have to provide a C-style callback for a specific C library in an iOS app. The callback has no void *userData or something similar. So I am not able to loop in a context. I'd like to avoid introducing a global context to solve this. An ideal solution would be an Objective-C block.
My question: Is there a way to 'cast' a block into a function pointer or to wrap/cloak it somehow?
Technically, you could get access to a function pointer for the block. But it's totally unsafe to do so, so I certainly don't recommend it. To see how, consider the following example:
#import <Foundation/Foundation.h>
struct Block_layout {
void *isa;
int flags;
int reserved;
void (*invoke)(void *, ...);
struct Block_descriptor *descriptor;
};
int main(int argc, char *argv[]) {
#autoreleasepool {
// Block that doesn't take or return anything
void(^block)() = ^{
NSLog(#"Howdy %i", argc);
};
// Cast to a struct with the same memory layout
struct Block_layout *blockStr = (struct Block_layout *)(__bridge void *)block;
// Now do same as `block()':
blockStr->invoke(blockStr);
// Block that takes an int and returns an int
int(^returnBlock)(int) = ^int(int a){
return a;
};
// Cast to a struct with the same memory layout
struct Block_layout *blockStr2 = (struct Block_layout *)(__bridge void *)returnBlock;
// Now do same as `returnBlock(argc)':
int ret = ((int(*)(void*, int a, ...))(blockStr2->invoke))(blockStr2, argc);
NSLog(#"ret = %i", ret);
}
}
Running that yields:
Howdy 1
ret = 1
Which is what we'd expect from purely executing those blocks directly with block(). So, you could use invoke as your function pointer.
But as I say, this is totally unsafe. Don't actually use this!
If you want to see a write-up of a way to do what you're asking, then check this out:
http://www.mikeash.com/pyblog/friday-qa-2010-02-12-trampolining-blocks-with-mutable-code.html
It's just a great write-up of what you would need to do to get this to work. Sadly, it's never going to work on iOS though (since you need to mark a page as executable which you're not allowed to do within your app's sandbox). But nevertheless, a great article.
If your block needs context information, and the callback does not offer any context, I'm afraid the answer is a clear no. Blocks have to store context information somewhere, so you will never be able to cast such a block into a no-arguments function pointer.
A carefully designed global variable approach is probably the best solution in this case.
MABlockClosure can do exactly this. But it may be overkill for whatever you need.
I know this has been solved but, for interested parties, I have another solution.
Remap the entire function to a new address space. The new resulting address can be used as a key to the required data.
#import <mach/mach_init.h>
#import <mach/vm_map.h>
void *remap_address(void* address, int page_count)
{
vm_address_t source_address = (vm_address_t) address;
vm_address_t source_page = source_address & ~PAGE_MASK;
vm_address_t destination_page = 0;
vm_prot_t cur_prot;
vm_prot_t max_prot;
kern_return_t status = vm_remap(mach_task_self(),
&destination_page,
PAGE_SIZE*(page_count ? page_count : 4),
0,
VM_FLAGS_ANYWHERE,
mach_task_self(),
source_page,
FALSE,
&cur_prot,
&max_prot,
VM_INHERIT_NONE);
if (status != KERN_SUCCESS)
{
return NULL;
}
vm_address_t destination_address = destination_page | (source_address & PAGE_MASK);
return (void*) destination_address;
}
Remember to handle pages that aren't required anymore and note that it takes a lot more memory per invocation than MABlockClosure.
(Tested on iOS)

SSH Keyboard Interactive authentication

I currently try to extend an libssh2 Wrapper in Objective-C.
I'm trying to implement the libssh2_userauth_keyboard_interactive method. My problem is the response callback.
I found this implementation on the net that bypasses the "real" interactivity and uses the actual password to make the authentication possible:
int error = libssh2_userauth_keyboard_interactive(session, [username UTF8String], &kbdCallback);
static void kbdCallback (const char *name, int name_len, const char *instruction, int instruction_len, int num_prompts, const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, void **abstract)
{
responses[0].text = (char *)[password UTF8String]; // resp. (char *)[#"test" UTF8String]
responses[0].length = strlen([password UTF8String]); // resp. (char *)[#"test" UTF8String]
}
One of my problems is to access the instance variable password within the static void call and my other problem is that I get SIGABRT when I try to call the method (I used a fixed string to test if that works).
Is there any possibility to get that working ?!
Julian
kbdCallback is not actually a method, it's a function - you can tell a couple of ways - there's no - or + in front of it, no parentheses around the return type, and also methods cannot be static. So, due to it being a function and not a method, there is no object associated with it, and no self pointer; thus you cannot get to any instance variables directly. There's a couple of ways to solve this I suppose; you could have a static instance of your object that the function could get the password from, or if there's some way to pass a context pointer to be used in the callback you might be able to pass an object in that way.
Regarding your SIGABRT, can you say which line exactly that happens on, and what the values of the arguments that you're using are? It's not clear from your question.
http://comments.gmane.org/gmane.network.ssh.libssh2.devel/4163
Cause: malloc-in-EXE-free-in-DLL under Win32.
Fix: Use custom free/malloc/realloc functions. Add below
static void *my_alloc(size_t count, void **abstract) { return malloc(count);}
static void my_free(void *ptr, void **abstract) { free(ptr);}
static void *my_realloc(void *ptr, size_t count, void **abstract){ return realloc(ptr, count);}
And replace
libssh2_session_init();
with
libssh2_session_init_ex(my_alloc, my_free, my_realloc, NULL);