I'm trying to use the MIT Kerberos implementation (using the krb5_32.dll from k4w-4.0.1 and the associated header file) to get a TGT and Service ticket.
I've loaded the krb5_init_context function which, according to the header file, google and SO, takes only 1 argument (The krb5_context struct) and populates it.
#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;
}
To call a function using a pointer, you have to declare a function pointer. In general, a function pointer (static member, global or static function) declaration looks like this:
typedef return_type (*alias_name)(argtype_1, argtype_2,...argtype_n);
where return_type
is the return type, the alias_name
is the resulting name you will use to declare your function pointer variable, and the arg1type_1, argtype_2,
etc. are the argument types that function accepts.
According to your post, krb5_init_context
should be declared as this (using typedef
to simplify things):
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
After these changes, make sure that you are also declaring the function pointer with the matching calling convention as the exported function. If the function is __stdcall
, then you need to specify that in the typedef
. If you don't do this, your function will crash.
To add the calling convention (this case, it's __stdcall
):
typedef krb5_int32 (__stdcall *context_fn)(krb5_context*);