=============== What I want to wrap: ===================
///////////////////////////////////////////////////////////////////////////////
// MPUSBWrite :
//
// handle - Identifies the endpoint pipe to be written to. The pipe handle
// must have been created with MP_WRITE access attribute.
//
// pData - Points to the buffer containing the data to be written to the pipe.
//
// dwLen - Specifies the number of bytes to write to the pipe.
//
// pLength - Points to the number of bytes written by this function call.
// MPUSBWrite sets this value to zero before doing any work or
// error checking.
//
// dwMilliseconds
// - Specifies the time-out interval, in milliseconds. The function
// returns if the interval elapses, even if the operation is
// incomplete. If dwMilliseconds is zero, the function tests the
// data pipe and returns immediately. If dwMilliseconds is INFINITE,
// the function's time-out interval never elapses.
//
// Note that "input" and "output" refer to the parameter designations in calls
// to this function, which are the opposite of common sense from the
// perspective of an application making the calls.
//
DWORD MPUSBWrite(HANDLE handle, // Input
PVOID pData, // Input
DWORD dwLen, // Input
PDWORD pLength, // Output
DWORD dwMilliseconds) // Input
{ ... }
================ How I wrapped it: ==================
apiUsbWrite: handle data: pData inputLen: dwLen outputLen: pLength milliseconds: dwMilliseconds
<apicall: long '_MPUSBWrite' (long void* long long* long) module: 'mpusbapi.dll'>
^self externalCallFailed
=================== How I am Calling it: ======================
write: aByteArray delay: aDelay
| bytesWritten |
bytesWritten := 0.
self class
apiUsbWrite: writePipe
data: aByteArray
inputLen: aByteArray size
outputLen: bytesWritten
milliseconds: aDelay.
^ bytesWritten
write: aByteArray timeout: aDelay
| bytesWrittenHandle status |
bytesWrittenHandle := ByteArray new: ExternalType long byteSize.
status := self apiUsbWriteTo: writePipe
buffer: (ExternalData fromHandle: aByteArray type: ExternalType void)
bytesToWrite: aByteArray size
bytesWritten: (ExternalData fromHandle: bytesWrittenHandle type: ExternalType long)
timeout: aDelay.
status = 1 ifFalse: [self error: 'Failed reading from device'].
^ bytesWrittenHandle unsignedLongAt: 1
=================== The Right Way To Call It: ======================
write: aByteArray delay: aDelay
| bytesWritten |
bytesWritten := WordArray with: 0.
self class
apiUsbWrite: writePipe
data: aByteArray
inputLen: aByteArray size
outputLen: bytesWritten
milliseconds: aDelay.
^bytesWritten at: 1
I'm suggesting you write something like (I'm sure this is not quite right)
DWORD MyMPUSBWrite(HANDLE handle, // Input
PVOID pData, // Input
DWORD dwLen, // Input
DWORD dwMilliseconds) // Input
{
DWORD returnVal;
PDWORD pLength = (PDWORD)malloc(sizeof pLength);
returnVal = MPUSBWrite(handle, pData, dwLen, dwMilliseconds);
return *pLength;
}I tried calling it like this before:
write: aByteArray delay: aDelay
| outputLen |
outputLen := ExternalData
fromHandle: (ExternalAddress gcallocate: 4)
type: ExternalType long asPointerType.
self class
apiUsbWrite: writePipe
data: aByteArray
inputLen: aByteArray size
outputLen: outputLen
milliseconds: aDelay.
^ outputLen