问题描述:

I'm reading through some source code, and I've found some wacky-looking code. Here is one example of a single function that's been declared an used, and this syntax has been used a lot.

HINSTANCE hlib = LoadLibrary(L"ps3tmapi.dll");

//initial definition

typedef int(__cdecl *ProcessInfo)(int,UINT32, UINT32*, SNPS3PROCESSINFO*);

//some function created using what's above

ProcessInfo GetProcessInfo = (ProcessInfo)GetProcAddress(hlib, "SNPS3ProcessInfo");

//use of this function

GetProcessInfo(Target,ProcessID,buffsz,info);

This is by no means in order, just snippets from the source code. So, what is this syntax doing exactly? I'm specifically scratching my head over the second line where GetProcessInfo is being declared.

To make things clear, I'm not so worried about the meaning of this code (as multiple things from here are defined elsewhere so you can't know what they are), I just haven't yet see this kind of syntax. So if someone could explain it, then that would be much appreciated. Thanks!

网友答案:
typedef int(__cdecl *ProcessInfo)(int,UINT32, UINT32*, SNPS3PROCESSINFO*);

This declares a type called ProcessInfo which represents a function returning an int and accepting 4 arguments int, UINT32, UINT32*, SNPS3PROCESSINFO*.

Now GetProcAddress returns a memory address which is the address of a function somewhere in memory. To be able to use it you must cast it to a function, that's the purpose of the second line

ProcessInfo GetProcessInfo = (ProcessInfo)GetProcAddress(hlib, "SNPS3ProcessInfo");

Declare a variable named GetProcessInfo which is a function of signature ProcessInfo and with the value of the address returned by GetProcAddress.

After this you can useGetProcessInfo as if you had

int GetProcessInfo(int,UINT32, UINT32*, SNPS3PROCESSINFO*);

defined in your code.

网友答案:

the example demonstrates how to load a dll and call one of the exported functions.

The signature of the exported function is set in below syntax.

typedef int(__cdecl *ProcessInfo)(int,UINT32, UINT32*, SNPS3PROCESSINFO*);

Below syntax, instructs the runtime to use the dll loaded by "LoadLibrary" call, and fetch a pointer to the exported function "SNPS3ProcessInfo" and assign to "GEtProcessInfo"

//some function created using what's above

ProcessInfo GetProcessInfo = (ProcessInfo)GetProcAddress(hlib, "SNPS3ProcessInfo");
相关阅读:
Top