Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2018-03

devmgmt.msc

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

José Mejuto
to duilio foschi
duilio foschi wrote
if I run devmgmt.msc on W7 I get this info on screen: Is there a Winows API I can call from my Lazarus program and get the same info ? My aim is to detect which COM number was assigned to the USB device "MSP Application UART1"

Hello,

Attached unit will help you to implement a COM port browsing. There is a bit of extra code as original comes from a bigger unit, and this is an extract.

-------------- next part -------------- unit udeviceenumwin; {$mode objfpc}{$H+} interface uses Classes, SysUtils; function EnumDevices: integer; implementation uses windows; const WINDOWS_SETUPAPI_NAME='setupapi.dll'; DIGCF_DEFAULT = $00000001; DIGCF_PRESENT = $00000002; DIGCF_ALLCLASSES = $00000004; DIGCF_PROFILE = $00000008; DIGCF_DEVICEINTERFACE = $00000010; SPDRP_FRIENDLYNAME = $0000000C; type {$PackRecords C} HDEVINFO = type THANDLE; SP_DEVINFO_DATA = record cbSize: DWORD; ClassGuid: TGUID; DevInst: DWORD; // DEVINST handle Reserved: Pointer; end; PSP_DEVINFO_DATA=^SP_DEVINFO_DATA; SP_DEVICE_INTERFACE_DATA = record cbSize: DWORD; InterfaceClassGuid: TGUID; Flags: DWORD; Reserved: Pointer; end; PSP_DEVICE_INTERFACE_DATA= ^SP_DEVICE_INTERFACE_DATA; SP_DEVICE_INTERFACE_DETAIL_DATA_W = packed record cbSize: DWORD; DevicePath: array [0..0] of WideChar; end; PSP_DEVICE_INTERFACE_DETAIL_DATA_W = ^SP_DEVICE_INTERFACE_DETAIL_DATA_W; // SETUPAPI.DLL TSetupDiGetClassDevs = function (var aGUID: TGUID; aEnumerator: PWideChar; hwndParent: SizeInt; aFlags: DWORD): HDEVINFO; stdcall; TSetupDiEnumDeviceInterfaces = function (DeviceInfoSet: HDEVINFO; DeviceInfoData: PSP_DEVINFO_DATA; var InterfaceClassGuid: TGUID; MemberIndex: DWORD; var DeviceInterfaceData: SP_DEVICE_INTERFACE_DATA): WINBOOL; stdcall; TSetupDiGetDeviceInterfaceDetailW = function (DeviceInfoSet: HDEVINFO; DeviceInterfaceData: PSP_DEVICE_INTERFACE_DATA; DeviceInterfaceDetailData: PSP_DEVICE_INTERFACE_DETAIL_DATA_W; DeviceInterfaceDetailDataSize: DWORD; var RequiredSize: DWORD; DeviceInfoData: PSP_DEVINFO_DATA): WINBOOL; stdcall; TSetupDiDestroyDeviceInfoList = function (DeviceInfoSet: HDEVINFO): WINBOOL; stdcall; TSetupDiGetDeviceRegistryPropertyA = function (DeviceInfoSet: HDEVINFO; DeviceInfoData: PSP_DEVINFO_DATA; PropertyID: DWORD; PropertyRegDataType: PDWORD; PropertyBuffer: PBYTE; PropertyBufferSize: DWORD; RequiredSize: PDWORD): WINBOOL; stdcall; TSetupDiEnumDeviceInfo = function (DeviceInfoSet: HDEVINFO; MemberIndex: DWORD; var DeviceInterfaceData: SP_DEVICE_INTERFACE_DATA): WINBOOL; stdcall; type TDLLSetupAPIEntriesRecord=record SetupDiGetClassDevs: TSetupDiGetClassDevs; SetupDiEnumDeviceInterfaces: TSetupDiEnumDeviceInterfaces; SetupDiGetDeviceInterfaceDetailW: TSetupDiGetDeviceInterfaceDetailW; SetupDiDestroyDeviceInfoList: TSetupDiDestroyDeviceInfoList; SetupDiGetDeviceRegistryPropertyA: TSetupDiGetDeviceRegistryPropertyA; SetupDiEnumDeviceInfo: TSetupDiEnumDeviceInfo; end; var DLLSetupAPIHandle: TLibHandle; DLLInitializedFlag: Boolean=false; DLLInitializedSuccess: Boolean=false; DLLSetupAPIFunctions: TDLLSetupAPIEntriesRecord; function InitializeLibraries: Boolean; forward; procedure DEBUG_OUTPUT(msg: string); begin writeln(msg); end; function EnumDevices(): integer; var lGUID: TGUID='{4d36e978-e325-11ce-bfc1-08002be10318}'; // COM PORTS and LPT lDeviceInfoList: HDEVINFO; lDeviceInfo: SP_DEVICE_INTERFACE_DATA; lMemberIndex: integer; lStopEnumeration: Boolean=false; lIndex: integer; lFriendlyName: array [0..511] of char; begin if not InitializeLibraries then exit(1); ldeviceInfoList := DLLSetupAPIFunctions.SetupDiGetClassDevs(lGUID, nil, 0, DIGCF_PRESENT or DIGCF_DEVICEINTERFACE); if lDeviceInfoList=INVALID_HANDLE_VALUE then begin exit(2); end; lDeviceInfo.cbSize:=SizeOf(lDeviceInfo); lMemberIndex:=0; while true do begin if not DLLSetupAPIFunctions.SetupDiEnumDeviceInterfaces(ldeviceInfoList, nil, lGUID, lMemberIndex, lDeviceInfo) then begin // Browse finished lStopEnumeration:=true; break; end; lIndex:=0; while true do begin if DLLSetupAPIFunctions.SetupDiEnumDeviceInfo(lDeviceInfoList,lIndex,lDeviceInfo)=FALSE then begin break; end; if DLLSetupAPIFunctions.SetupDiGetDeviceRegistryPropertyA(lDeviceInfoList, at lDeviceInfo,SPDRP_FRIENDLYNAME,nil, at lFriendlyName[0],sizeof(lFriendlyName)-1,nil) then begin DEBUG_OUTPUT(lFriendlyName); end; inc(lIndex); end; if lStopEnumeration then break; inc(lMemberIndex); end; DLLSetupAPIFunctions.SetupDiDestroyDeviceInfoList(lDeviceInfoList); Result:=0; end; function InitializeLibraries: Boolean; var lSuccessFunctions: Boolean=true; function CheckFunctionsRecord(p: Pointer; aEntries: integer): Boolean; var j: integer; pProc: Pointer; lAssigned: Boolean=true; pp: PPointer; begin pp:=p; for j := 0 to Pred(aEntries) do begin pProc:=pp^; if not Assigned(pProc) then begin lAssigned:=false; break; end; inc(pp); end; Result:=lAssigned; end; begin if DLLInitializedFlag then exit(DLLInitializedSuccess); DLLSetupAPIFunctions:=Default(TDLLSetupAPIEntriesRecord); DLLInitializedFlag:=true; DLLSetupAPIHandle:=LoadLibrary(WINDOWS_SETUPAPI_NAME); if DLLSetupAPIHandle0 then begin Pointer(DLLSetupAPIFunctions.SetupDiGetClassDevs):=GetProcAddress(DLLSetupAPIHandle,'SetupDiGetClassDevsW'); Pointer(DLLSetupAPIFunctions.SetupDiEnumDeviceInterfaces):=GetProcAddress(DLLSetupAPIHandle,'SetupDiEnumDeviceInterfaces'); Pointer(DLLSetupAPIFunctions.SetupDiGetDeviceInterfaceDetailW):=GetProcAddress(DLLSetupAPIHandle,'SetupDiGetDeviceInterfaceDetailW'); Pointer(DLLSetupAPIFunctions.SetupDiDestroyDeviceInfoList):=GetProcAddress(DLLSetupAPIHandle,'SetupDiDestroyDeviceInfoList'); Pointer(DLLSetupAPIFunctions.SetupDiGetDeviceRegistryPropertyA):=GetProcAddress(DLLSetupAPIHandle,'SetupDiGetDeviceRegistryPropertyA'); Pointer(DLLSetupAPIFunctions.SetupDiEnumDeviceInfo):=GetProcAddress(DLLSetupAPIHandle,'SetupDiEnumDeviceInfo'); end; if lSuccessFunctions then begin lSuccessFunctions:=CheckFunctionsRecord(@DLLSetupAPIFunctions,sizeof(DLLSetupAPIFunctions) div sizeof(Pointer)); end; if not lSuccessFunctions then begin DEBUG_OUTPUT('LoadLibrary error'); end; DLLInitializedSuccess:=lSuccessFunctions; Result:=DLLInitializedSuccess; end; finalization if DLLInitializedFlag then begin if (DLLSetupAPIHandle0) then FreeLibrary(DLLSetupAPIHandle); end; end.

