function-pointers

Run-Time Check Failure #0 loading QueryFullProcessImageName from kernel32.dll

本秂侑毒 提交于 2019-11-26 21:38:19
问题 I have an application that needs to run both on WinXP and Vista64. My program requires QueryFullProcessImageName() to work on Vista but not on XP. I try to load QueryFullProcessImageName() (instead of linking statically) via the kernel32.dll so that the same executable can run on both WinXP and Vista. The code that loads it is: //only gets called on vista bool LoadQueryFullProcessImageName() { HMODULE hDLL = LoadLibrary("kernel32.dll"); if (!hDLL) return(0); //Now use pointer to get access to

How define an array of function pointers in C

本秂侑毒 提交于 2019-11-26 21:25:31
I've a little question. I'm trying to define an array of function pointers dynamically with calloc . But I don't know how to write the syntax. Thanks a lot. The type of a function pointer is just like the function declaration, but with "(*)" in place of the function name. So a pointer to: int foo( int ) would be: int (*)( int ) In order to name an instance of this type, put the name inside (*), after the star, so: int (*foo_ptr)( int ) declares a variable called foo_ptr that points to a function of this type. Arrays follow the normal C syntax of putting the brackets near the variable's

Pointers to members representations

爱⌒轻易说出口 提交于 2019-11-26 21:17:24
问题 I'm trying to make some callbacks from member functions and everything was ok until I tried to use a template class derived from 2 classes as callback object when I got the following error: error C2440: 'reinterpret_cast' : Pointers to members have different representations; cannot cast between them This thing signaled me that member function pointers have different representations(doh!) What are these representations? What is the difference between them? 回答1: Danny Kalev explains this quite

C++: Function pointer to functions with variable number of arguments

我们两清 提交于 2019-11-26 21:14:10
问题 I'm trying to figure out a way of how to be able to assign a function pointer to functions with different number of arguments. I have a while loop which takes a number of different functions as a conditional statement, so instead of writing multiple while loops with exactly the same code inside I'd like to have one with a function pointer. All the functions are of format bool f(...) . I think some code will best illustrate what I mean: int a, b, c, d; MyClass* my_class; typedef bool (MyClass:

Do function pointers need an ampersand [duplicate]

耗尽温柔 提交于 2019-11-26 20:47:43
问题 This question already has answers here : Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'? (2 answers) Closed 2 years ago . In C/C++, if I have a the following functions: void foo(); void bar(void (*funcPtr)()); Is there a difference between these two calls: bar(foo); bar(&foo); ? 回答1: No, there is no difference, since function can be implicitly converted to pointer to function. Relevant quote from standard (N3376 4.3/1). An lvalue of function type T

Call c++ function pointer from c#

坚强是说给别人听的谎言 提交于 2019-11-26 20:24:44
问题 Is it possible to call a c(++) static function pointer (not a delegate) like this typedef int (*MyCppFunc)(void* SomeObject); from c#? void CallFromCSharp(MyCppFunc funcptr, IntPtr param) { funcptr(param); } I need to be able to callback from c# into some old c++ classes. C++ is managed, but the classes are not ref classes (yet). So far I got no idea how to call a c++ function pointer from c#, is it possible? 回答1: dtb is right. Here a more detailed example for Marshal

When is an array name or a function name 'converted' into a pointer ? (in C)

烈酒焚心 提交于 2019-11-26 20:14:09
1) Misconception : Whenever an array is declared in C language, a pointer to the first element of the array is created (the name of the array) implicitly. (Is it? I don't think so!) The first two lines of this page (though I am not sure about the correctness of the information) state the same. As we have seen, when we declare an array, a contiguous block of memory is allocated for the cells of the array and a pointer cell (of the appropriate type) is also allocated and initialized to point to the first cell of the array. But when I output the address contained in that pointer and the address

Why the size of a pointer to a function is different from the size of a pointer to a member function?

穿精又带淫゛_ 提交于 2019-11-26 19:51:12
问题 Isn't a pointer just an address? Or I'm missing something? I tested with several types of pointers: pointers to any variables is the same (8B on my platform) pointers to functions are the same size, as pointers to variables (8B again) pointers to functions with different parameters - still the same (8B) BUT pointers to member functions are bigger - 16B on my platform. Three things: Why are pointers to member functions bigger? What more information do they need? As far as I know, the standard

Alternative to c++ static virtual methods

拟墨画扇 提交于 2019-11-26 19:34:51
问题 In C++ is not possible to declare a static virtual function, neither cast a non-static function to a C style function pointer. Now, I have a plain ol' C SDK that uses function pointers heavily. I have to fill a structure with several function pointers. I was planning to use an abstract class with a bunch of static pure virtual methods, and redefine them in derived classes and fill the structure with them. It wasn't until then that I realized that static virtual are not allowed in C++. Also

generic member function pointer as a template parameter

这一生的挚爱 提交于 2019-11-26 19:10:02
问题 Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<"f"<<endl; } virtual void ff(){ cout<<"ff"<<endl; } }; #define call_mem_fn(object, ptr) ((object).*(ptr)) template<R (C::*ptr_to_mem)(Args...)> void proxycall(C& obj){ cout<<"hello"<<endl; call_mem_fn(obj, ptr_to_mem)(); } int main(){ hello obj; proxycall<&hello::f>(obj); } Of course this won't compile at line 16, because the compiler doesn't know what R , C and Args , are. But there's another