Win32 API Functions not Found

馋奶兔 提交于 2019-12-07 22:32:25

问题


I am usign DevC++ on Windows 7 Ultimate 32 bit and have included windows.h and psapi.h in my program. All the Windows APIs I've used so far ar working except for GetProcessId and DebugActiveProcessStop. The compiler returns in both cases that the specified function is undeclared. However, when I look in winbase.h, I can clearly see that GetProcessId is declared. Even when I hover the mouse over the function, information on the structure is displayed. So, why can't the compiler seem to recognize either function?


回答1:


When using the Win32 API headers you need to specify what version of the operating system you are targeting. This is documented in the MSDN library.

Nowadays, you are supposed to do this by defining NTDDI_VERSION. If you check the documentation for GetProcessId you'll note that it requires Windows XP SP1, so you need to specify at least NTDDI_WINXPSP1 as the target operating system version. In fact since SP1 is no longer supported you're probably better off with SP2:

#define NTDDI_VERSION 0x05010200

In the past I've found that defining NTDDI_VERSION doesn't always work as expected, though hopefully most of the glitches have been ironed out by now. If it doesn't work, try using the older macro _WIN32_WINNT instead, which is also documented at the link given above. In this case you want:

#define _WIN32_WINNT 0x0502

If you later need to use functions that were introduced in Vista or Windows 7, change the value of NTDDI_VERSION or _WIN32_WINNT appropriately. The MSDN library documentation for each function says which version of the operating system it was introduced in.




回答2:


This problem sometimes pops up when you're coding in the windows api. You can see that the function is in the header file, but for some reason, your compiler disagrees. When you come across this problem, find the function in the header file, and look for pre-processor directives around it. You may need to define something in order to use that function.

In this case, here's what i found for the functions you're having problems with:

    #if (_WIN32_WINNT >= 0x0501)
    WINBASEAPI DWORD WINAPI GetProcessId(HANDLE);
    #endif

and

    #if (_WIN32_WINNT >= 0x0501)
    WINBASEAPI BOOL WINAPI DebugActiveProcessStop(DWORD);
    #endif

So, in your main code file, where you include the windows header, put this definition BEFORE your include of the windows header:

 #define _WIN32_WINNT 0x0501

This should solve your problem. Good luck ^_^



来源:https://stackoverflow.com/questions/9643182/win32-api-functions-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!