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
224 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
ID: 40541991
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

867 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

21 Experts available now in Live!

Get 1:1 Help Now