我的客户提供了一个COM组件:usbiocom.dll给我。
通过帮助文档查看,里边包含一系列接口函数,其中一个接口如1楼图。
我的USB设备的GUID是Class=USBIOControlledDevices
ClassGUID={96e73b6e-7a5a-11d4-9f24-0080c82727f4}
请各位大侠帮忙,困扰很久了,谢谢。
5 个解决方案
#1
#2
用VB6作宿主调用COM库最方便。
楼主非要用C++的话,参考下面:
楼主非要用C++的话,参考下面:
1.在VC中新建一控制台程序,选支持MFC(当然,你也可以不选择支持MFC的,不过会很麻烦)
2.按CTRL+W调出MFC ClassWizard,Add Class->From a type library,选择你的word的类型库
(例如我的是word2003,安装在e盘,我的路径是"e:\edittools\microsoft office\office11\msword.olb"),
选择完毕后,在弹出的窗口中选择要让classwizard生成的包装类,在本例中要用到
_Application,
Documents,
_Document,
Range
这四个类,选中他们后按OK
3.进入你的main函数所在的cpp文件,加入头文件引用
#include "msword.h" //引用刚才classwizard生成的idispatch包装类
4.加入代码
// console_word.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "console_word.h"
#include "msword.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
printf(_T("Fatal Error: MFC initialization failed!\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
if (CoInitialize(NULL) != S_OK)
{
AfxMessageBox("初始化COM支持库失败!");
return -1;
}
_Application wordApp;
Documents docs;
_Document doc;
Range aRange;
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CString txt;
wordApp.CreateDispatch("Word.Application",NULL);
wordApp.SetVisible(FALSE);
docs=wordApp.GetDocuments();
doc=docs.Open(COleVariant("c:\\new\\测试.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
aRange=doc.Range(vOpt,vOpt);
txt=aRange.GetText();
AfxMessageBox(txt);//这里GetText得到的就是word文件的纯文本了,你可以将其写到txt文件中
printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的换行不是\r\n而是\r,所以需要输出重定向到文本文件看结果。
aRange.ReleaseDispatch();
doc.Close(vOpt,vOpt,vOpt);
doc.ReleaseDispatch();
docs.ReleaseDispatch();
wordApp.Quit(vOpt,vOpt,vOpt);
wordApp.ReleaseDispatch();
CoUninitialize();
}
return nRetCode;
}
#3
我现在使用了如下代码打开USB接口:
HDEVINFO DevInfo;
GUID g_UsbioID=USBIO_IID;
SP_DEVICE_INTERFACE_DATA DevData;
SP_INTERFACE_DEVICE_DETAIL_DATA *DevDetail = NULL;
DWORD ReqLen;
DevInfo=SetupDiGetClassDevs(&g_UsbioID,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);
SetupDiEnumDeviceInterfaces(DevInfo, NULL,&g_UsbioID, DEVICE_NUMBER, &DevData );
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, NULL,0, &ReqLen, NULL);
DevDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(ReqLen);
// now get the detailed device information
DevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, DevDetail,ReqLen, &ReqLen, NULL);
// open the device, use OVERLAPPED flag if necessary
// use DevDetail->DevicePath as device name
hdle = CreateFile(DevDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
不知为什么始终无法获取到设备信息,GUID是正确的
HDEVINFO DevInfo;
GUID g_UsbioID=USBIO_IID;
SP_DEVICE_INTERFACE_DATA DevData;
SP_INTERFACE_DEVICE_DETAIL_DATA *DevDetail = NULL;
DWORD ReqLen;
DevInfo=SetupDiGetClassDevs(&g_UsbioID,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);
SetupDiEnumDeviceInterfaces(DevInfo, NULL,&g_UsbioID, DEVICE_NUMBER, &DevData );
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, NULL,0, &ReqLen, NULL);
DevDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(ReqLen);
// now get the detailed device information
DevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, DevDetail,ReqLen, &ReqLen, NULL);
// open the device, use OVERLAPPED flag if necessary
// use DevDetail->DevicePath as device name
hdle = CreateFile(DevDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
不知为什么始终无法获取到设备信息,GUID是正确的
#4
HDEVINFO DevInfo;
GUID g_UsbioID=USBIO_IID;
SP_DEVICE_INTERFACE_DATA DevData;
SP_INTERFACE_DEVICE_DETAIL_DATA *DevDetail = NULL;
DWORD ReqLen;
DevInfo=SetupDiGetClassDevs(&g_UsbioID,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);
SetupDiEnumDeviceInterfaces(DevInfo, NULL,&g_UsbioID, DEVICE_NUMBER, &DevData );
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, NULL,0, &ReqLen, NULL);
DevDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(ReqLen);
// now get the detailed device information
DevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, DevDetail,ReqLen, &ReqLen, NULL);
// open the device, use OVERLAPPED flag if necessary
// use DevDetail->DevicePath as device name
hdle = CreateFile(DevDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
#5
找客户要个sample 代码看看。一般提供dll的,多少都有点例子的吧
#1
#2
用VB6作宿主调用COM库最方便。
楼主非要用C++的话,参考下面:
楼主非要用C++的话,参考下面:
1.在VC中新建一控制台程序,选支持MFC(当然,你也可以不选择支持MFC的,不过会很麻烦)
2.按CTRL+W调出MFC ClassWizard,Add Class->From a type library,选择你的word的类型库
(例如我的是word2003,安装在e盘,我的路径是"e:\edittools\microsoft office\office11\msword.olb"),
选择完毕后,在弹出的窗口中选择要让classwizard生成的包装类,在本例中要用到
_Application,
Documents,
_Document,
Range
这四个类,选中他们后按OK
3.进入你的main函数所在的cpp文件,加入头文件引用
#include "msword.h" //引用刚才classwizard生成的idispatch包装类
4.加入代码
// console_word.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "console_word.h"
#include "msword.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
printf(_T("Fatal Error: MFC initialization failed!\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
if (CoInitialize(NULL) != S_OK)
{
AfxMessageBox("初始化COM支持库失败!");
return -1;
}
_Application wordApp;
Documents docs;
_Document doc;
Range aRange;
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CString txt;
wordApp.CreateDispatch("Word.Application",NULL);
wordApp.SetVisible(FALSE);
docs=wordApp.GetDocuments();
doc=docs.Open(COleVariant("c:\\new\\测试.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
aRange=doc.Range(vOpt,vOpt);
txt=aRange.GetText();
AfxMessageBox(txt);//这里GetText得到的就是word文件的纯文本了,你可以将其写到txt文件中
printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的换行不是\r\n而是\r,所以需要输出重定向到文本文件看结果。
aRange.ReleaseDispatch();
doc.Close(vOpt,vOpt,vOpt);
doc.ReleaseDispatch();
docs.ReleaseDispatch();
wordApp.Quit(vOpt,vOpt,vOpt);
wordApp.ReleaseDispatch();
CoUninitialize();
}
return nRetCode;
}
#3
我现在使用了如下代码打开USB接口:
HDEVINFO DevInfo;
GUID g_UsbioID=USBIO_IID;
SP_DEVICE_INTERFACE_DATA DevData;
SP_INTERFACE_DEVICE_DETAIL_DATA *DevDetail = NULL;
DWORD ReqLen;
DevInfo=SetupDiGetClassDevs(&g_UsbioID,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);
SetupDiEnumDeviceInterfaces(DevInfo, NULL,&g_UsbioID, DEVICE_NUMBER, &DevData );
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, NULL,0, &ReqLen, NULL);
DevDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(ReqLen);
// now get the detailed device information
DevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, DevDetail,ReqLen, &ReqLen, NULL);
// open the device, use OVERLAPPED flag if necessary
// use DevDetail->DevicePath as device name
hdle = CreateFile(DevDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
不知为什么始终无法获取到设备信息,GUID是正确的
HDEVINFO DevInfo;
GUID g_UsbioID=USBIO_IID;
SP_DEVICE_INTERFACE_DATA DevData;
SP_INTERFACE_DEVICE_DETAIL_DATA *DevDetail = NULL;
DWORD ReqLen;
DevInfo=SetupDiGetClassDevs(&g_UsbioID,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);
SetupDiEnumDeviceInterfaces(DevInfo, NULL,&g_UsbioID, DEVICE_NUMBER, &DevData );
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, NULL,0, &ReqLen, NULL);
DevDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(ReqLen);
// now get the detailed device information
DevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, DevDetail,ReqLen, &ReqLen, NULL);
// open the device, use OVERLAPPED flag if necessary
// use DevDetail->DevicePath as device name
hdle = CreateFile(DevDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
不知为什么始终无法获取到设备信息,GUID是正确的
#4
HDEVINFO DevInfo;
GUID g_UsbioID=USBIO_IID;
SP_DEVICE_INTERFACE_DATA DevData;
SP_INTERFACE_DEVICE_DETAIL_DATA *DevDetail = NULL;
DWORD ReqLen;
DevInfo=SetupDiGetClassDevs(&g_UsbioID,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);
SetupDiEnumDeviceInterfaces(DevInfo, NULL,&g_UsbioID, DEVICE_NUMBER, &DevData );
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, NULL,0, &ReqLen, NULL);
DevDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(ReqLen);
// now get the detailed device information
DevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(DevInfo, &DevData, DevDetail,ReqLen, &ReqLen, NULL);
// open the device, use OVERLAPPED flag if necessary
// use DevDetail->DevicePath as device name
hdle = CreateFile(DevDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
#5
找客户要个sample 代码看看。一般提供dll的,多少都有点例子的吧