I can't manage to upload binary files via WinHTTP (i.e. send post data in byte type), because it appears that there is a problem with function that converts string to bytes - WideCharToMultiByte (I have tried to put CP_UNICODE in CodePage parameter but again it won't convert properly). Can someone help me in solving this?
(...)
EDIT: Finally found a working function here: ToCPString(String, CP_ACP)
Last edited by MikiSoft; Apr 13th, 2015 at 08:58 AM.
You get yourself in trouble when you do things wrong, and trying to use String expressions blindly with binary data is about as wrong as things get.
You don't want a String body payload. That just means numerous gyrations pouring data from glass to glass to glass doing multiple potentially unsafe character encoding conversions.
I fail to see why so many people try to do this and then have to attempt exotic manipulations on top of it, hoping to compensate for the mess. Instead consider using a sort of "binary string builder" as implemented in the attachment.
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Private Const BINARY_CHUNK As Long = 256
Private Binary() As Byte
Private BinaryNext As Long
Private Sub ClearBinary()
Binary = "" 'Creates an initialized but empty Byte array.
ResetBinary
End Sub
Private Sub ResetBinary()
BinaryNext = 0 'Retain allocated space for reuse.
End Sub
Private Sub CatBinary(ByRef Bytes() As Byte)
Dim BytesLen As Long
BytesLen = UBound(Bytes) - LBound(Bytes) + 1
If BinaryNext + BytesLen - 1 > UBound(Binary) Then
If BytesLen > BINARY_CHUNK Then
ReDim Preserve Binary(BinaryNext + BytesLen - 1)
Else
ReDim Preserve Binary(BinaryNext + BINARY_CHUNK - 1)
End If
End If
CopyMemory Binary(BinaryNext), Bytes(LBound(Bytes)), BytesLen
BinaryNext = BinaryNext + BytesLen
End Sub
Private Sub CatBinaryString(ByRef Text As String)
Dim Bytes() As Byte
Bytes = StrConv(Text, vbFromUnicode) 'Safe if Text only has valid 7-bit US-ASCII chars.
CatBinary Bytes
End Sub
Private Sub TrimBinary()
If BinaryNext > 0 Then
ReDim Preserve Binary(BinaryNext - 1)
Else
ClearBinary
End If
End Sub
The attachment is large because it includes a sample PDF document to be wrapped as a payload. The sample program writes the result to disk so you can view it.
Last edited by dilettante; Mar 2nd, 2015 at 01:04 AM.