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
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
229 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

840 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