Blog Image

guivi

About the blog

In this blog I will keep track of projects I develop though out this year and may be in the future. For now it is juts a testing ground for developing the blog itself but I hope as I put more material it will become a good place for me to hold information.

List Video and Audio capture devices

C/C++ Programming, OpenCV & C++ Posted on 30 Apr, 2021 12:47:35
static HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
{
	// Create the System Device Enumerator.
	ICreateDevEnum *pDevEnum;
	HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
		CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));

	if (SUCCEEDED(hr))
	{
		// Create an enumerator for the category.
		hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0);
		if (hr == S_FALSE)
		{
			hr = VFW_E_NOT_FOUND;  // The category is empty. Treat as an error.
		}
		pDevEnum->Release();
	}
	return hr;
}

std::vector<CString> UpdatecameraLists()
{
	IMoniker *pMoniker = NULL;
	CString str;
	LPCTSTR buf;
	HRESULT hr;
	IEnumMoniker *pEnum;
	IPropertyBag *pPropBag;
	VARIANT var;


	hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

	hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
	std::vector<CString> _camlist;

	if (SUCCEEDED(hr))
	{
		while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
		{
			hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
			if (FAILED(hr))
			{
				pMoniker->Release();
				continue;
			}

			VariantInit(&var);

			// Get description or friendly name.
			hr = pPropBag->Read(L"Description", &var, 0);
			if (FAILED(hr))
			{
				hr = pPropBag->Read(L"FriendlyName", &var, 0);
			}
			if (SUCCEEDED(hr))
			{
				str.Format(L"%ls\n", var.bstrVal);
				buf = str;
				_camlist.push_back(str);
				//m_cameras.AddString(buf);
				VariantClear(&var);
			}

			hr = pPropBag->Write(L"FriendlyName", &var);

			// WaveInID applies only to audio capture devices.
			hr = pPropBag->Read(L"WaveInID", &var, 0);
			if (SUCCEEDED(hr))
			{
				printf("WaveIn ID: %d\n", var.lVal);
				VariantClear(&var);
			}

			hr = pPropBag->Read(L"DevicePath", &var, 0);
			if (SUCCEEDED(hr))
			{
				// The device path is not intended for display.
				printf("Device path: %S\n", var.bstrVal);
				VariantClear(&var);
			}

			pPropBag->Release();
			pMoniker->Release();
		}
		pEnum->Release();
	}
	CoUninitialize();

	return _camlist;
}


Check Available Serial Pots

C/C++ Programming Posted on 30 Jan, 2021 23:02:24
void CDialog::SelectComPort()
{
	TCHAR lpTargetPath[5000]; // buffer to store the path of the COMPORTS
	DWORD test;
	bool gotPort = 0; // in case the port is not found
	m_serialList.ResetContent();

	for (int i = 0; i < 255; i++) // checking ports from COM0 to COM255
	{
		CString str;
		str.Format(_T("%d"), i);
		CString ComName = CString("COM") + CString(str); // converting to COM0, COM1, COM2

		test = QueryDosDevice(ComName, lpTargetPath, 5000);

		// Test the return value and error if any
		if (test != 0) //QueryDosDevice returns zero if it didn't find an object
		{
			m_serialList.AddString((CString)ComName); // add to the ComboBox
			
			gotPort = 1; // found port
		}

		if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
		{
			lpTargetPath[10000]; // in case the buffer got filled, increase size of the buffer.
			continue;
		}

	}

	if (!gotPort) // if not port
		m_serialList.AddString((CString)"No Active Ports Found"); // to display error message incase no ports found

}


On Device Arrived/Removed

C/C++ Programming Posted on 30 Jan, 2021 23:01:31
BEGIN_MESSAGE_MAP(CDialog, CDialogEx)
	ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()

BOOL CDialog::OnDeviceChange( UINT wParam, DWORD_PTR  lParam)
{

}


Adding folder to Dll in Visual studio Win32 apps

C/C++ Programming, Visual Studio Posted on 08 Jan, 2021 22:19:01
  1. Go to project properties (Alt+F7)
  2. Under Debugging, look to the right
  3. There’s an Environment field.
  4. Add your relative path there (relative to vcproj folder) i.e. ..\some-framework\lib by appending PATH=%PATH%;$(ProjectDir)\some-framework\lib or prepending to the path PATH=C:\some-framework\lib;%PATH%
  5. Hit F5 (debug) again and it should work.


