I'm curious about grammar.

Asked 2 years ago, Updated 2 years ago, 27 views

c++ cooking watching

typedef HRESULT(__stdcall* endScene)(IDirect3DDevice9* pDevice); 

I don't know what that means.

c++ hooking

2022-09-20 19:11

2 Answers

Definition of function pointer type.


endScene fpEncScene;

fpEncScene = some_addr;   // setting

fpEncScene(device); // calling the function


2022-09-20 19:11

As Daewon said, it's function pointer type definition.

Let me write down a little bit of explanation.

First of all, if you look at the code and the results below, you can easily see how the function pointer type definition is used.

#include <iostream>

using namespace std;

int sum(int a, int b)
{
    return a + b;
}

typeef (*p_sum)(inta, intb); // Define function pointer type

int main()
{
    p_summy_sum = sum; // Define function pointer variables

    cout <<"sum: "<< sum(10, 20) <<'\n'; // Use general function sum
    cout <<"my_sum:" << my_sum(10, 20) <<'\n'; // Use function pointer

    return 0;
}

When you define a function pointer type as below, you can see that it is a function pointer with a star in front of the function name part and parentheses before and after it, as shown in (*p_sum). By looking at the return value at the front of the function name part and the shape of the parameter at the back, you can see that the variable declared with this function pointer type can only store the address of the function with the return value int and the parameter (int, int). A compilation error occurs if the return value and parameter form do not match.

typedef int (*p_sum)(int a, int b);

In the case of the code below you asked, the variable declared as endSene type can only contain the address value of the function with the return value HRESULT and the parameter type (IDirect3DDdevice9*). And __stdcall is like a reserved word to mean that the function is a WINAPI function. It is attached before the WINAPI functions.

typedef HRESULT (__stdcall* endScene)(IDirect3DDevice9* pDevice); 

Please refer to the code and the results below. It'll help you understand.

#include <iostream>
#include <Windows.h>

using namespace std;

typedef DWORD (__stdcall* p_name)(LPSTR lpConsoleTitle, DWORD nSize);

int main()
{
    p_name my_get_console_title = GetConsoleTitleA;

    char str1[80] = { 0, };
    char str2[80] = { 0, };

    GetConsoleTitleA(str1, 80);
    my_get_console_title(str2, 80);

    cout << "str1: " << str1 << '\n';
    cout << "str2: " << str2 << '\n';

    return 0;
}


2022-09-20 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.