在Windows 7下,函数SetupDiEnumDeviceInterfaces失败,并显示1008错误代码

编程入门 行业动态 更新时间:2024-10-25 06:31:06
本文介绍了在Windows 7下,函数SetupDiEnumDeviceInterfaces失败,并显示1008错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我们正在将HID设备支持应用程序从Windows XP移植到Windows7. 问题是该应用程序在Windows 7下找不到该设备,尽管在Windows XP下可以正常运行.函数SetupDiEnumDeviceInterfaces失败,错误代码为1008(尝试引用不存在的令牌.").操作系统正确枚举了我们的设备,并且在应用程序外部(相对于操作系统)正常运行.只是我们的应用程序不能(在Win 7下)使用其接口之一(有3个)来设置通信. 任何建议将不胜感激. 这里代码是:

Hello,We are porting our HID device support application from Windows XP to Windows 7.The problem is that the application can not find the device under windows 7, though it worked OK under Windows XP. The function SetupDiEnumDeviceInterfaces failsand theerror code is 1008 ("An attempt was made to reference a token that does not exist."). The OS correctly enumerates our device and it functions correctly outside our application (vs. OS). Just our application can not (under Win 7) setup communication with one of its interfaces (there are 3).Any suggestion will be appreciated.Here is the code:

//目的:使用SetupDi API函数检索

//:属于界面类.

//Accepts:myGuid-接口类GUID.

//:devicePathName-指向包含

//:附件的设备路径名设备.

//返回:如果至少有一个,则为true找到设备,否则为False.

//int DetailData;

布尔 DeviceFound = 错误 ;

bool DeviceFound = false;

IntPtr DeviceInfoSet;

IntPtr DeviceInfoSet;

布尔 LastDevice = 错误 ;

bool LastDevice = false;

int BufferSize = 0;

int BufferSize = 0;

int MemberIndex = 0;

int MemberIndex = 0;

//DeviceManagementApiDeclarations.SP_DEVINFO_DATA MyDeviceInfoData;

DeviceManagementApiDeclarations . SP_DEVICE_INTERFACE_DETAIL_DATA MyDeviceInterfaceDetailData = 新 HID_Test_Application. DeviceManagementApiDeclarations . SP_DEVICE_INTERFACE_DETAIL_DATA ();

DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DETAIL_DATA MyDeviceInterfaceDetailData = new HID_Test_Application.DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DETAIL_DATA();

DeviceManagementApiDeclarations . SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = 新 HID_Test_Application. DeviceManagementApiDeclarations . SP_DEVICE_INTERFACE_DATA ();

DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new HID_Test_Application.DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA();

int 结果= 0;

int Result = 0;

字符串 SingledevicePathName;

string SingledevicePathName;

布尔 成功= 错误 ;

bool Success = false;

尝试

{

//***

//API函数:SetupDiGetClassDevs

//目的:

//检索设备的信息集指定的设备组.

//SetupDiEnumDeviceInterfaces使用设备信息集.

//接受:

//接口类GUID

//空可检索所有设备的信息实例

//可选的顶部句柄-级别窗口(此处未使用)

//用于将返回的信息限制为当前存在的设备

//以及在由GUID指定的类.

//返回:

//设备信息集的句柄用于设备.

//***

DeviceInfoSet =

DeviceInfoSet =

DeviceManagementApiDeclarations .SetupDiGetClassDevs(

DeviceManagementApiDeclarations.SetupDiGetClassDevs (

参考 myGuid,

ref myGuid,

空 ,

0,

null,

0,

DeviceManagementApiDeclarations .DIGCF_PRESENT | DeviceManagementApiDeclarations .DIGCF_DEVICEINTERFACE);

DeviceManagementApiDeclarations.DIGCF_PRESENT | DeviceManagementApiDeclarations.DIGCF_DEVICEINTERFACE);

调试 .WriteLine(MyDebugging.ResultOfAPICall( "SetupDiClassDevs" )));

Debug.WriteLine(MyDebugging.ResultOfAPICall("SetupDiClassDevs"));

DeviceFound =

DeviceFound =

false ;

MemberIndex = 0;

false;

MemberIndex = 0;

要做 {

do {

//以0开头,并通过设备信息设置到

//没有更多可用的设备.

//MyDeviceInterfaceData结构的cbSize元素必须设置为

//结构的大小(以字节为单位).大小为28个字节.

MyDeviceInterfaceData.cbSize = 28;

MyDeviceInterfaceData.cbSize = 28;

//Marshal.SizeOf(MyDeviceInterfaceData);

//***

//API函数:

//SetupDiEnumDeviceInterfaces()

//目的:检索SP_DEVICE_INTERFACE_DATA

//设备的结构.

//返回时,MyDeviceInterfaceData包含该句柄到

//用于检测到的设备的SP_DEVICE_INTERFACE_DATA结构.

//接受:

//SetupDiGetClassDevs返回的DeviceInfoSet.

//接口类GUID.

//用于在其中指定设备的索引设备信息集.

//指向设备的SP_DEVICE_INTERFACE_DATA结构.

//返回:

//成功时非零,零在True上.

//***

结果=

DeviceManagementApiDeclarations .SetupDiEnumDeviceInterfaces(

DeviceInfoSet,

DeviceManagementApiDeclarations.SetupDiEnumDeviceInterfaces (

DeviceInfoSet,

0,

参考 myGuid,

会员索引

ref myGuid,

MemberIndex,

参考 MyDeviceInterfaceData);

ref MyDeviceInterfaceData);

调试 .WriteLine(MyDebugging.ResultOfAPICall( "SetupDiEnumDeviceInterfaces" )));

Debug.WriteLine(MyDebugging.ResultOfAPICall("SetupDiEnumDeviceInterfaces"));

//了解是否设置了设备信息已被检索.

如果 (结果== 0){

LastDevice =

if (Result == 0) {

LastDevice =

真 ;

}

true;

}

其他 {

else {

//存在设备.

调试 .WriteLine( 设备#的DeviceInfoSet" + MemberIndex.ToString()+ ":" );

Debug.WriteLine(" DeviceInfoSet for device #" + MemberIndex.ToString() + ": ");

调试 .WriteLine( " cbSize =" + MyDeviceInterfaceData.cbSize.ToString());

Debug.WriteLine(" cbSize = " + MyDeviceInterfaceData.cbSize.ToString());

调试 .WriteLine( " InterfaceclassGuid =" + MyDeviceInterfaceData.InterfaceClassGuid.ToString());

Debug.WriteLine(" InterfaceclassGuid = " + MyDeviceInterfaceData.InterfaceClassGuid.ToString());

调试 .WriteLine( 标志=" + MyDeviceInterfaceData.Flags.ToString( "x" ));

Debug.WriteLine(" Flags = " + MyDeviceInterfaceData.Flags.ToString("x"));

//***

//API函数:

//SetupDiGetDeviceInterfaceDetail()

//目的:

//检索SP_DEVICE_INTERFACE_DETAIL_DATA结构

//包含有关设备的信息.

//要检索信息,请调用此功能两次.

//首次返回结构.

//第二次返回指向数据.

//接受:

//SetupDiGetClassDevs返回的DeviceInfoSet

//SetupDiEnumDeviceInterfaces返回的SP_DEVICE_INTERFACE_DATA结构

//指向SP_DEVICE_INTERFACE_DETAIL_DATA结构的指针接收信息

//有关指定的界面.

//SP_DEVICE_INTERFACE_DETAIL_DATA结构的大小.

//指向变量的指针收到返回的所需大小的

//SP_DEVICE_INTERFACE_DETAIL_DATA结构.

//指向SP_DEVINFO_DATA结构的指针,该指针指向接收有关设备的信息.

//返回:

//成功时非零,零失败.

//***

成功=

DeviceManagementApiDeclarations .SetupDiGetDeviceInterfaceDetail(

DeviceInfoSet,

DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(

DeviceInfoSet,

参考 MyDeviceInterfaceData,

ref MyDeviceInterfaceData,

IntPtr .0, 参考 BufferSize, IntPtr .零);

IntPtr.Zero, 0, ref BufferSize, IntPtr.Zero);

调试 .WriteLine(MyDebugging.ResultOfAPICall( "SetupDiGetDeviceInterfaceDetail" )));

Debug.WriteLine(MyDebugging.ResultOfAPICall("SetupDiGetDeviceInterfaceDetail"));

调试 .WriteLine( (可以说太小了")

Debug.WriteLine(" (OK to say too small)");

调试 .WriteLine( 数据所需的缓冲区大小:" + BufferSize);

Debug.WriteLine(" Required buffer size for the data: " + BufferSize);

//存储结构的尺寸.

//MyDeviceInterfaceDetailData.cbSize = MyDeviceInterfaceDetailData.ToString ().长度;

MyDeviceInterfaceDetailData.cbSize =

MyDeviceInterfaceDetailData.cbSize =

元帅 .SizeOf(MyDeviceInterfaceDetailData);

Marshal.SizeOf(MyDeviceInterfaceDetailData);

//使用以下方法为MyDeviceInterfaceDetailData结构分配内存返回的缓冲区大小.

IntPtr DetailDataBuffer = 元帅 .AllocHGlobal(BufferSize);

IntPtr DetailDataBuffer = Marshal.AllocHGlobal(BufferSize);

//在前4个字节中存储cbSize数组的

元帅 .WriteInt32(DetailDataBuffer,4 + 元帅 .SystemDefaultCharSize);

Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemDefaultCharSize);

调试 .WriteLine( " cbsize =" + MyDeviceInterfaceDetailData.cbSize);

Debug.WriteLine("cbsize = " + MyDeviceInterfaceDetailData.cbSize);

//再次调用SetupDiGetDeviceInterfaceDetail.

//这次,将指针传递给DetailDataBuffer

//和返回的所需缓冲区大小.

成功=

DeviceManagementApiDeclarations .SetupDiGetDeviceInterfaceDetail(

DeviceInfoSet,

DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail (

DeviceInfoSet,

参考 MyDeviceInterfaceData,

DetailDataBuffer

ref MyDeviceInterfaceData,

DetailDataBuffer,

BufferSize,

BufferSize,

参考 BufferSize,

ref BufferSize,

IntPtr .为零);

IntPtr.Zero);

调试 .WriteLine(MyDebugging.ResultOfAPICall( 第二次调用的结果:" )));

Debug.WriteLine(MyDebugging.ResultOfAPICall(" Result of second call: "));

调试 .WriteLine( "MyDeviceInterfaceDetailData.cbSize:" + MyDeviceInterfaceDetailData.cbSize.ToString());

Debug.WriteLine(" MyDeviceInterfaceDetailData.cbSize: " + MyDeviceInterfaceDetailData.cbSize.ToString());

//跳过cbsize(4个字节)获取devicePathName的地址.

IntPtr pdevicePathName = 新 IntPtr (DetailDataBuffer.ToInt32 ()+ 4);

IntPtr pdevicePathName = new IntPtr(DetailDataBuffer.ToInt32() + 4);

//获取包含devicePathName的字符串.

SingledevicePathName =

SingledevicePathName =

元帅 .PtrToStringAuto(pdevicePathName);

devicePathName [MemberIndex] = SingledevicePathName;

Marshal.PtrToStringAuto(pdevicePathName);

devicePathName[MemberIndex] = SingledevicePathName;

调试 .WriteLine( 设备路径=" + devicePathName [MemberIndex]);

Debug.WriteLine("Device Path = " + devicePathName[MemberIndex]);

//Debug.WriteLine("设备路径长度="+ Marshal.SizeOf(devicePathName [MemberIndex]));

调试 .WriteLine( 设备路径长度=" + devicePathName [MemberIndex] .Length);

Debug.WriteLine("Device Path Length = " + devicePathName[MemberIndex].Length);

//释放AllocHGlobal先前分配的内存

元帅 .FreeHGlobal(DetailDataBuffer);

DeviceFound =

Marshal.FreeHGlobal(DetailDataBuffer);

DeviceFound =

真 ;

}

true;

}

MemberIndex ++;

MemberIndex++;

}

同时 (!LastDevice);

while (!LastDevice);

//将数组修剪为找到设备.

调试 .WriteLine( "找到的HID数量=" +(MemberIndex-1).ToString() );

Debug.WriteLine("Number of HIDs found = " + (MemberIndex - 1).ToString());

//***

//API函数:

//SetupDiDestroyDeviceInfoList

//目的:

//释放为DeviceInfoSet保留的内存由SetupDiGetClassDevs返回.

//接受:

//SetupDiGetClassDevs返回的DeviceInfoSet.

//返回:

//成功则为True,失败则为False

//***

结果=

DeviceManagementApiDeclarations .SetupDiDestroyDeviceInfoList(DeviceInfoSet);

DeviceManagementApiDeclarations.SetupDiDestroyDeviceInfoList(DeviceInfoSet);

调试 .WriteLine(MyDebugging.ResultOfAPICall( "DestroyDeviceInfoList"));

Debug.WriteLine(MyDebugging.ResultOfAPICall("DestroyDeviceInfoList"));

}

catch (Exception ex) {

HandleException(ModuleName +

catch (Exception ex) {

HandleException(ModuleName +

":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);

}Thank you.

":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);

}Thank you.

推荐答案

Hello,

感谢您的信息!我建议您在( Windows客户端技术中心 > Windows 7 IT Pro论坛 > Windows 7应用程序兼容性 )论坛,位于: ( social.technet.microsoft/Forums/en/w7itproappcompat/threads ).

Thank you for your post! I would suggest creating a new thread for your question in the (Windows Client TechCenter > Windows 7 IT Pro Forums > Windows 7 Application Compatibility ) forum located here: (social.technet.microsoft/Forums/en/w7itproappcompat/threads).

希望会有所帮助.

祝您度过愉快的一天!

感谢&问候,

更多推荐

在Windows 7下,函数SetupDiEnumDeviceInterfaces失败,并显示1008错误代码

本文发布于:2023-11-06 01:20:15,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   错误代码   Windows   SetupDiEnumDeviceInterfaces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!