Scan a File for Viruses in C/C++

Cloudmersive
2 min readMar 25, 2021

--

Due to the ever-increasing variety of viruses and threats that can infect your devices, systems, and programs, the absence of virus scanning software is essentially an invitation for infection. This is especially true if your company allows users to upload unscanned files to a website or application. In this article we will discuss how to use an API in C or C++ to provide 360-degree content protection to your business by scanning files for more than just your everyday viruses or malware; this simple operation also screens for zero-day threats, executable files, macros, and much more.

To get things started, we will install dependencies for our C/C++ project:

libcurl/7.75.0

Then, we will perform our virus scan function call as demonstrated below:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/virus/scan/file/advanced");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "allowExecutables: <boolean>");
headers = curl_slist_append(headers, "allowInvalidFiles: <boolean>");
headers = curl_slist_append(headers, "allowScripts: <boolean>");
headers = curl_slist_append(headers, "allowPasswordProtectedFiles: <boolean>");
headers = curl_slist_append(headers, "allowMacros: <boolean>");
headers = curl_slist_append(headers, "restrictFileTypes: <string>");
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);

As long as you input your file and API key, the API will do the rest of the work for you. The response will be returned before you can blink, and indicate if the file is clean; if a threat was found, the name and type will be specified.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.