Re: Copying string to byte array
From: Jim Mack (jmack_at_mdxi.nospam.com)
Date: 01/13/05
- Next message: Rick Rothstein: "Re: VB.NET advantages"
- Previous message: VNN: "Re: Install VB service package"
- In reply to: Sam Hobbs: "Re: Copying string to byte array"
- Next in thread: Sam Hobbs: "Re: Copying string to byte array"
- Reply: Sam Hobbs: "Re: Copying string to byte array"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 13 Jan 2005 12:48:27 -0500
Sam Hobbs wrote:
> "Jim Mack" <jmack@mdxi.nospam.com> wrote in message
> news:O9kLO2Q%23EHA.3700@tk2msftngp13.phx.gbl...
>>
>> VB strings cannot contain arbitrary binary data. This is the reason
>> (stated by MS) that byte arrays were introduced into the language.
>
> Where does Microsoft state that VB strings cannot contain arbitrary
> binary data? I found a KB article that says it will work. See Article
> ID:122289 ("Passing Structures in OLE Automation") that says that
> serializing a structure into a BSTR will work "if both the controller
> and server are 32-bit and both support Unicode", which is what is
> relevant here. Note that for this context, "serializing" means
> storing. Also note that (beginning with VB 4 at least) VB uses the
> BSTR OLE automation data type for strings.
>
> Unless you are aware of something as definitive or more definitive
> stating the opposite, then you should not post comments that waste
> people's time.
It seems to me that it's you who are wasting your own time. If you are
sure of your position and willing to go down that road, why bother
responding?
What I have is many years of experience with OLE, BSTR and VB, which I
count as at least equivalent to MS docs. I'm sure you know at least one
instance where your own experience has led you to question the
correctness, completeness or wisdom of an MS article or example.
In a narrow range of circumstances, and with a full understanding of the
pitfalls -- some of which you won't find in MS docs -- you can use a
BSTR for binary data. But there is nothing to be gained from doing so,
when byte arrays are available. And it's frankly much easier to say
"never" than to try to be exhaustive in laying out the problems.
Most people are not masochistic, and presented with two options, one
complicated and fraught with peril and the other simple and direct, will
choose the latter. I am guilty of oversimplifying, but the advice
remains sound. Take it or don't, it matters not to me.
>> Bottom line: for arbitrary binary data, use byte arrays and never
>> let it touch a VB string -- not for file I/O, not for API parameters
>> -- never.
>
> Then please explain why strings are used for the Platform SDK
> "CryptEncrypt" function's input and output data. At least every
> sample I have seen does, including Microsoft samples. The input and
> output data is the fifth ("pbData") parameter for CryptEncrypt.
Wouldn't I be wasting your time if I explained that? ByRef B(0) As Byte
is nearly a perfect analog of *pbdata. BSTR is very far from a good
analog of that.
> I seldom use the word "wrong" to state explicitly that there is a
> problem with an answer, but in this case, I truly believe this answer
> to definitely be wrong.
Then you are definitely right, since you are defining right and wrong. I
wish you well in your endeavor. You _can_ make it work, if you're very
careful, and in the process you'll probably learn something. But it's an
even bigger waste of time than reading my posts, since byte arrays are
straightforward and carry no baggage.
--
Jim
- Next message: Rick Rothstein: "Re: VB.NET advantages"
- Previous message: VNN: "Re: Install VB service package"
- In reply to: Sam Hobbs: "Re: Copying string to byte array"
- Next in thread: Sam Hobbs: "Re: Copying string to byte array"
- Reply: Sam Hobbs: "Re: Copying string to byte array"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|