GUI Libraries For C++

C/C++ Programming Posted on 30 Dec, 2020 09:55:02

Qt
Sciter
wxWidgets
GTK+
gtkmm
CEGUI
Dear ImGui
Noesis GUI
Juce
Fox Toolkit
MiniGUI
Nuklear
NanoGUI
LittlevGL
neoGFX
morda
U++
dlib
nana
GWork
libui
Agar
FLTK
IUP
Boost.UI
LCUI
GLUI
TGUI
ivtools
GuiLite
Ultralight
Chromium Embedded Framework
CopperSpice
FlatUI
SFGUI
Lgi
Verdigris
Elements C++ GUI library
Boden
GacLib
Yue
MyGUI



Small program to stop the screen saver or screen blanking by moving cursor.

C/C++ Programming, Uncategorised Posted on 04 Sep, 2020 08:07:31
#include <iostream>
#include <windows.h>

bool iskeypressed(unsigned timeout_ms = 0)
{
    return WaitForSingleObject(
        GetStdHandle(STD_INPUT_HANDLE),
        timeout_ms
    ) == WAIT_OBJECT_0;
}

int main()
{
    POINT cursor;
    GetCursorPos(&cursor);

    std::cout << "Press any key to stop the program!\n";

    while (!iskeypressed(500)) {
        cursor.x += 10;
        SetCursorPos(cursor.x, cursor.y);
        Sleep(1000);

        cursor.x -= 10;
        SetCursorPos(cursor.x, cursor.y);
        Sleep(500);
    }

    std::cout << "Bye, Bye!\n";
    return 0;
}


Simple Server Socket C++

C/C++ Programming, Linux Posted on 01 Jun, 2020 12:01:41

This is a simple sever written in C++ and the original code can be found in “The definitive guide to Linux network programming” book.

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>

const char APRESSMESSAGE[] = "APRESS - For Professionals, by Professionals!\n";

int main(int argc, char *argv[])
{
	int simpleSocket = 0;
	int simplePort = 0;
	int returnStatus = 0;
	struct sockaddr_in simpleServer;
	
	// make sure we va a port number
	if (argc != 2){
		fprintf(stderr, "Usage: %s <port>\n", argv[0]);
		exit(1);
	}
	
	// Create streaming socket
	simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (simpleSocket == -1){
		fprintf(stderr, "Could not create a socket!\n");
		exit(1);
	}
	else {
		fprintf(stderr, "Socket created!\n");
	}
	// retrieve the port number for listing
	simplePort = atoi(argv[1]);
	
	// Set up address strucuter
	//use INADDR_ANY
	std::memset(&simpleServer, '\0',sizeof(simpleServer));
	simpleServer.sin_family = AF_INET;
	simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
	simpleServer.sin_port = htons(simplePort);
	
	returnStatus = bind(simpleSocket, (struct sockaddr*)&simpleServer, sizeof(simpleServer));
	
	if (returnStatus == 0){
		fprintf(stderr, "Bind Completed!\n");
	}
	else {
		fprintf(stderr, "Could not bid the address!\n");
		close(simpleSocket);
		exit(1);
	}
	// Listen on the socket for connection
	returnStatus = listen(simpleSocket, 5);
	
	if (returnStatus == -1){
		fprintf(stderr, "Cannot listen to socket\n!");
		close(simpleSocket);
		exit(1);
	}
	
	while(1) {
		struct sockaddr_in clientName = { 0};
		int simpleChildSocket = 0;
		socklen_t clientNameLength = sizeof(clientName);
		
		simpleChildSocket = accept(simpleSocket, (struct sockaddr *)&clientName, &clientNameLength);
		if (simpleChildSocket == -1){
			fprintf(stderr, "Cannot accept connections!\n");
			close(simpleSocket);
			exit(1);
		}
		
		write(simpleChildSocket, APRESSMESSAGE, strlen(APRESSMESSAGE));
		close(simpleChildSocket);
	}
	close(simpleSocket);
	return 0;
}


MFC C++

C/C++ Programming Posted on 04 Mar, 2020 10:23:27

Use DYNAMIC_DOWNCAST to get ribbon objects



Next »