Use existing function declaration as a type, without requring typedef in C

I am writing a program which dynamically resolves the address of some Windows API functions, such as the InternetConnectA function of the wininet.dll.

The exact details are irrellevant, the point is I have a function which gives me an LPVOID to the desired Windows API function, which I can then cast to a function pointer and call like the normal Windows API:

#include <Windows.h> typedef HINTERNET(*InternetConnectA_t)( HINTERNET hInternet, LPCSTR lpszServerName, INTERNET_PORT nServerPort, LPCSTR lpszUserName, LPCSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext ); int main(int argc, char** argv) { InternetConnectA_t internet_connect_a = (InternetConnectA_t) find_function("wininet.dll", "InternetConnectA"); internet_connect_a(...); } 

The find_function method internally parses the PE headers to find the address of the function inside wininet.dll.

However, using this method, I currently need to manually declare a typedef for every function I want to use (or specifiy the type inline). But since I already include the Windows.h header, which contains function declarations for all these methods, I am wondering if I could use them to tell the compiler "Please treat this address as having the same function signature as the declaration of InternetConnectA", removing the need for an extra typedef.

The following code does not work but might explain want I am attempting to do:

#include <Windows.h> int main(int argc, char** argv) { InternetConnectA* internet_connect_a = (InternetConnectA*) find_function("wininet.dll", "InternetConnectA"); internet_connect_a(...); } 

In that snippet, the compiler would then look the declaration of "InternetConnectA" and use that signature as the type for the pointer.

I understand that function overloading would introduce ambiguity to the function name, however that should only become a problem when using C++, since there is no function overloading possibilities in C (to my knowledge).

5

1 Answer

Well,

#include <stdio.h> int foo(int a, int b) { return a + b; } typedef typeof(foo)* foo_func_p; int main() { foo_func_p x = (foo_func_p)foo; printf("%d", x(1, 5)); } 

works (though I'm advised this is not standard C), so I'd imagine you could get away with

typedef typeof(InternetConnectA)* InternetConnectA_p; // ... InternetConnectA_p* internet_connect_a = (InternetConnectA_p) find_function("wininet.dll", "InternetConnectA"); 

to avoid typing out the typedef yourself.

Sure, if you want to get rid of the typedef altogether,

typeof(InternetConnectA)* internet_connect_a = (typeof(InternetConnectA)*) find_function("wininet.dll", "InternetConnectA"); 

should work too, but that's a mouthful...

8

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like