Reply by email

duilio foschi
to José Mejuto

Hi Jose',

thank you for the code, but

1. I get a number of error messages while compiling the unit with Lazarus 1.6.4 under W7 (see below)
2. I cannot understand how I can get the enumerated COM device names into a TStringList as I need to process the names.

Thank you

Peppe

Compile Project, Target:
C:\Users\PCCE88~1.BUF\AppData\Local\Temp\project1.exe: Exit code 1, Errors: 11
udeviceenumwin.pas(83,22) Error: Identifier not found "TLibHandle" udeviceenumwin.pas(83,32) Error: Error in type definition udeviceenumwin.pas(164,23) Error: Operator is not overloaded: "<erroneous type>" = "ShortInt"
udeviceenumwin.pas(165,88) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "QWord"
udeviceenumwin.pas(166,96) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "QWord"
udeviceenumwin.pas(167,101) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "QWord"
udeviceenumwin.pas(168,97) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "QWord"
udeviceenumwin.pas(169,102) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "QWord"
udeviceenumwin.pas(170,90) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "QWord"
udeviceenumwin.pas(184,26) Error: Operator is not overloaded: "<erroneous type>" = "ShortInt"
udeviceenumwin.pas(184,65) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "QWord"

2018-03-29 14:22 GMT+02:00 José Mejuto :

