📄 vcdll.cpp
字号:
// vcdll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "vcdll.h"
extern "C" int APIENTRY WriteUsb(char SendBuff[],int count);//
extern "C" BSTR APIENTRY ReadUsb(int count);
//extern static int GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath);
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CVcdllApp
BEGIN_MESSAGE_MAP(CVcdllApp, CWinApp)
//{{AFX_MSG_MAP(CVcdllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVcdllApp construction
CVcdllApp::CVcdllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CVcdllApp object
CVcdllApp theApp;
static int GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
{
HDEVINFO hDevInfoSet;
SP_DEVICE_INTERFACE_DATA ifdata;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
int nCount;
BOOL bResult;
HANDLE uu;
uu=INVALID_HANDLE_VALUE;
// 取得一个该GUID相关的设备信息集句柄
hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
NULL, // 无关键字
NULL, // 不指定父窗口句柄
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
// 失败...
if (hDevInfoSet == INVALID_HANDLE_VALUE)
{
return 0;
}
// 申请设备接口数据空间
pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
nCount = 0;
bResult = TRUE;
// 设备序号=0,1,2... 逐一测试设备接口,到失败为止
while (bResult)
{
ifdata.cbSize = sizeof(ifdata);
// 枚举符合该GUID的设备接口
bResult = ::SetupDiEnumDeviceInterfaces(
hDevInfoSet, // 设备信息集句柄
NULL, // 不需额外的设备描述
lpGuid, // GUID
(ULONG)nCount, // 设备信息集里的设备序号
&ifdata); // 设备接口信息
if (bResult)
{
// 取得该设备接口的细节(设备路径)
bResult = SetupDiGetInterfaceDeviceDetail(
hDevInfoSet, // 设备信息集句柄
&ifdata, // 设备接口信息
pDetail, // 设备接口细节(设备路径)
INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
NULL, // 不需计算输出缓冲区大小(直接用设定值)
NULL); // 不需额外的设备描述
if (bResult)
{
// 复制设备路径到输出缓冲区
::strcpy(pszDevicePath[nCount], pDetail->DevicePath);
// 调整计数值
nCount++;
}
}
}
// 释放设备接口数据空间
::GlobalFree(pDetail);
//::setupdigetdevicein
// 关闭设备信息集句柄
bResult=::SetupDiDestroyDeviceInfoList(hDevInfoSet);
return nCount;
}
//extern "C" int APIENTRY sum2(int i)
extern "C" int APIENTRY WriteUsb(char SendBuff[],int count)
{
const GUID DiskClassGuid1= {0x77f49320, 0x16ef, 0x11d2,{0xad, 0x51, 0x00, 0x60, 0x97, 0xb5, 0x14, 0xdd}};
// char SendBuff[16];
// IO_BLOCK ioBlock;
BOOL bResult; // results flag
ULONG nBytes;
int i;
const MAX_DEVICE=2;
char* szDevicePath[MAX_DEVICE]; // 设备路径
int nDevice;
HANDLE hDevice;
// 分配需要的空间
for (i = 0; i < MAX_DEVICE; i++)
{
szDevicePath[i] = new char[256];
}
// 取设备路径
nDevice = GetDevicePath((LPGUID)&DiskClassGuid1, szDevicePath);
strcat (szDevicePath[0],"\\");
strcat (szDevicePath[0],"PIPE01");
hDevice = CreateFile (szDevicePath[0],
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL); // No template file
if (hDevice == INVALID_HANDLE_VALUE)
{
return 0;
}
//SendBuff[3]=val;
bResult = WriteFile(hDevice,
SendBuff,
count,
&nBytes,
NULL);
CloseHandle(hDevice);
return (int)SendBuff[3];
}
//int ReadUsb(int count)
extern "C" BSTR APIENTRY ReadUsb(int count)
{
char *str="1234567890123456";
LPSTR readstr;
const GUID DiskClassGuid1= {0x77f49320, 0x16ef, 0x11d2,{0xad, 0x51, 0x00, 0x60, 0x97, 0xb5, 0x14, 0xdd}};
char SendBuff[16];
// IO_BLOCK ioBlock;
BOOL bResult; // results flag
ULONG nBytes;
int i;
const MAX_DEVICE=2;
char* szDevicePath[MAX_DEVICE]; // 设备路径
int nDevice;
HANDLE hDevice;
readstr="000";
// 分配需要的空间
for (i = 0; i < MAX_DEVICE; i++)
{
szDevicePath[i] = new char[256];
}
// 取设备路径
nDevice = GetDevicePath((LPGUID)&DiskClassGuid1, szDevicePath);
strcat (szDevicePath[0],"\\");
strcat (szDevicePath[0],"PIPE00");
hDevice = CreateFile (szDevicePath[0],
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL); // No template file
if (hDevice == INVALID_HANDLE_VALUE)
{
return SysAllocString((BSTR)str);
}
bResult = ReadFile(hDevice,
SendBuff,
count,
&nBytes,
NULL);
CloseHandle(hDevice);
readstr="1234";
str=(char*)SendBuff;
return SysAllocString((BSTR)str);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -