-1

I have a function:

long __stdcall call_DLL(long n, byte s0, byte s1, long(__stdcall *CallBack)(long m, byte s0, byte s1)){
//trying to copy the address of CallBack to another pointer
long *x = &CallBack;
}

I am getting an error:

a value of type "long(__stdcall *CallBack)(long m, byte s0, byte 
s1)"cannot be used to initialize an entity of type "long *"

Anyone know how I can do this?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Shox88
  • 67
  • 1
  • 2
  • 7
  • 1
    Why are you trying to copy a function pointer into a `long*`? – NathanOliver Feb 12 '18 at 17:19
  • You're trying to assign a pointer of type long; with a pointer of a function. I can't do it captain! I don't have the Type! – UKMonkey Feb 12 '18 at 17:19
  • 1
    You are trying to assign a pointer to a function, to a pointer to a `long`. These types are simply not compatible. Why would you want to do this? – Algirdas Preidžius Feb 12 '18 at 17:20
  • 3
    this is a [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). I suppose, the question you actually wanted to ask is "How do I store the pointer to the callback?", your current "problem" cannot be solved – 463035818_is_not_an_ai Feb 12 '18 at 17:25
  • 1
    Anyone knows how you can do WHAT? If you want to assign `a pointer to a function` value to a variable which is `a pointer to long int` then the answer has already been given by your compiler: you _can't_. But if you want to do something else, please specify what you're trying to achieve. – CiaPan Feb 12 '18 at 17:26
  • 3
    Are you using C or C++? You shouldn't have both tags. – Keith Thompson Feb 12 '18 at 17:28

2 Answers2

1

If you really want to save the callback to use it later, you can do:

long (* __stdcall x)(long, byte, byte) = CallBack;

or in C++ you can also use auto for brevity:

auto x = CallBack;

In either case, use it later like

long ret = x(n, s0, s1);

Otherwise, if you want to just call that CallBack, do something like

long x = CallBack(n, s0, s1);
ash108
  • 1,763
  • 1
  • 17
  • 21
0

Rather than initialize a function pointer with an incompatible long *, initialize it with a compatible type. @Algirdas Preidžius

pointer to function (long, byte, byte) returning long.

long __stdcall call_DLL(long n, byte s0, byte s1,
    long (__stdcall *CallBack)(long m, byte s0, byte s1)) {

  // long *x = &CallBack;
  long (__stdcall *x)(long, byte, byte) = CallBack;

  // sample usage of `x`
  return x(n, s0, s1);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256