2

GetProcAddress returns a function pointer. Lets suppose we get the address of the function Beep ( its declaration can be found at WinBase.h (when including Windows.h))

BOOL WINAPI Beep(
  _In_  DWORD dwFreq,
  _In_  DWORD dwDuration
);

then the classic code could look something like

typedef BOOL(__stdcall *pbeep)(DWORD , DWORD );
pbeep beep = NULL;
FARPROC fptr = GetProcAddress(Hnd,"Beep");
beep = reinterpret_cast<pbeep>(fptr);
if( beep != NULL ) {
   beep( 440, 1200 ); //this generates a beep for 1.2 secs...
      }

Everything looks good and works. My question:

Is there any way I can avoid the typedef declaration considering the compiler could "somehow" get the function pointer "information" from the already included Beep() declaration from WinBase.h. My goal is to somehow re-use the info (return/parameters/etc) already contained at the already included .h file where the Beep() function is declared w/o having to manually repeat all of that info on a typedef. when doing it for one function it's ok but when the number of functions grows those typedef are really a pain and a big source of errors. Can that be done?

edit; I'm moving to VS 2013 soon but so far still using VS2008 then the idea is doing this w/o C++11

Pat
  • 2,670
  • 18
  • 27

5 Answers5

4

You can make a function to do that in C++11 (or possibly C++03 if you can make Boost.Typeof do your bidding):

template<typename F>
F GetKnownProcAddress(HMODULE hmod, const char *name, F) {
    auto proc = reinterpret_cast<F>(GetProcAddress(hmod, name));
    if (!proc) {/*throw or something*/}
    return proc;
}

int main() {
    auto beep = GetKnownProcAddress(Hnd, "Beep", Beep);
}

If you're willing to use a macro, you can go one step further:

//where GetKnownProcAddressImpl is the above; also consider the usual STRINGIFY
#define GetKnownProcAddress(hmod, func) GetKnownProcAddressImpl(hmod, #func, func);
auto beep = GetKnownAddressProc(Hnd, Beep);
chris
  • 60,560
  • 13
  • 143
  • 205
2

In c++11 you can write

decltype (&Beep) beep_ptr = reinterpret_cast<decltype (&Beep)>GetProcAddress(Hnd,"Beep");

But I don't understand why would you want to do this - if you already have a pointer to a function, why load it manually?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Alex Telishev
  • 2,264
  • 13
  • 15
  • I do not have the pointer; the pointer is returned by GetProcAddress and has to be casted; I want to simplify that casting not having to manually add all the information again. I have the .h declaration and the returned FARPROC pointer; I'm dreamimg of putting those parts together and not typing typedef anymore... – Pat Feb 02 '14 at 23:09
2
#include <windows.h>

int main()
{
    decltype(Beep)* beep = (decltype(Beep)*)GetProcAddress(GetModuleHandle("Kernel32.dll"), "Beep");
    beep(440, 1200);
}
Brandon
  • 22,723
  • 11
  • 93
  • 186
  • And you can use `auto` to make it more concise. `auto beep = (decltype(Beep)*)GetProcAddress(GetModuleHandle("Kernel32.dll"), "Beep");` – Matthew Jul 19 '18 at 15:08
0

Or try:-

typedef BOOL (*pbeep)(DWORD, DWORD);
FARPROC fptr = GetProcAddress(Hnd,"Beep");
((pbeep)fptr)( 440, 1200);
Dallas Clarke
  • 261
  • 2
  • 5
-1
BOOL (__stdcall *beep)(DWORD, DWORD); // beep - pointer to function with parameters (DWORD, DWORD) and result of type bool
(FARPROC)beep = GetProcAddress(Hnd,"Beep");
if( beep != NULL ) {
     beep( 440, 1200 ); //this generates a beep for 1.2 secs...
}
hunt
  • 39
  • 1
  • 2
  • 2
    When posting answers, please add and explanation describing what you changed or added or how it works or something. Even though your answer may be the solution, it's likely that some people who read this don't understand what it means or how it works. – Tim Apr 28 '14 at 11:38