c++ - 导入的 DLL 函数引发 "term does not evaluate to a function taking 1 arguments"错误

标签 c++ dll dllimport kerberos mit-kerberos

我正在尝试使用 MIT Kerberos 实现(使用 k4w-4.0.1 中的 krb5_32.dll 和关联的头文件)来获取 TGT 和服务票证。

我已经加载了 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;
}

最佳答案

要使用指针调用函数,您必须声明一个函数指针。通常,函数指针(静态成员、全局或静态函数)声明如下所示:

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*);  

关于c++ - 导入的 DLL 函数引发 "term does not evaluate to a function taking 1 arguments"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727258/

相关文章:

c++ - 整个应用程序的变量

.net - 从托管代码中释放非托管内存分配

java - 可运行的 JavaCV 项目

c# - C# 中的非托管 dll 函数字节 * 参数返回

C# 与 C DLL 互操作,如何编码从函数返回长度的 char 数组?

c++ - 迭代器不是对象吗?

python - TensorFlow,为什么选择 python 语言?

c++ - 无法使用 MinGW 链接到 SDL2 函数

c++ - 通过 sscanf 读取全名

c++ - 编译 C++ dll Visual Studio 链接错误与 crt 库