📄 asyncapi.c
字号:
LocalFree(pAsyncRead);
TRACE(TL_TRACE, (hWnd, "Exit AsyncRead = %d\r\n", dwRet));
return(dwRet);
} // AsyncRead
ULONG
WINAPI
AsyncWrite(
HWND hWnd,
PSTR szDeviceName,
PASYNC_WRITE asyncWrite,
BOOL bAutoAlloc
)
{
HANDLE hDevice;
DWORD dwRet, dwBytesRet;
PASYNC_WRITE pAsyncWrite;
ULONG ulBufferSize;
ULONG i;
TRACE(TL_TRACE, (hWnd, "Enter AsyncWrite\r\n"));
TRACE(TL_TRACE, (hWnd, "bRawMode = %d\r\n", asyncWrite->bRawMode));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_ID.NA_Bus_Number = 0x%x\r\n", asyncWrite->DestinationAddress.IA_Destination_ID.NA_Bus_Number));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_ID.NA_Node_Number = 0x%x\r\n", asyncWrite->DestinationAddress.IA_Destination_ID.NA_Node_Number));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_Offset.Off_High = 0x%x\r\n", asyncWrite->DestinationAddress.IA_Destination_Offset.Off_High));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_Offset.Off_Low = 0x%x\r\n", asyncWrite->DestinationAddress.IA_Destination_Offset.Off_Low));
TRACE(TL_TRACE, (hWnd, "nNumberOfBytesToWrite = 0x%x\r\n", asyncWrite->nNumberOfBytesToWrite));
TRACE(TL_TRACE, (hWnd, "nBlockSize = 0x%x\r\n", asyncWrite->nBlockSize));
TRACE(TL_TRACE, (hWnd, "fulFlags = 0x%x\r\n", asyncWrite->fulFlags));
TRACE(TL_TRACE, (hWnd, "ulGeneration = 0x%x\r\n", asyncWrite->ulGeneration));
ulBufferSize = sizeof(ASYNC_WRITE) + asyncWrite->nNumberOfBytesToWrite;
if (bAutoAlloc) {
pAsyncWrite = (PASYNC_WRITE)LocalAlloc(LPTR, ulBufferSize);
if (!pAsyncWrite) {
dwRet = GetLastError();
TRACE(TL_ERROR, (hWnd, "Could not allocate pAsyncWrite\r\n"));
goto Exit_AsyncWrite;
}
FillMemory(pAsyncWrite, ulBufferSize, 0);
*pAsyncWrite = *asyncWrite;
for (i=0; i<asyncWrite->nNumberOfBytesToWrite/sizeof(ULONG); i++) {
CopyMemory((ULONG *)&pAsyncWrite->Data+i, (ULONG *)&i, sizeof(ULONG));
}
}
else
pAsyncWrite = asyncWrite;
for (i=0; i<(asyncWrite->nNumberOfBytesToWrite/sizeof(ULONG)); i++) {
PULONG ulTemp;
ulTemp = (PULONG)&pAsyncWrite->Data[i*sizeof(ULONG)];
TRACE(TL_TRACE, (hWnd, "Quadlet[0x%x] = 0x%x\r\n", i, *ulTemp));
}
hDevice = OpenDevice(hWnd, szDeviceName, FALSE);
if (hDevice != INVALID_HANDLE_VALUE) {
dwRet = DeviceIoControl( hDevice,
IOCTL_ASYNC_WRITE,
pAsyncWrite,
ulBufferSize,
pAsyncWrite,
ulBufferSize,
&dwBytesRet,
NULL
);
if (!dwRet) {
dwRet = GetLastError();
TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
}
else {
dwRet = ERROR_SUCCESS;
}
// free up resources
CloseHandle(hDevice);
}
else {
dwRet = GetLastError();
TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
}
Exit_AsyncWrite:
if (pAsyncWrite && bAutoAlloc)
LocalFree(pAsyncWrite);
TRACE(TL_TRACE, (hWnd, "Exit AsyncWrite = %d\r\n", dwRet));
return(dwRet);
} // AsyncWrite
ULONG
WINAPI
AsyncLock(
HWND hWnd,
PSTR szDeviceName,
PASYNC_LOCK asyncLock
)
{
HANDLE hDevice;
DWORD dwRet, dwBytesRet;
TRACE(TL_TRACE, (hWnd, "Enter AsyncLock\r\n"));
TRACE(TL_TRACE, (hWnd, "bRawMode = %d\r\n", asyncLock->bRawMode));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_ID.NA_Bus_Number = 0x%x\r\n", asyncLock->DestinationAddress.IA_Destination_ID.NA_Bus_Number));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_ID.NA_Node_Number = 0x%x\r\n", asyncLock->DestinationAddress.IA_Destination_ID.NA_Node_Number));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_Offset.Off_High = 0x%x\r\n", asyncLock->DestinationAddress.IA_Destination_Offset.Off_High));
TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_Offset.Off_Low = 0x%x\r\n", asyncLock->DestinationAddress.IA_Destination_Offset.Off_Low));
TRACE(TL_TRACE, (hWnd, "nNumberOfArgBytes = 0x%x\r\n", asyncLock->nNumberOfArgBytes));
TRACE(TL_TRACE, (hWnd, "nNumberOfDataBytes = 0x%x\r\n", asyncLock->nNumberOfDataBytes));
TRACE(TL_TRACE, (hWnd, "fulTransactionType = 0x%x\r\n", asyncLock->fulTransactionType));
TRACE(TL_TRACE, (hWnd, "fulFlags = 0x%x\r\n", asyncLock->fulFlags));
TRACE(TL_TRACE, (hWnd, "Arguments[0] = 0x%x\r\n", asyncLock->Arguments[0]));
TRACE(TL_TRACE, (hWnd, "Arguments[1] = 0x%x\r\n", asyncLock->Arguments[1]));
TRACE(TL_TRACE, (hWnd, "DataValues[0] = 0x%x\r\n", asyncLock->DataValues[0]));
TRACE(TL_TRACE, (hWnd, "DataValues[1] = 0x%x\r\n", asyncLock->DataValues[1]));
TRACE(TL_TRACE, (hWnd, "ulGeneration = 0x%x\r\n", asyncLock->ulGeneration));
hDevice = OpenDevice(hWnd, szDeviceName, FALSE);
if (hDevice != INVALID_HANDLE_VALUE) {
dwRet = DeviceIoControl( hDevice,
IOCTL_ASYNC_LOCK,
asyncLock,
sizeof(ASYNC_LOCK),
asyncLock,
sizeof(ASYNC_LOCK),
&dwBytesRet,
NULL
);
if (!dwRet) {
dwRet = GetLastError();
TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
}
else {
TRACE(TL_TRACE, (hWnd, "Buffer[0] = 0x%x\r\n", asyncLock->Buffer[0]));
TRACE(TL_TRACE, (hWnd, "Buffer[1] = 0x%x\r\n", asyncLock->Buffer[1]));
dwRet = ERROR_SUCCESS;
}
// free up resources
CloseHandle(hDevice);
}
else {
dwRet = GetLastError();
TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
}
TRACE(TL_TRACE, (hWnd, "Exit AsyncLock = %d\r\n", dwRet));
return(dwRet);
} // AsyncLock
ULONG
WINAPI
AsyncStream(
HWND hWnd,
PSTR szDeviceName,
PASYNC_STREAM asyncStream,
BOOL bAutoAlloc
)
{
HANDLE hDevice;
DWORD dwRet, dwBytesRet;
PASYNC_STREAM pAsyncStream;
ULONG ulBufferSize;
ULONG i;
TRACE(TL_TRACE, (hWnd, "Enter AsyncStream\r\n"));
TRACE(TL_TRACE, (hWnd, "nNumberOfBytesToStream = 0x%x\r\n", asyncStream->nNumberOfBytesToStream));
TRACE(TL_TRACE, (hWnd, "fulFlags = 0x%x\r\n", asyncStream->fulFlags));
TRACE(TL_TRACE, (hWnd, "ulTag = 0x%x\r\n", asyncStream->ulTag));
TRACE(TL_TRACE, (hWnd, "nChannel = 0x%x\r\n", asyncStream->nChannel));
TRACE(TL_TRACE, (hWnd, "ulSynch = 0x%x\r\n", asyncStream->ulSynch));
TRACE(TL_TRACE, (hWnd, "nSpeed = 0x%x\r\n", asyncStream->nChannel));
ulBufferSize = sizeof(ASYNC_STREAM) + asyncStream->nNumberOfBytesToStream;
if (bAutoAlloc) {
pAsyncStream = (PASYNC_STREAM)LocalAlloc(LPTR, ulBufferSize);
if (!pAsyncStream) {
dwRet = GetLastError();
return (dwRet);
}
FillMemory(pAsyncStream, ulBufferSize, 0);
*pAsyncStream = *asyncStream;
for (i=0; i<asyncStream->nNumberOfBytesToStream/sizeof(ULONG); i++) {
CopyMemory((ULONG *)&pAsyncStream->Data+i, (ULONG *)&i, sizeof(ULONG));
}
}
else
pAsyncStream = asyncStream;
for (i=0; i<(asyncStream->nNumberOfBytesToStream/sizeof(ULONG)); i++) {
PULONG ulTemp;
ulTemp = (PULONG)&pAsyncStream->Data[i];
TRACE(TL_TRACE, (hWnd, "Quadlet[0x%x] = 0x%x\r\n", i, *ulTemp));
}
hDevice = OpenDevice(hWnd, szDeviceName, FALSE);
if (hDevice != INVALID_HANDLE_VALUE) {
dwRet = DeviceIoControl( hDevice,
IOCTL_ASYNC_STREAM,
pAsyncStream,
ulBufferSize,
pAsyncStream,
ulBufferSize,
&dwBytesRet,
NULL
);
if (!dwRet) {
dwRet = GetLastError();
TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
}
else {
dwRet = ERROR_SUCCESS;
}
// free up resources
CloseHandle(hDevice);
}
else {
dwRet = GetLastError();
TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
}
if (pAsyncStream && bAutoAlloc)
LocalFree(pAsyncStream);
TRACE(TL_TRACE, (hWnd, "Exit AsyncStream = %d\r\n", dwRet));
return(dwRet);
} // AsyncStream
void
WINAPI
AsyncStartLoopback(
HWND hWnd,
PSTR szDeviceName,
PASYNC_LOOPBACK_PARAMS asyncLoopbackParams
)
{
asyncLoopbackParams->szDeviceName = szDeviceName;
asyncLoopbackParams->hWnd = hWnd;
asyncLoopbackParams->bKill = FALSE;
asyncLoopbackParams->hThread = CreateThread( NULL,
0,
AsyncLoopbackThread,
(LPVOID) asyncLoopbackParams,
0,
&asyncLoopbackParams->ThreadId
);
} // AsyncStartLoopback
void
WINAPI
AsyncStopLoopback(
PASYNC_LOOPBACK_PARAMS asyncLoopbackParams
)
{
// kill the thread
asyncLoopbackParams->bKill = TRUE;
// wait for the thread to exit before returning
WaitForSingleObjectEx(asyncLoopbackParams->hThread,
INFINITE,
FALSE);
// close the handle
CloseHandle(asyncLoopbackParams->hThread);
return;
} // AsyncStopLoopback
DWORD
WINAPI
AsyncLoopbackThread(
LPVOID lpParameter
)
{
PASYNC_LOOPBACK_PARAMS pLoopbackParams = NULL;
PASYNC_READ pAsyncRead = NULL;
PASYNC_WRITE pAsyncWrite = NULL;
PVOID pAsyncLock = NULL;
HANDLE hDevice;
DWORD dwRet, dwBytesRet;
ULONG ulReadSize;
ULONG ulWriteSize;
ULONG ulLockSize;
ULONG ulBufferSize;
BOOLEAN bFailed = FALSE;
// get pointer to our thread parameters
pLoopbackParams = (PASYNC_LOOPBACK_PARAMS)lpParameter;
// reset pass/fail iteration counts
pLoopbackParams->ulPass = 0;
pLoopbackParams->ulFail = 0;
pLoopbackParams->ulIterations = 0;
// try to open the device
hDevice = OpenDevice(pLoopbackParams->hWnd, pLoopbackParams->szDeviceName, FALSE);
// device opened, so let's do loopback
if (hDevice != INVALID_HANDLE_VALUE) {
// lets get our buffers setup for whatever loopback we're doing.
// see if we want random sized buffers...
if (pLoopbackParams->ulLoopFlag & ASYNC_LOOPBACK_RANDOM_LENGTH) {
// lets get a random buffer size that's under our max bytes to read
srand((unsigned) time(NULL));
}
// write...
if (pLoopbackParams->ulLoopFlag & ASYNC_LOOPBACK_WRITE) {
ulWriteSize = sizeof(ASYNC_WRITE) + pLoopbackParams->nMaxBytes;
pAsyncWrite = (PASYNC_WRITE)LocalAlloc(LPTR, ulWriteSize);
if (!pAsyncWrite) {
dwRet = GetLastError();
TRACE(TL_ERROR, (pLoopbackParams->hWnd, "Could not allocate pAsyncWrite\r\n"));
goto Exit_AsyncLoopbackThread;
}
FillMemory(pAsyncWrite, ulWriteSize, 0);
// setup our values in the write buffer
pAsyncWrite->DestinationAddress.IA_Destination_Offset.Off_High =
pLoopbackParams->AddressOffset.Off_High;
pAsyncWrite->DestinationAddress.IA_Destination_Offset.Off_Low =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -