2016-01-13 32 views
6

C kullanarak Windows API'nın üzerine gerçekten basit bir soyutlama kitaplığı oluşturmaya çalışıyorum. Basit bir Makefile yaptım ve Msys kullanarak projeyi yapmaya çalıştığımda şunu elde ediyorum:Tek "tanımsız başvuru" hatası

current_dir = $(shell pwd) 

all: test 

test: lib 
    gcc -c -g tests.c 
    gcc -o tests.exe tests.o -L$(current_dir) -lwinapi 

lib: 
    gcc -c -g winapi.c 
    gcc -shared -o winapi.dll winapi.o -lIphlpapi -luser32 

clean: 
    rm *.o 
:

winapi.o: In function `get_client_pid': 
(long path)/winapi.c:13: undefined reference to `GetExtendedTcpTable' 
(long path)/winapi.c:31: undefined reference to `GetExtendedTcpTable' 
collect2.exe: error: ld returned 1 exit status 
make: *** [lib] Error 1 

ve get_client_pid() bu bir bağlayıcı hatası olduğu

#include <stdint.h> 
#include <stdio.h> 
#include <windows.h> 
#include <Iphlpapi.h> 

#define true 1 
#define false 0 


    uint32_t get_client_pid() 
    { 
     MIB_TCPTABLE_OWNER_PID *tcpTable; 
     PDWORD tableSize; 
     DWORD ret = GetExtendedTcpTable(tcpTable, 
             tableSize, 
             true, 
             2, 
             TCP_TABLE_OWNER_PID_ALL, 
             0); 

     if (ret == NO_ERROR) { 
      printf("ERROR get_client_pid: No error with table size at 0?\n"); 
      return 0; 
     } 
     if (ret != ERROR_INSUFFICIENT_BUFFER) { 
      printf("ERROR get_client_pid: No insufficient buffer with table size at 0?\n"); 
      return 0; 
     } 
     printf("TABLE SIZE %d\n", *tableSize); 
     tcpTable = malloc(*tableSize); 

     ret = GetExtendedTcpTable( tcpTable, 
            tableSize, 
            true, 
            2, 
            TCP_TABLE_OWNER_PID_ALL, 
            0); 

     if (ret != NO_ERROR) { 
      printf("ERROR get_client_pid: GetExtendedTcpTable error: %d\n", ret); 
      free(tcpTable); 
      return 0; 
     } 

     int i; 
     for (i = 0; i < tcpTable->dwNumEntries; i++) { 
      MIB_TCPROW_OWNER_PID row = tcpTable->table[i]; 
      if (row.dwRemotePort == 0x208 && row.dwRemoteAddr == 0x100007F) { 
       printf("FOUND PROCESS: %d\n", row.dwOwningPid); 
       free(tcpTable); 
       return row.dwOwningPid; 
      } 
     } 

     free(tcpTable); 
     return 0; 
    } 

olarak winapi.c tanımlanır, benim makefile ekleyeceğiz 10

"-lIphlpapi" yi eklemek için mümkün olan her yeri denediğimi söylemeliyim ki, eğer bağlantı bir şekilde yanlış sırada olsa da bu yardımcı olmadı.

+0

C++ adı bozma sorunu? –

+0

Kullanıcı32'den bir işleve bir çağrı eklemeyi deneyin ve bu bağlantıların –

+0

olup olmadığını kontrol edin, makefile btw'nizden tamamen memnun değilim. winapi.dll winapi.c'ye bağlı olmalıdır, winapi.c'ye bağlı olmalıdır. 'clean' tests.exe ve winapi.dll dosyasını kaldırmalıdır. –

cevap

2

MinGW, WINVER'u windef.h dosyasında 0x400 (Windows NT) olarak tanımlamaktadır ve bu, Windows'un bazı yeni özelliklerinin bağlanmayacağı anlamına gelir.

#ifndef WINVER 
#define WINVER 0x0400 
/* 
* If you need Win32 API features newer the Win95 and WinNT then you must 
* define WINVER before including windows.h or any other method of including 
* the windef.h header. 
*/ 
#endif 

Yani herhangi bir Windows başlıkları dahil önce hedefleyen Windows sürümüne göre WINVER kendinizi tanımlamanız gerekir. Aşağıdaki tanımlardan

birini kullanın:

#define WINVER 0x0500 // Windows 2000 
#define WINVER 0x0501 // Windows XP 
#define WINVER 0x0502 // Windows Server 2003 
#define WINVER 0x0600 // Windows Vista, Windows Server 2008 
#define WINVER 0x0601 // Windows 7 
#define WINVER 0x0602 // Windows 8 
#define WINVER 0x0603 // Windows 8.1 
#define WINVER 0x0A00 // Windows 10 
İlgili konular