Why the pointer to function gets value 0x00000000? - function

Why proc which is a function pointer gets null value?
EDITED:

My guess would be that the library you loaded doesn't export a function called "StartHook".
If the library is written in C++, which it looks like it is, the function name will be mangled based on its argument types (e.g, to something like _Z9StartHookv). Wrap the definition in extern "C" { ... } to prevent this.

NULL is the documented return value for when the function fails. To get the reason, call GetLastError and look it up here.

Related

Function calling variable output number

I've found myself trying to interface a custom class with builtin functions, and I've encoutered a situation I could only solve with eval, I'd like a "cleaner" way.
Basically, the builtin function is defined as varargout=blabla(varargin)
I defined an overriden function in the custom class, as varargout=blabla(varargin). The function looks like:
function varargout=blabla(varargin)
varargout=blabla(function_of_varargin)
end
The function of varargin transforms it from the custom class to the builtin clas.
But it doesn't work as-is: Basically when the builtin function is called inside the overriden function, it sees only one output parameter (varargout), even if the custom overriden function is called with more than one output parameter.
I solved it by basically calling this :
[varargout{1},varargout{2},...,varargout{nargout}]=blabla(function_of_varargin)
Constructing the LHS with a loop and eval-ing.
Have you tried this:
[varargout{1:nargout}] = blabla(varargin{:})
?

can function pointer point to member function of STL container

For example,
if I have
deque<int> a;
and I want to use a function pointer to a.front() , a.back() and a.push_front(), a.push_back(), can I accomplish that? If yes, how?
Pointers to member functions are a different type of function pointer than regular pointers, due to needing to pass this. The best solution is to implement a wrapper function as such:
template<typename T>
T deque_front(std::deque<T> *q) {
return q->front();
}
To call a (non-static) member function, you need an object to call it on (as well as the parameters of the function). This is implemented by having the member function take a (hidden)
extra parameter, this (effectively).
If you're using c++11 you can write a lambda to do this, or use std::bind to bind an instance of a deque and a member function together.

How to pass a value to a function when the value is SWIGTYPE

I have a wrapped function in java like this:
dosomething(SWIGTYPE_sometypeSTRUCT STRUCTtype);
Originally in the C code, the declaration looks so:
dosomething(sometypeSTRUCT* structtype);
How can I pass the SWIGTYPE to the java function.
if I do:
SWIGTYPE_sometypeSTRUCT something = new SWIGTYPE_sometypeSTRUCT ();
it won't work.. It will work only if I set somthing = null.
The problem is that SWIG hasn't seen a definition of sometypeSTRUCT - as far as it knows the pointer your dosomething function takes is an opaque type.
When SWIG doesn't know details it can't wrap it meaningfully and the only safe thing to do is let you do what you would be able to do in C with such a type.
To fix this you probably want to use %include to bring in the definition of the struct, or possibly provide a definition within the SWIG interface file.

lua-function as parameter for exported function

Is it possible to send lua-function to a main C++ program like this?
function a()
... -- do something
end
cpp_exported_function(a);
Or better, like this?
cpp_exported_function(function () .... end);
And how do I call it from the main program?
If it is possible - use lua table the same way. I mean exported_function(table);?
Yes - you'd have a C++ function that accepts a luabind::object as a parameter in both those cases. Luabind defines operator[] (for indexing a table) and operator() (for calling a function) for luabind::object for exactly that reason. See the documentation here: http://www.hci.iastate.edu/~rpavlik/doxygen/luabind/docs.html#object

variable names in function definition, call and declaration

I see C books that use the same variable names in the function definition, calling function and declaration. Others use the same variable names in the calling function and in the declaration/prototype but a different one in the definition as in:
void blabla(int something); //prototype
blabla(something) // calling function inside main after something has been initialized to int
void blabla(int something_else) //definition
I have two questions:
What convention is best to use in C?;
Does the convention apply regardless whether a value is being passed "by-value" or if it's being passed by a pointer?
Thanks a lot...
The name used for a function parameter in a function declaration is basically just a comment. It doesn't have any meaning and (as you've noticed) doesn't have to match the function definition. That said, it should be a good descriptive name that tells you what the parameter is for. So why not use the same name in the declaration? If you use a different name and one of the names is better (more descriptive), then you should probably use that name in both places.