-->

WinSock: How send() a PByte type?

2019-08-03 00:30发布

问题:

Firstly, i want know if the PByte type is equivalent to a BYTE*(byte pointer) in C++. In negative case, what's could be on Delphi that more near to BYTE* of C++?

Well, suppose that i'm right about that PByte is BYTE* (C++), then based on following C++ code, how send() this data type (PByte) correctly using native WinSock?

See:

C++:

SOCKET sock;
BITMAPINFO bmpInfo;
BYTE *bytes = NULL;
BYTE *temp_bytes = NULL;
DWORD workSpaceSize, fragmntWorkSpaceSize, size;

 RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &workSpaceSize, &fragmntWorkSpaceSize);

 bytes = (BYTE *) Alloc(bmpInfo.bmiHeader.biSizeImage);
 temp_bytes = (BYTE *) Alloc(bmpInfo.bmiHeader.biSizeImage);
 BYTE *memory = (BYTE *) Alloc(workSpaceSize);

 RtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, 
             bytes, 
             bmpInfo.bmiHeader.biSizeImage, 
             temp_bytes, 
             bmpInfo.bmiHeader.biSizeImage, 
             2048,
             &size,
             memory);

 free(bytes);
 free(memory);

 if(Send(sock, (char *) temp_bytes, size, 0) <= 0) return;

 free(temp_bytes);

Delphi:

var
 Sock: TSocket;
 bmpInfo: TBitMapInfo;
 bytes: PByte = nil;
 temp_bytes: PByte = nil;
 memory: PByte;
 workSpaceSize, fragmntWorkSpaceSize, Size: Cardinal;

 //...

RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, @workSpaceSize, @fragmntWorkSpaceSize);

bytes := AllocMem(bmpInfo.bmiHeader.biSizeImage);
temp_bytes := AllocMem(bmpInfo.bmiHeader.biSizeImage);
memory := AllocMem(workSpaceSize);

RtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, bytes, bmpInfo.bmiHeader.biSizeImage, 
                  temp_bytes, bmpInfo.bmiHeader.biSizeImage, 2048, @Size, memory);

FreeMem(bytes);
FreeMem(memory);

if send(Sock, temp_bytes^, Size, 0) <= 0 then Exit;

FreeMem(temp_bytes);
  • Reference to RtlGetCompressionWorkSpaceSize() and RtlCompressBuffer() functions in C++.
  • Reference to RtlGetCompressionWorkSpaceSize() and RtlCompressBuffer() functions in Delphi.