I would look at the devcon source code in the Windows DDK. I attached readme so you can see that it has a source, to do exactly what you want - disable the serial port.
DevCon Example
Devcon
DevCon is a command line tool that displays detailed information about devices and allows you to search and manage devices from the command line. DevCon enables, disables, installs, configures, and removes devices on the local computer and displays detailed information about devices on local and remote computers. DevCon is included in the Windows DDK.
ABOUT THIS DOCUMENT
This document describes the source code for DevCon, which is included in the Windows DDK in the / src / setup / devcon directory. He explains the construction of DevCon and describes how to use the SetupAPI and device setup functions to list devices and perform device operations in a console application.
For a detailed description of DevCon features and instructions for using them, see the DevCon help file in the DDK documentation in Driver Development Tools / Driver Testing Tools / DevCon.
APPLICATION AREA
These instructions apply to Windows XP and Windows Server 2003. DevCon was designed for use in Windows 2000, Windows XP, and Windows Server 2003. It will not work in Windows 95, Windows 98, or Windows ME.
HOW IT WORKS
Running "devcon help" will provide a list of commands, as well as a brief description of what each command does. "devcon help" will provide more detailed help on this command. Each command is interpreted through the DispatchTable distribution table located at the bottom of cmds.cpp. Some commands use the generic EnumerateDevices device enumerator. Some of these commands will work when using the remote target computer, and will also work when using the 32-bit devcon on Wow64. Below are some of the most interesting features and APIs that they use:
cmdClasses
This command demonstrates the use of SetupDiBuildClassInfoListEx to list all device class GUIDs. The SetupDiClassNameFromGuidEx and SetupDiGetClassDescriptionEx functions are used to obtain additional information about each device class.
cmdListClass
This command demonstrates the use of SetupDiClassGuidsFromNameEx to enumerate one or more class GUIDs that match the class name. This command also demonstrates the use of SetupDiGetClassDevsEx to list all devices for each class GUID.
cmdFind cmdFindAll cmdStatus
Simple use of EnumerateDevices (see below) to list devices and display different levels of information about each device. Please note that all but cmdFindAll use DIGCF_PRESENT only to display information about the devices that are currently present. The main functionality of these and related devices is performed inside FindCallback.
cmdEnable cmdDisable cmdRestart
These commands show how to release DIF_PROPERTYCHANGE to turn on the device, disconnect the device, or restart the device. The main functionality for each of these commands is executed inside the ControlCallback.
These operations cannot be performed on a remote machine or in a Wow64 context. The CFGMGR32 API should not be used because they skip class and shared installations.
cmdUpdate
This command shows how to use UpdateDriverForPlugAndPlayDevices to update the driver for all devices with a specific driver. Normally INSTALLFLAG_FORCE is not specified, which allows UpdateDriverForPlugAndPlayDevices to determine if a more appropriate match already exists. It is defined in DevCon to allow DevCon to be used more efficiently as a debugging / testing tool. This cannot be done on a remote machine or in a Wow64 context.
cmdInstall
Changing cmdUpdate to install the driver when there is no related hardware. It creates a new instance of the instance with the root enumeration and associates it with the installed hardware identifier specified on the command line (which must match the hardware identifier in INF). This cannot be done on a remote machine or in a Wow64 context.
cmdRemove
The command to remove devices. Remote Plug and Play devices will reappear in response to cmdRescan. The main functionality of this command is in RemoveCallback, which demonstrates the use of DIF_REMOVE. This cannot be done on a remote machine or in a Wow64 context. The CFGMGR32 API should not be used because they skip class and shared installations.
cmdRescan
This command shows the correct way to rescan all Plug and Play devices that were previously removed, or that otherwise require a rescan to find them.
cmdDPAdd
This command allows you to add a driver package to your device. The main functions of this command demonstrate the use of SetupCopyOEMInf. Adding a driver package to the machine does not mean that the drivers are installed on the devices, it just means that the drivers are automatically available when a new device is connected or an existing device is updated.
cmdDPDelete
This command allows you to remove the driver package from the computer. The main functions of this command demonstrate the use of SetupUninstallOEMInf. Removing the driver package from the device does not remove the drivers associated with the device. If you want to do both, use cmdRemove on all devices using this driver package and then cmdDPDelete to remove the driver package itself from the computer. This feature is not available in Windows 2000 or earlier.
cmdDPEnum
This command allows you to list all third-party driver packages installed on the computer, and also shows how to get some common properties from the driver package (provider, class description, date and version of DriverVer).
cmdDPEnumLegacy
This command shows you how to list third-party driver packages on Windows Server 2003 and earlier operating systems.
Reboot
This function shows how to properly restart the computer from the hardware installation program. In particular, it passes ExitWindowsEx flags that cause a reboot related to the installation of the equipment. You should not restart the machine unnecessarily.
EnumerateDevices
Demonstrates using SetupDiGetClassDevsEx to list all devices or all existing devices globally or limited to a specific installation class. Demonstrates using SetupDiCreateDeviceInfoListEx to create an empty list associated with a class or not (in most cases, an empty list should not be associated with a class). Demonstrates using SetupDiOpenDeviceInfo to add a device instance to the device data list. These last two APIs are ideal for getting the DeviceInfoData structure from the device instance and machine name when mixing the CFGMGR32 API with the SETUPAPI API. SetupDiGetDeviceInfoListDetail is called to obtain a handle to the remote machine, which can be passed to the CFGMGR32 API. SetupDiEnumDeviceInfo is called to enumerate each device that is in the device data list (either explicitly added or defined by calling SetupDiGetClassDevsEx). The instance identifier is obtained by calling CM_Get_Device_ID_Ex, using the information in devInfo (obtained from SetupDiEnumerateDeviceInfo) and devInfoListDetail (obtained from SetupDiGetDeviceInfoListDetail). GetHwIds is called to obtain a list of hardware and a compatible identifier (explained below). Once an interesting device is discovered (usually by checking the hardware identifiers), a callback is called to work on that particular device.
Gethwids
Shows how to get a complete list of hardware identifiers or a compatible identifier for a device using SetupDiGetDeviceRegistryProperty.
GetDeviceDescription
Shows how to get descriptive information about the device. A friendly name is used if it exists; otherwise, a device description is used.
DumpDeviceWithInfo
Shows how to get the instance ID (or use any CFGMGR32 API) with HDEVINFO data (device information list) and PSP_DEVINFO_DATA (device information data).
DumpDeviceStatus
Shows how to interpret the information returned by CM_Get_DevNode_Status_Ex. For information returned by this API, refer to cfg.h.
DumpDeviceResources
Shows how to get information about the resources used by the device.
DumpDeviceDriverFiles
Provided as a debugging tool, receives information about files that are apparently used for the device. It uses SetupDiBuildDriverInfoList to obtain information about the driver used for the specified device. The list of drivers associated with the device can be listed by calling SetupDiEnumDriverInfo. In this case, no more than one driver will be indicated. This function continues to receive the list of files that are usually copied for this driver using DIF_INSTALLDEVICEFILES. SetupScanFileQueue is used to list the file queue to display a list of files associated with the driver.
DumpDeviceDriverNodes
As a debugging aid, this function defines a list of compatible drivers for the device. It uses SetupDiBuildDriverInfoList to get a list of compatible drivers. In this case, all drivers are listed, however, usually DIF_SELECTBESTCOMPATDRV and SetupDiGetSelectedDriver will be used together to find which driver will be considered the best for the OS.
DumpDeviceStack
This function defines the upper and lower class and device filters.
CREATING A DEVCON SAMPLE
To create a devcon sample:
Click the Build Environment icon of your choice in the Build Development Environment submenu. This will create the correct build environment to create this sample. Note that this sample will be built in 64-bit environments as well as in 32-bit environments.
At a command prompt, change to the directory containing the DevCon source code. For instance:
cd src \ setup \ devcon
- Use the BLD macro or do the following from the command line:
build -c
This calls the Microsoft make routines, which create the log files Build.log, Build.wrn, and Build.err.
When the assembly is completed, the executable will be placed in the ObjXXX \ I386 subdirectory of the directory specified in the Sources file (depending on the selected build environment).
If the assembly failed, check these errors: 1) the assembly environment is not configured properly, or 2) the changes made to the source code of the sample, the errors introduced.
USING DEVCON
DevCon is provided ready-made in \ devcon tools. For use, refer to the document provided by devcon.exe. DevCon is a command line utility with built-in documentation by typing "devcon help".
TESTING
Type "devcon find *" to display the device instances of all existing devices on the local computer.
Type "devcon status @root \ rdp_mou \ 0000" to view the status of the terminal server mouse driver.
Enter "devcon status PNP05" to display the status of all COM ports.