主题:请问如何处理DeviceIoControl函数的lpOutBuffer参数?
huhu0013
[专家分:160] 发布于 2008-04-12 23:11:00
我在VB6中使用API函数DeviceIoControl,遇到如下问题,请大家帮看看:
DeviceIoControl函数原型定义如下:
Public Declare Function DeviceIoControl Lib "kernel32" Alias "DeviceIoControl" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, [color=FF0000]lpOutBuffer As Any[/color], ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
从lpOutBuffer参数返回的数据,应该如何转换为字符串类型,从而显示到窗体上呢?
希望能给出演示代码。
回复列表 (共7个回复)
沙发
tanchuhan [专家分:15140] 发布于 2008-04-12 23:37:00
lpOutBuffer返回的类型是由你调用的驱动决定的
板凳
huhu0013 [专家分:160] 发布于 2008-04-13 01:38:00
驱动部分也是我自己写的,我从驱动里返回了一些字符串,
可是我不知道怎么从DeviceIoControl函数的lpOutBuffer参数里
获取这些字符串。
楼上能写出这样的代码作为例子吗?
3 楼
tanchuhan [专家分:15140] 发布于 2008-04-13 02:09:00
你把你驱动那里响应DeviceIoControl的函数代码贴出来分析。
这个可不简单,例如你返回char类型数组,那lpOutBuffer可以直接声明为ByVal lpOutBuffer As String,如果你返回的是WCHAR,又另当别论了
4 楼
huhu0013 [专家分:160] 发布于 2008-04-13 09:47:00
哦,恰巧我用的是WCHAR,
难怪我把lpOutBuffer的定义改为ByVal lpOutBuffer As String也不行。
下面是我在驱动里写的代码:
WCHAR g_swNiceToMeetYou[] = L"Nice to meet you.";
RtlCopyMemory(pIoBuffer, (PVOID)&g_swNiceToMeetYou[0], 42);
请问为何char就可以用ByVal lpOutBuffer As String,而WCHAR不行呢?
对WCHAR,又应该怎么处理呢?
5 楼
tanchuhan [专家分:15140] 发布于 2008-04-13 14:31:00
试一下这样的代码:
WCHAR g_swNiceToMeetYou[] = L"Nice to meet you.";
RtlCopyMemory(pIoBuffer, (PVOID)[color=0000FF]g_swNiceToMeetYou[/color], [color=0000FF]sizeof(g_swNiceToMeetYou[/color])); //这样写简洁正确点,但你原来那样写也没错
in vb:
Dim lpOutBuffer As String
lpOutBuffer = String(512, 0) '分配缓冲区
Call DeviceIoControl(..., ByVal StrPtr(lpOutBuffer), ...)
lpOutBuffer = Left(lpOutBuffer, InStr(lpOutBuffer, vbNullChar))
Debug.Print lpOutBuffer
6 楼
huhu0013 [专家分:160] 发布于 2008-04-13 22:24:00
to tanchuhan:
很感谢你的帮助,我的问题终于解决了。
代码是这样写的:
in VC:
WCHAR g_swNiceToMeetYou[] = L"Nice to meet you.";
RtlCopyMemory(pIoBuffer, (PVOID)g_swNiceToMeetYou, sizeof(g_swNiceToMeetYou));
//你这样写确实更好,谢谢指正。
in VB:
dim outBuffer(1024) as byte,outLen as long,tmpStr as string
DeviceIoControl ZZ,XX,YY,AA,byval varptr(outbuffer(0)),ubound(outbuffer)+1,outlen,BB
if outlen>0 then
tmpStr = outBuffer '通过tmpStr即可在VB中输出从驱动中获得的字符串了
endif
=======================================================================================
你给出的代码我没有实验过,可以分析一下你这样写的道理吗?
7 楼
zcxlhx [专家分:720] 发布于 2008-04-30 16:45:00
真是强,varptr(outbuffer(0))虽然看上去是个数组,但实际上 应该是传递一个字符串指针过去。结果是不是应该加上:
tmpStr=replace(tmpStr,chr(0),"")
能给段用VB加载和启动驱动的代码吗?
我来回复