Solved

im making a dll of https client,does it has a problem if i make a static function to call

Posted on 2015-01-10
1
221 Views
Last Modified: 2015-01-10
hello sir
im making a dll,does it has a problem if i make a static funcion?

please take a look at my code

//this is a SSL https client with libcurl & cjson

//"httpsclient.h"
extern "C"
  {
   __declspec(dllexport) int __stdcall sendmyinv(const char* theurl,const char * user,const char * pass,const char * pCaPath,const int recordcount,const char * prodno,const char * prodadd,const char * batchno,const double* invnum,const double * stdprice,const double * sellprice,const char * clino,const char * invbywho);
  }

//"httpsclient.cpp"

//
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <curl/curl.h>
#include "httpsclient.h"
#include "cJSON.h"

using namespace std;


//does this will have a problem if create a static function here?
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
if( NULL == str || NULL == buffer )
{
return -1;
}
char* pData = (char*)buffer;
str->append(pData, size * nmemb);
return nmemb;
}



int __stdcall sendmyinv(const char* theurl,const char * user,const char * pass,const char * pCaPath,const int recordcount,const char * prodno,const char * prodadd,const char * batchno,const char* invnum,const char * stdprice,const char * sellprice,const char * clino,const char * invbywho)
{

  CURLcode res;
  //serialze to json
  cJSON *root;
  root=cJSON_CreateObject();
  cJSON_AddStringToObject(root, "user", user);
  cJSON_AddStringToObject(root, "pass", pass);
  cJSON_AddNumberToObject(root, "recordcount", recordcount);
  cJSON_AddStringToObject(root, "prodno", prodno);
  cJSON_AddStringToObject(root, "prodadd", prodadd);
  cJSON_AddStringToObject(root, "batchno", batchno);
  cJSON_AddStringToObject(root, "invnum", invnum);
  cJSON_AddStringToObject(root, "stdprice", stdprice);
  cJSON_AddStringToObject(root, "sellprice", sellprice);
  cJSON_AddStringToObject(root, "clino",clino);
  cJSON_AddStringToObject(root, "invbywho",invbywho);

  char *out ;
  out=cJSON_Print(root);
  std::string strResponse="";
  int thereturncode=0;


  CURL* curl = curl_easy_init();
  if(NULL == curl)
  {
    return CURLE_FAILED_INIT;
  }
 
    curl_easy_setopt(curl, CURLOPT_URL, theurl);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, out);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
    //here will call the static function
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
    //i have a little confuse that why here must use the & symbol
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
      
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
   
      if(NULL == pCaPath)
    {
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    else
    {
       //default is PEM,in addition support DER
       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
       curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
    }
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
        res = curl_easy_perform(curl);

             if(res != CURLE_OK)
             {
               
           free(out);
           curl_easy_cleanup(curl);
           return -2;
             }
             else
             {
         
          //json deserialize
          
             cJSON * json =cJSON_Parse(strResponse.c_str());
             thereturncode=cJSON_GetObjectItem(json, "returncode")->valueint;
           cJSON_Delete(json);
         //

              free(out);
          curl_easy_cleanup(curl);
              if (thereturncode==0)
              return 0;
              else
              return -1;
             }
       
 
 
}
0
Comment
Question by:ken yup
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Since 'static' in this very case only means that the visibility of 'OnWriteData()' is limited to the compilation unit (i.e. this very source code file) you might only run into problems if you need to use that function later in other source file. For now, you absolutely won't have any problems here.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now