commit 121e6b560e29977bfece54219036b8e28205d0bf
Author: Hamish Coleman <hamish@zot.org>
Date:   Thu Apr 24 09:18:20 2008 +1000

    Begin testing in an attempt to enumerate serial ports

diff --git a/Makefile b/Makefile
index c7bfb94..8391c6f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 
-all: wconsd.exe
+all: wconsd.exe portenum.exe
 
 get:
 	pscp 192.168.1.1:s/src/wconsd/*.c ./
@@ -11,6 +11,9 @@ put:
 wconsd.exe: wconsd.c
 	gcc -Wall -o wconsd.exe wconsd.c -lws2_32
 
+portenum.exe: portenum.c
+	gcc -Wall -o portenum.exe portenum.c -lwinspool -lsetupapi
+
 test: wconsd.exe
 	./wconsd.exe -d
 
diff --git a/portenum.c b/portenum.c
new file mode 100644
index 0000000..d3526a8
--- /dev/null
+++ b/portenum.c
@@ -0,0 +1,162 @@
+#include <windows.h>
+#include <stdio.h>
+#include <setupapi.h>
+
+int enumports_level1() {
+	char *buf;
+	DWORD size=0;
+	DWORD nrports=0;
+	PORT_INFO_1 *pi1;
+
+	printf("getting level 1 information\n\n");
+
+	EnumPorts(NULL,1,NULL,0,&size,&nrports);
+	/* size now contains the required number of bytes */
+
+	if(!size) {
+		printf("size==0!!\n");
+		return 1;
+	}
+
+	buf=malloc(size);
+	if (!buf) {
+		printf("malloc fail\n");
+		return 1;
+	}
+
+	if(!EnumPorts(NULL,1,buf,size,&size,&nrports)) {
+		printf("enumports fail\n");
+		free(buf);
+		return 1;
+	}
+
+	printf("nrports=%lu\n",nrports);
+
+	pi1 = (PORT_INFO_1*)buf;
+	while(nrports) {
+		printf("port = %s\n",pi1->pName);
+		pi1++;
+		nrports--;
+	}
+
+	free(buf);
+	return 0;
+}
+
+int enumports_level2() {
+	char *buf;
+	DWORD size=0;
+	DWORD nrports=0;
+	PORT_INFO_2 *pi2;
+
+	printf("\n\ngetting level 2 information\n\n");
+
+	EnumPorts(NULL,2,NULL,0,&size,&nrports);
+
+	if(!size) {
+		printf("size==0!!\n");
+		return 1;
+	}
+
+	buf=malloc(size);
+	if (!buf) {
+		printf("malloc fail\n");
+		return 1;
+	}
+
+	if(!EnumPorts(NULL,2,buf,size,&size,&nrports)) {
+		printf("enumports fail\n");
+		free(buf);
+		return 1;
+	}
+	printf("nrports=%lu\n",nrports);
+
+	pi2 = (PORT_INFO_2*)buf;
+	printf("%-10s %-15s %-15s %-4s %-8s\n",
+		"Port","Monitor","Desc","Type","Reserved"
+	);
+	while(nrports) {
+		printf("%-10s %-15s %-15s %4lu %8lu\n",
+			pi2->pPortName, pi2->pMonitorName, pi2->pDescription,
+			pi2->fPortType, pi2->Reserved
+		);
+		pi2++;
+		nrports--;
+	}
+
+	free(buf);
+	return 0;
+}
+
+int m3() {
+	int bufsize;
+	char *guid;
+
+	HDEVINFO diset;
+
+	int i=0;
+
+	printf("\n\nGet something\n\n");	
+
+	SetupDiClassGuidsFromName("Ports",NULL,0,&bufsize);
+	if(!bufsize){printf("size==0!!\n");return 1;}
+
+	if (!(guid=malloc(bufsize))){printf("malloc fail\n");return 1;}
+	if (!SetupDiClassGuidsFromName("Ports",guid,bufsize,&bufsize)) {
+		printf("!SetupDiClassGuidsFromName\n");
+		return 1;
+	}
+
+	if ((diset = SetupDiGetClassDevs(guid,NULL,0,DIGCF_DEVICEINTERFACE))==INVALID_HANDLE_VALUE) {
+		printf("SetupDiGetClassDevs fail %lu\n",GetLastError());
+		return 1;
+	}
+
+	while(1) {
+		SP_DEVICE_INTERFACE_DATA didata;
+		SP_DEVICE_INTERFACE_DETAIL_DATA *didatad;
+		SP_DEVINFO_DATA dedata;
+		/* Hello, I am a retardo windows interface */
+		didata.cbSize=sizeof(didata);
+		dedata.cbSize=sizeof(dedata);
+
+		if (!SetupDiEnumDeviceInterfaces(diset,NULL,guid,i,&didata)) {
+			/* end of list? */
+			if (GetLastError()!=ERROR_NO_MORE_ITEMS) {
+				printf("!SetupDiEnumDeviceInterfaces %lu\n",GetLastError());
+			}
+			break;
+		}
+
+		SetupDiGetDeviceInterfaceDetail(diset,&didata,NULL,0,&bufsize,NULL);
+		if(!bufsize){printf("size==0!!\n");return 1;}
+
+		if (!(didatad=malloc(bufsize))){printf("malloc fail\n");return 1;}
+		/* GAH, stoopid interface */
+		didatad->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
+
+		if (!SetupDiGetDeviceInterfaceDetail(diset,&didata,didatad,bufsize,&bufsize,&dedata)) {
+			printf("!SetupDiGetDeviceInterfaceDetail %lu\n",GetLastError());
+			return 1;
+		}
+
+		printf("DevicePath=%s\n",didatad->DevicePath);
+	
+		free(didatad);	
+		i++;
+	}
+
+	SetupDiDestroyDeviceInfoList(diset);
+	free(guid);
+
+	return 0;
+}
+
+int main(int argc, char **argv) {
+
+	enumports_level1();
+	enumports_level2();
+	m3();
+
+	return 0;
+}
