Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows api programming with c: undefined reference to OpenJobObject

Tags:

c

windows

winapi

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:

  • Compiler: gcc, clang
  • Different versions for _WIN32_WINNT (0x500,0x600,0x601)
  • Different OS: Windows 7 and Windows server 2008
  • "OpenJobObjectW" and "OpenJobObjectA"
  • Defining WINVER

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

like image 837
F. K. Avatar asked Nov 11 '22 03:11

F. K.


1 Answers

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.

like image 142
F. K. Avatar answered Nov 14 '22 23:11

F. K.