| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 585 人关注过本帖
标题:[求助]从c++ builder到visual c++
只看楼主 加入收藏
jynkl
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-9-10
收藏
 问题点数:0 回复次数:0 
[求助]从c++ builder到visual c++

编译环境: Borland C++ Builder 6.0, Windows Server 2003 DDK

运行环境: Win98/2000/XP/2003

下面是访问 USB 口必备的函数:

#include <vcl.h>

#include <dir.h>

#include <setupapi.h>

#include "C:/WINDDK/3790/inc/ddk/w2k/usbdi.h"

#include "C:/WINDDK/3790/inc/ddk/w2k/devioctl.h"

#include <initguid.h>

//---------------------------------------------------------------------------

// 下面必须为驱动程序的 GUID 值, 这里我乱写的数

DEFINE_GUID(USB_DRIVER_GUID, 0x12345678,0xabcd,0x1122,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00);

//---------------------------------------------------------------------------

HANDLE OpenOneDevice(HDEVINFO hDvcInfo, PSP_INTERFACE_DEVICE_DATA DvcInfoData, char *sDevNameBuf)

{

HANDLE hOut = INVALID_HANDLE_VALUE;

ULONG iReqLen = 0;

SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, NULL, 0, &iReqLen, NULL);

ULONG iDevDataLen = iReqLen; //sizeof(SP_FNCLASS_DEVICE_DATA) + 512;

PSP_INTERFACE_DEVICE_DETAIL_DATA pDevData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(iDevDataLen);

pDevData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

if(SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, pDevData, iDevDataLen, &iReqLen, NULL))

{

strcpy(sDevNameBuf, pDevData->DevicePath);

hOut = CreateFile(pDevData->DevicePath,
GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);

}

free(pDevData);

return hOut;

}

//---------------------------------------------------------------------------

HANDLE OpenUsbDevice(const GUID *pGuid, char *sDevNameBuf)

{

HANDLE hOut = INVALID_HANDLE_VALUE;

HDEVINFO hDevInfo = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);

SP_INTERFACE_DEVICE_DATA deviceInfoData;

deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);

ULONG nGuessCount = MAXLONG;

for(ULONG iDevIndex=0; iDevIndex<nGuessCount; iDevIndex++)

{

if(SetupDiEnumDeviceInterfaces(hDevInfo, 0, pGuid, iDevIndex, &deviceInfoData))

{


if((hOut=OpenOneDevice(hDevInfo, &deviceInfoData, sDevNameBuf)) !=
INVALID_HANDLE_VALUE)

break;

}

else if(GetLastError() == ERROR_NO_MORE_ITEMS) //No more items

{

break;

}

}

SetupDiDestroyDeviceInfoList(hDevInfo);

return hOut;

}

//---------------------------------------------------------------------------

bool GetUsbDeviceFileName(const GUID *pGuid, char *sDevNameBuf)

{

HANDLE hDev = OpenUsbDevice(pGuid, sDevNameBuf);

if(hDev != INVALID_HANDLE_VALUE)

{

CloseHandle(hDev);

return true;

}

return false;

}

//---------------------------------------------------------------------------

HANDLE OpenMyDevice()

{

char DeviceName[MAXPATH] = "";

return OpenUsbDevice(&USB_DRIVER_GUID, DeviceName);

}

//---------------------------------------------------------------------------

HANDLE OpenMyDevPipe(const char *PipeName)

{

char DeviceName[MAXPATH] = "";

if(GetUsbDeviceFileName(&USB_DRIVER_GUID, DeviceName))

{

strcat(DeviceName,"\\\\");

strcat(DeviceName,PipeName);

return CreateFile(DeviceName,
GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);

}

return INVALID_HANDLE_VALUE;

}

//---------------------------------------------------------------------------

有了上面的函数就可以访问 USB 口了://打开 USB 口读写, 由驱动程序的 Pipe 名确定

HANDLE hPipe = OpenMyDevPipe("MyPipe1"); //驱动程序里面的 Pipe 名, 对应访问某个端点的 I/O, 这里我乱写的, 需要与驱动一致

if(hPipe != INVALID_HANDLE_VALUE) //打开 Pipe 成功

{

ReadFile(hPipe, Buffer, BufSize, &nBytesRead, NULL); //从 hPipe 里读取数据到 Buffer 里

//WriteFile(hPipe, Buffer, BytesToWrite, &nBytesWritten, NULL); //把 Buffer 里面的 BytesToWrite 字节写入 hPipe

CloseHandle(hPipe);

}

//使用 DeviceIoControl 访问 USB 设备

HANDLE hDevice = OpenMyDevice();

if(hDevice != INVALID_HANDLE_VALUE) //打开设备成功

{

//这些 DeviceIoControl 功能都是由设备定义的, 具体看设备和驱动的资料

if(DeviceIoControl(hDevice, IOCTL_READ_xxxx, &IOBlock, sizeof(IOBLOCK), &c, 1, &nBytes, NULL))

{

//成功

}

CloseHandle(hDevice);
若让这个程序在vc中运行,在那些方面需要改动???
本人刚学vc,希望大家帮忙一下

搜索更多相关主题的帖子: builder visual 
2006-11-22 22:09
快速回复:[求助]从c++ builder到visual c++
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046377 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved