?? 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 + -