1) Download The Library

The first step is to download our library from http://getlittleapps.com/little-software-stats/libraries/cppdll-latest.zip

2) Include The Library

Using a Windows C++ project that you created, add the “LittleSoftwareStatsDLL.dll”  (located in the DLL folder) to the folder where the result of the compiled code is located.

3) Integrate with your software

Next, you’ll want to call the DLL from your code to use it with your Little Software Stats. The following code is from Example.cpp which is included in the library:

/*
 * Little Software Stats - C++ DLL Library
 * Copyright (C) 2008 Little Apps (http://www.little-apps.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

#ifdef UNICODE
	#define WIDEN2(x) L ## x
	#define WIDEN(x) WIDEN2(x)
	#define __WFILE__ WIDEN(__FILE__)
#endif

// Formats
#define FORMAT_XML 0
#define FORMAT_JSON 1

typedef void (__cdecl *fStartProc)(LPCTSTR, int, LPCTSTR, LPCTSTR); 
typedef void (__cdecl *fStopProc)(); 
typedef void (__cdecl *fEventProc)(LPCTSTR, LPCTSTR); 
typedef void (__cdecl *fEventValueProc)(LPCTSTR, LPCTSTR, LPCTSTR); 
typedef void (__cdecl *fEventPeriodProc)(LPCTSTR, LPCTSTR, int, BOOL); 
typedef void (__cdecl *fLogProc)(LPCTSTR); 
typedef void (__cdecl *fCustomDataProc)(LPCTSTR, LPCTSTR); 
typedef void (__cdecl *fLicenseProc)(TCHAR); 
typedef void (__cdecl *fExceptionProc)(LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR); 
typedef void (__cdecl *fInstallProc)(LPCTSTR, LPCTSTR); 
typedef void (__cdecl *fUninstallProc)(LPCTSTR, LPCTSTR); 

fStartProc fStart;
fStopProc fStop;
fEventProc fEvent;
fEventValueProc fEventValue;
fEventPeriodProc fEventPeriod;
fLogProc fLog;
fCustomDataProc fCustomData;
fLicenseProc fLicense;
fExceptionProc fException;
fInstallProc fInstall;
fUninstallProc fUninstall;

bool LoadDll() {
	HINSTANCE hInst = LoadLibrary(_T("LittleSoftwareStatsDLL.dll"));

	if (hInst != NULL) {
		if ((fStart = (fStartProc) GetProcAddress(hInst, "Start")) == NULL)
			return false;
		if ((fStop = (fStopProc) GetProcAddress(hInst, "Stop")) == NULL)
			return false;
		if ((fEvent = (fEventProc) GetProcAddress(hInst, "Event")) == NULL)
			return false;
		if ((fEventValue = (fEventValueProc) GetProcAddress(hInst, "EventValue")) == NULL)
			return false;
		if ((fEventPeriod = (fEventPeriodProc) GetProcAddress(hInst, "EventPeriod")) == NULL)
			return false;
		if ((fLog = (fLogProc) GetProcAddress(hInst, "Log")) == NULL)
			return false;
		if ((fCustomData = (fCustomDataProc) GetProcAddress(hInst, "CustomData")) == NULL)
			return false;
		if ((fLicense = (fLicenseProc) GetProcAddress(hInst, "License")) == NULL)
			return false;
		if ((fException = (fExceptionProc) GetProcAddress(hInst, "Exception")) == NULL)
			return false;
		if ((fInstall = (fInstallProc) GetProcAddress(hInst, "Install")) == NULL)
			return false;
		if ((fUninstall = (fUninstallProc) GetProcAddress(hInst, "Uninstall")) == NULL)
			return false;

		return true;
	}

	return false;
}

void Event() {
	// Event info
	const TCHAR szCategory[] = _T("Buttons");
	const TCHAR szEvent[] = _T("OK");

	// Send event
	fEvent(szCategory, szEvent);
}

void EventValue() {
	// Event value info
	const TCHAR szCategory[] = _T("View");
	const TCHAR szEventName[] = _T("Zoom");
	const TCHAR szEventValue[] = _T("In"); // Or Out

	// Send event value
	fEventValue(szCategory, szEventName, szEventValue);
}

void EventPeriod() {
	// Event period info
	const TCHAR szCategory[] = _T("Main");
	const TCHAR szEventName[] = _T("Scan");
	int nDuration = 60; // equals 60 seconds or 1 minute
	bool bCompleted = false;

	// Send event period
	fEventPeriod(szCategory, szEventName, nDuration, bCompleted);
}

void Log() {
	// Log message
	const TCHAR szLogMsg[] = _T("Hello World!");

	// Send message
	fLog(szLogMsg);
}

void CustomData() {
	// Custom data
	const TCHAR szCustomName[] = _T("Name");
	const TCHAR szCustomValue[] = _T("Joe Blow");

	// Send custom data
	fCustomData(szCustomName, szCustomValue);
}

void License() {
	// Software licenses
	TCHAR chLicenseFree = _T('F');
	TCHAR chLicenseDemo = _T('D');
	TCHAR chLicenseTrial = _T('T');
	TCHAR chLicenseRegistered = _T('R');
	TCHAR chLicenseCracked = _T('C');

	fLicense(chLicenseCracked);
}

int _tmain(int argc, _TCHAR* argv[]) {
	UNREFERENCED_PARAMETER(argc);
	UNREFERENCED_PARAMETER(argv);

	if (!LoadDll()) {
		_tprintf(_T("Unable to load DLL"));
		return -1;
	}

	// API info
	const TCHAR szApiUrl[] = _T(""); // URL to API (ie: http://YOURWEBSITEHERE/api.json)
	int nApiFormat = FORMAT_JSON; // Must correspond with extension in URL (FORMAT_XML or FORMAT_JSON)

	// Application info
	const TCHAR szAppId[] = _T(""); // Application ID (ie: 26ad4eb8f804200f1e23b27c1873d703)
	const TCHAR szAppVer[] = _T(""); // Application version (ie: 1.0)

	if (_tccmp(szApiUrl, _T("")) == 0) {
		_tprintf(_T("API URL cannot be empty"));
		return -1;
	}

	if (_tccmp(szAppId, _T("")) == 0) {
		_tprintf(_T("Application ID cannot be empty"));
		return -1;
	}

	if (_tccmp(szAppVer, _T("")) == 0) {
		_tprintf(_T("Application version cannot be empty"));
		return -1;
	}

	// Start tracking
	fStart(szApiUrl, nApiFormat, szAppId, szAppVer);

	Log();

	// Exceptions
	try {
		throw 20;
	} catch(...) {
		LPCTSTR szStackTrace = _T("N/A"); // Set stack trace to "n/a" since were unable to get a stack trace
		LPCTSTR szMessage = _T("Divide by zero");
		LPCTSTR szTarget = _T("");

		TCHAR szSource[512];
		ZeroMemory(szSource, 512);
#ifdef UNICODE
		_stprintf_s(szSource, 512, _T("%s %d"), __WFILE__, __LINE__);
#else
		_stprintf_s(szSource, 512, _T("%s %d"), __FILE__, __LINE__);
#endif

		fException(szMessage, szStackTrace, szSource, szTarget);
	}

	fStop();

	return 0;
}

LittleSoftwareStatsDLL Functions

There are other functions that can be utilized to collect data using LittleSoftwareStatsDLL.dll which are described below:

// Starts tracking software
// Example: Start(_T("http://stats.mywebsite.com/api.json"), FORMAT_JSON, _T("26ad4eb8f804200f1e23b27c1873d703"), _T("1.0"));
void Start(LPCTSTR szApiUrl, int nApiType, LPCTSTR szAppId, LPCTSTR szAppVer);

// Stops tracking software and sends data to API
// Example: Stop();
void Stop();

// Track an event
// Example: Event(_T("Events"), _T("Scan"));
void Event(LPCTSTR szCategoryName, LPCTSTR szEventName);

// Track an event value
// Example: EventValue(_T("Settings", _T("Theme"), _T("Blue"));
void EventValue(LPCTSTR szCategoryName, LPCTSTR szEventName, LPCTSTR szEventValue);

// Track an event period
// Example: EventPeriod(_T("Scans"), _T("Clean"), 360, TRUE);
void EventPeriod(LPCTSTR szCategoryName, LPCTSTR szEventName, int nEventDuration, BOOL bCompleted);

// Track a log message
// Example: Log(_T("Hello World!"));
void Log(LPCTSTR szLogMessage);

// Track software license
// License parameter ("chLicense") can be either 'F' (Free), 'T' (Trial), 'R' (Registered), 'D' (Demo), or 'C' (Cracked)
// Example: License(_T('F'));
void License(TCHAR chLicense);

// Track custom data
// Example: CustomData(_T("GFX Card Brand"), _T("NVIDIA"));
void CustomData(LPCTSTR szDataName, LPCTSTR szDataValue);

// Track an exception
// Example: Exception(_T("Divide by zero"), _T("N/A"), _T("Example.cpp 29"), _T(""));
void Exception(LPCTSTR szMessage, LPCTSTR szStackTrace, LPCTSTR szSource, LPCTSTR szTargetSite);

// Track an installation
// Example: Install(_T("26ad4eb8f804200f1e23b27c1873d703"), _T("1.0"));
void Install(LPCTSTR szAppId, LPCTSTR szAppVer);
// Track an uninstallation
// Example: Uninstall(_T("26ad4eb8f804200f1e23b27c1873d703"), _T("1.0"));
void Uninstall(LPCTSTR szAppId, LPCTSTR szAppVer);

 

Notes

Please see the notes for this project on GitHub for more information.

Development Version

You can get the latest developer version of this library from GitHub. This version is not recommended, but it does contain new features. You will need the Git VCS to download the developer version.

You can get a copy of the branch using the command:

$ git clone git@github.com:little-apps/LittleSoftwareStatsDLL.git

You can also browse the source code here:
https://github.com/little-apps/LittleSoftwareStatsDLL

Comments