2015-11-16 61 views
0

我想使用MIT Kerberos實現(使用k4w-4.0.1中的krb5_32.dll和相關聯的頭文件)來獲取TGT和服務票據。導入的DLL函數拋出「術語不計算爲一個函數帶1個參數」錯誤

我已經加載了krb5_init_context函數,它根據頭文件google和SO只接受1個參數(krb5_context結構體)並填充它。

#include "stdafx.h" 
#include "windows.h" 
#include "krb5.h" 

typedef int krb5_int32; 
typedef krb5_int32 krb5_error_code; 

int _tmain(int argc, _TCHAR* argv[]) 
{  

    HMODULE kerberos = LoadLibrary(L"krb5_32.dll"); 
    HANDLE krb5_init_context = NULL; 

    if(kerberos == NULL) 
    { 
     printf("Failed to load library!\n"); 
     printf("%lu", GetLastError()); 
     return -1; 
    } 
    else 
    { 
     printf("Library krb5_32.dll loaded successfully!\n"); 
    } 

    if((krb5_init_context = GetProcAddress(kerberos, "krb5_init_context")) == NULL) 
    { 
     printf("GetProcAddress for krb5_init_context failed!\n"); 
     return -1; 
    } 
    else 
    { 
     printf("Function krb5_init_context loaded successfully!\n"); 
    } 

    krb5_context context = NULL; 
    krb5_ccache cache = NULL; 
    krb5_principal client_princ = NULL; 
    char* name = NULL; 
    krb5_keytab keytab = 0; 
    krb5_creds creds; 
    krb5_get_init_creds_opt *options = NULL; 
    krb5_error_code error_code = 0; //error_status_ok; 

    error_code = (*krb5_init_context)(&context); 

    printf("Error Code: " + error_code); 

    while(true); 


    return 0; 
} 
+0

'krg5_init_context'是一個句柄,它不是一個函數指針。如果你想要它是函數指針,請將它聲明爲一個。 – PaulMcKenzie

+0

所以看看這個https://msdn.microsoft.com/en-us/library/64tkc9y5.aspx和其他一些頁面,我認爲它必須是一個句柄?我可以直接指定一個指向'GetProcAddress'的輸出嗎? – T3CHN0CR4T

+0

看到我的答案。 'HANDLE'是通用的。您需要將返回值轉換爲函數指針。 – PaulMcKenzie

回答

2

要使用指針調用函數,必須聲明函數指針。在一般情況下,一個函數指針(靜態成員,全局或靜態函數)聲明如下所示:

typedef return_type (*alias_name)(argtype_1, argtype_2,...argtype_n);

其中return_type是返回類型,alias_name是您將使用來聲明函數指針變量所產生的名字,而arg1type_1, argtype_2,等是函數接受的參數類型。

根據您的文章,krb5_init_context應(使用typedef簡化的東西)聲明爲此:

typedef krb5_int32 (*context_fn)(krb5_context*); // pointer to function type 
contextfn krb5_init_context; // declare it 
//... 
krb5_init_context = (context_fn)GetProcAddress(...); // get the address 
//.. 
krb5_context context; 
krb5_init_context(&context); // now function can be called 

這些更改後,請確保您也宣佈與匹配的調用約定的函數指針導出的函數。如果該功能是__stdcall,那麼您需要在typedef中指定該功能。如果你不這樣做,你的功能會崩潰。

要添加的調用約定(這種情況下,它的__stdcall):

typedef krb5_int32 (__stdcall *context_fn)(krb5_context*); 
+0

'typedef krb5_error_code(* context_fn)(&krb5_context);''給出'krb5_context:非法使用此類型作爲表達式。' – T3CHN0CR4T

+0

仍然從typedef獲得一堆錯誤。 '錯誤:'krb5_context':非法使用這種類型作爲表達式,''錯誤:'krb5_context':非法使用這種類型作爲表達式',錯誤:'krb5_int32 *':在'='之前聲明沒有變量。 – T3CHN0CR4T

+0

你打電話的函數的確切原型是什麼?我正在發佈你正在描述的內容,所以我認爲你需要從頭文件中發佈* exact *原型。另外,你在哪裏放置這個'typedef'。在'krb5_context'之前是否定義了所有其他別名?如果是這樣,那麼當然你會得到錯誤。 – PaulMcKenzie

相關問題