Show quoted text 17 lines
El 28/03/2018 a las 22:48, duilio foschi via Lazarus escribió: > > if I run devmgmt.msc on W7 I get this info on screen: > Is there a Winows API I can call from my Lazarus program and get the same > info ? > My aim is to detect which COM number was assigned to the USB device > "MSP Application UART1" Hello, Attached unit will help you to implement a COM port browsing. There is a bit of extra code as original comes from a bigger unit, and this is an extract. --

Reply by email

José Mejuto
to duilio foschi
duilio foschi wrote 5 lines
thank you for the code, but 1. I get a number of error messages while compiling the unit with Lazarus 1.6.4 under W7 (see below) 2. I cannot understand how I can get the enumerated COM device names into a TStringList as I need to process the names.

Hello,

I'm unable to understand how you get the first error :-? TLibHandle is in system unit which is mandatory, so I do not know how to you can get an "Identifier not found" :-?

Anyway, attached you can get a project sample, so you can process the data. You can extract the COM number from the name displayed.

-------------- next part -------------- A non-text attachment was scrubbed... Name: enumdevicessample.zip Type: application/x-zip-compressed Size: 3912 bytes Desc: not available URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20180330/6e89a2c1/attachment.bin>

Reply by email

duilio foschi
to José Mejuto

Hi Jose',

doubting a configuration error, I uninstalled Lazarus, then installed it again (current version is 1.8.2).

Unfortunately I could not get rid of the error on TLibHandle.

Then I found out that this type (TLibHandle) is not declared in system.pas but in rtl/dynlibs.pas

http://i67.tinypic.com/20ft940.jpg

I fixed the problem by adding dynlibs.pas to the project.

Unfortunately when I compile the code I get new error messages (see below) in unit lnfodwrf.

See

http://i68.tinypic.com/116qrdy.jpg

Well, I am sure I will fix all problems before or later :)

Thank you

Peppe

lnfodwrf.pp(846,23) Error: Incompatible types: got "<address of function(Pointer):AnsiString;Register>" expected "<procedure variable type of function(Pointer):ShortString;Register>"

Reply by email

José Mejuto
to duilio foschi
duilio foschi wrote 10 lines
Hi Jose', doubting a configuration error, I uninstalled Lazarus, then installed it again (current version is 1.8.2). Unfortunately I could not get rid of the error on TLibHandle. Then I found out that this type (TLibHandle) is not declared in system.pas but in rtl/dynlibs.pas

Hello,

You have a somehow corrupted Lazarus/fpc installation. Ensure that your Lazarus configuration points to the right fpc executable, or start a configuration from scratch using the "--pcp" executable parameter. Something like "Lazarus.exe --pcp C:\Lazarus\CleanConfig"

TLibHandle is defined in "sysdlh.inc" which is included by "dynlibh.inc" which is included by "system" unit in Windows platform.

Also you get a compilation error in the dwarf support which has been included long time ago, so your Lazarus configuration is pointing to an old fpc 2.2 or something like that.

Reply by email