开发者

Use libcurl in native c to write a Windows service

开发者 https://www.devze.com 2023-02-11 22:33 出处:网络
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

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 fashio开发者_JAVA技巧n. Even just call curl_version() will cause the bug.

Thanks.


Just issue curl_global_init() before you split to threads:

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消