i am trying to write a short program which allows me to terminate a windows job object by its name. Here is the (shortend) code of file TerminateJobObject.c:
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#endif
#define JOB_OBJECT_TERMINATE 0x0008
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <limits.h>
LPTSTR jobObjectName;
HANDLE jobObj;
int main(int argc, TCHAR *argv[]){
jobObjectName = argv[0];
jobObj = OpenJobObject(JOB_OBJECT_TERMINATE,FALSE,jobObjectName);
TerminateJobObject(jobObj,0);
}
I get the following error when compiling with "gcc TerminateJobObject.c -o TerminateJobObject":
TerminateJobObject.c: In function 'main'
C:/<...>:TerminateJobObject.c:(.text+0x62):undefined reference to 'OpenJobObject'
collect2: ld returned 1 exit status
I don't understand why the linker can't resolve OpenJobObject. TerminateJobObject is linked correctly and is also from the windows api.
What i tried so far:
I am not very experienced with c and windows api and can't find anything on this problem, so it would be great if somebody could point me in a direction.
Api reference: OpenJobObject
I solved it. The Problem was/is that the header file winbase.h of mingw32 (which is included via windows.h) is missing the function definition for OpenJobObject as Harry Johnston suspected.
I added the following lines to .../mingw/include/winbase.h
#define OpenJobObject __MINGW_NAME_AW(OpenJobObject)
WINBASEAPI HANDLE WINAPI OpenJobObjectA (DWORD dwDesiredAccess, WINBOOL bInheritHandle, LPCSTR lpName);
WINBASEAPI HANDLE WINAPI OpenJobObjectW (DWORD dwDesiredAccess, WINBOOL bInheritHandle, LPCWSTR lpName);
Which i found in the winbase.h from mingw-w64
Next I changed the call to "OpenJobObjectA" and now it works. :) Thanks for the help!
Edit: As Hans Passant pointed out a cleaner way would probably be to migrate to mingw-64 or something else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With