?
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
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 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