delphi 非法访问问题
关于 标准USB通信问题
下面是我的代码求高手解释:
function TUsbUtil.OpenUsbDevice(pGuid: TGUID; sDevNameBuf: pchar): THandle;
var
hDevInfo1 : HDEVINFO;
deviceInfoData : TSPDeviceInterfaceData ;
nGuessCount : Dword;
iDevIndex :DWord;
hOut: THandle;
begin
showmessage('e');
//hDevInfo1 := SetupDiGetClassDevs(@pGuid, nil,0, DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
SetupDiGetClassDevs(@pGuid, nil,0, DIGCF_PRESENT or DIGCF_DEVICEINTERFACE); showmessage('f');
deviceInfoData.cbSize := sizeof(TSPDeviceInterfaceData);
nGuessCount := MAXLONG;
for iDevIndex :=0 to nGuessCount do
begin
if(SetupDiEnumDeviceInterfaces(hDevInfo1,nil,pGuid,iDevIndex,deviceInfoData)) then
begin
hOut := OpenOneDevice(hDevInfo1, @deviceInfoData, sDevNameBuf);
if (hOut <> INVALID_HANDLE_VALUE) then
begin
break;
end
else if (GetLastError() = ERROR_NO_MORE_ITEMS) then
begin
break;
end;
end;
end;
SetupDiDestroyDeviceInfoList(hDevInfo1);
Result := hOut;
end;
函数在执行到红色部分是出现错误;提示:
Access violation at address 00000000 Read of address 0000000
我在网上查了下 是非法访问地址,有可能是没有初始化造成的,可我看了下我函数,也没有研究出个所以然求高手指导!
[解决办法]
SetupDiGetClassDevs(@pGuid, nil,0, DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
这个可能是这个过程中有问题的