Andrew
Andrew

Reputation: 11

Use libcurl in native c to write a Windows service

Anyone knows how? I tried but there always is a problem with curl_global_init

"This function is not thread safe. You must not call it when any other thread in the program (i.e. a thread sharing the same memory) is running. This doesn't just mean no other thread that is using libcurl. Because curl_global_init() calls functions of other libraries that are similarly thread unsafe, it could conflict with any other thread that uses these other libraries. "

From this tutorial http://devx.com/cplus/Article/9857#codeitemarea I got my service working correctly, however, just by adding this line:

if (rand() == -1) curl_global_init(CURL_GLOBAL_ALL); 

I got error 1053: The service did not response to the start or control request in timely fashion. Even just call curl_version() will cause the bug.

Thanks.

Upvotes: 1

Views: 442

Answers (3)

Tometzky
Tometzky

Reputation: 23920

I think your problem is not with threads, as if you do:

if (rand() == -1) curl_global_init(CURL_GLOBAL_ALL);

then curl_global_init is never run.

But curl library has to be linked. And I think this is your problem — your service is unable to find or load this library. Maybe a service cannot find it because without shell it does not have correct PATH.

Upvotes: 0

MSalters
MSalters

Reputation: 180305

Install your service as SERVICE_WIN32_OWN_PROCESS. Obviously, your own service shouldn't have created any threads either at the point where you call curl_global_init. You shouldn't be creating threads before ReportSvcStatus(SERVICE_RUNNING) anyway, so this should be no big problem. Just call curl_global_init while in state SERVICE_START_PENDING.

Upvotes: 0

Tometzky
Tometzky

Reputation: 23920

Just issue curl_global_init() before you split to threads:

int main()
{
  if ( curl_global_init(CURL_GLOBAL_ALL) ) {
    panic();
  }
  // here goes your program
}

Upvotes: 1

Related Questions