ruijiexie
ruijiexie

Reputation: 97

c/c++ va_list about hook

    void foo(int fmt, ...)
    {
    }

    //I hook foo

    static void (*original_foo)(int fmt, ...);
    void replaced_foo(int fmt, ...)
    {
      printf("Hooking");
      va_list args;
      va_start(args, fmt);
      //do something
      va_end(args);

//But I want to call the original_foo function, //I do not know how to invoke it ...

    }
    //Hook Function not include ...

    Hook(foo, replaced_foo, (void **)&original_foo);

Upvotes: 2

Views: 1117

Answers (1)

glglgl
glglgl

Reputation: 91119

If you have a corresponding original_foo_v() which takes a va_args, you are lucky: you can use that.

If you don't (such as if you use DbgPrintf() or LStrPrintf() for interfacing with LabVIEW), you'll have to craft something on your own.

Essentially, you'll have to

  • examine the va_list you get,
  • find its stack frame by walking along the stack,
  • allocate as much memory on the stack as you need, assuming that you need the whole area between where the va_list points to and the next stack frame,
  • calling the non-va-aware function.

Of course, you'll have to do this for each and every platform you intend to support...

Good luck and have fun.

Upvotes: 2

Related Questions