Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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

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
ken yup
Asked:
ken yup
1 Solution
 
jkrCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now