문제

I have the following code:

#include <windows.h>
class systemfunctions
{
    public:
    void (*sleep) (DWORD ms);

    systemfunctions ()
    {
        sleep = reinterpret_cast<void>(Sleep);
    }
} sys;

When I call sys.sleep(), the application crashes. Why does the program crash, and what can I do to resolve the problem?

도움이 되었습니까?

해결책

Windows.h declares Sleep() like WINBASEAPI VOID WINAPI Sleep(__in DWORD dwMilliseconds);, try telling the compiler it needs to use the proper calling convention when it uses that pointer:

typedef VOID (WINAPI * SleepFunction)(DWORD ms);
SleepFunction sleep;

sleep = Sleep;

다른 팁

The only time you should cast a function pointer is when using the return value from GetProcAddress.

Get rid of the cast, and then the compiler will tell you what is wrong.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top