Hi,

This is a boring problem, long to explain. I will try to make things simple.

There is a C library that has a function with this interface:

void ne_set_server_auth(ne_session *session, ne_request_auth callback, void *userdata);

Where ne_request_auth is declared this way:

typedef int (*ne_request_auth)(void *userdata, const char *realm, int attempt, char *username, char *password);

I'm trying to create a C++ wrapper for some small set of the library functions.

I have a method "Connect" that uses the function ne_set_server_auth(), and also have two member variables to store both username and password.

Following the rules, username and password can only be set inside the callback, but I'm having a hell trying to define that callback.

The Connect method is defined as:

STDMETHODIMP DAVClient::Connect(/*some parameters, including username and password*/) {
....
  /*username and password copied into member variables*/
....
  /*the call to ne_ssl_set_verify()*/
....
  return S_OK;
}

My first try was to define the callback this way:

int DAVClient::AuthCallBack(void *userdata,const char *realm,int attempt,char *username,char *password) {
  ....
}

And leave the ne_ssl_set_verify() call in Connect's like:

ne_ssl_set_verify(m_Session,AuthCallback,NULL);

But the compiler complained with

ne_set_server_auth: cannot convert parameter 2 from 'int (void *,const char *,int,char *,char *)' to 'int (__cdecl *)(void *,const char *,int,char *,char *)'

That prevented me of to have a private method as a callback, and a private member variables to store both username and password. That would have been nice. :(

So I had to make the member variables public, and the callback function a simple "helper" function (not a method, but same type/inteface of course).

The new ne_ssl_set_verify() call in Connect's left this way:

ne_ssl_set_verify(m_Session,VerifyCallback,(void *)this);

And I have to pass the a reference to the current object (userdata parameter, to the rescue :D) to be able to get the public member variables values inside the callback. What a crazyness....and uglyness.

The code works, but I don't like it, and that approach is not thread safe, right?

I'm not a ++ guru, so please somebody more skilled give me his/her opinion.


Thanks. ;)

bump

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.