VB depth Dive sea snake winding

Source: Internet
Author: User
Tags continue empty requires textout
Dive Depth (10 meters, water temperature 0°c), time of 0 o'clock the whole.
"Sea Snake" someone is exclaim.

I saw a long Black Sea snake swimming in the depths of the sea, and almost all of them were in a daze. But the sea snake said a let us frightened words, "I am a well-known VB string, if you continue to dive, you have to want to pass me."

I habitually observed it, because I knew that to defeat an opponent, I must understand the opponent in depth.

So I wrote a little code to understand it.

Dim Str as String



str = "Give me a Belle"

Dim L1 as Long

Dim L2 as Long

L1 = Len (str)

L2 = LenB (str)

Debug.Print L1, L2

This code works well, exactly as I imagined, showing 12, 24 in the Immediate window,

Good, so I'm going to print it out in the window with TextOut, so why do I have to have this function instead of the other, do you care? So I wrote down the code.

Dim Str as String



str = "Give me a Belle"

Dim L1 as Long

Dim L2 as Long

L1 = Len (str)

L2 = LenB (str)

Debug.Print L1, L2

ME.HDC, TextOut, str, L1

And I found it wrong? It seems that the string length is wrong, think of it, in VB string is BSTR type, then should use L2 for length, right, try to know.

God, after the string appeared in the heavenly book, is it a hint of the Lord when I give me a beautiful woman?

But I don't think God would have promised me so soon, so there must be something wrong with my code.

When I was meditating, all the people were watching me. No way, too handsome.

So I quickly wrote down the following code

Private Declare Function lenansi Lib "kernel32" Alias "Lstrlena" (ByVal string1 as String) as Long

......

Dim Str as String



str = "Give me a Belle"

Dim Truelen as Long

Truelen = Lenansi (str)

ME.HDC, TextOut, str, Truelen

When I quickly pressed the F5, the sky echoed, "Give me a beautiful" this sentence has been the correct response.

At this time I looked at the sea snake, full of confidence to swim to it, but it is a very secretive smile.

Have you ever seen a snake smile, it also slowly swam over.

"If you're going to pass me this way, it's too easy," he said. Did anyone in the gold 12 house say that?

"Give you a little test, you know vbNullString and" "What's the difference? ”

"Don't think you're a sea snake, you're a very simple question." I am surrounded by a good-looking young man, a nickname (the world's seventh handsome), "You think I have not read the Guide to Life" (Object Browser)

Const vbnullstring = ""

Vba. Members of the Constants

Constants used when calling an external procedure that requires a string that is not a value of 0

"Then you mean to say the same, then vbNullChar?" "Sea snakes look at the world's seventh handsome in a hostile way."

"Of course it is the same, you see the explanation of VB" The world's seventh handsome blurted out, but he felt like some wrong. But VB's Object Browser does say

Const vbNullChar = ""

Vba. Members of the Constants

So what does the following code represent?

The sea snake gave it the code.

Dim S1 as String

Dim S2 as String

Dim S3 as String



S1 = vbNullString

S2 = vbNullChar

S3 = ""



Debug.Print StrPtr (S1), StrPtr (S2), StrPtr (S3)

Debug.Print LenB (S1), LenB (S2), LenB (S3)

The world's seventh Shuai pressed F5, and he was surprised by the results of the sea snake code running

0 1899284 1434596

0 2 0

Then there must be a mistake between the description of VB and sea snake. Moreover, for the use of S1 two values are zero, the pointer points to zero, the length of zero, it is not a common sense of the 0 value AH.

The world's seventh Shuai turned his head and looked at me.

"Boy, come on, let me show you something first."

"If you want to defeat the sea serpent, you must understand the structure of the sea serpent."

The VB string is a standard BSTR string, such as the "Hello" string, which is structured like this

A 0
0
0
H
0
E
0
L
0
L
0
' O '
0
0
0


You can see that the preceding four bytes represent the number of bytes in the actual length of the string, which is a long value.

The last two bytes are the end character representing the 0 value.

The middle 10 byte is exactly the content of the string.

If we use s1= "Hello", then where does S1 point?

"Initially I thought S1 was pointing to the first byte, but when I looked at the byte values followed by StrPtr (S1) using the custom VB Memory Observation tool, S1 was pointing to the fifth byte, where our string really started. ”

"We all know the structure of the sea snake, so why are the three things that it has just put forward?" "I am still in a daze of the diver."

"Yes, why?" ”

Well, take out your oleview, open the VB6.DLL in file-> View typelib, do you see a wonderful world, don't be in a daze, find the following parts

Modules->modules Constants

Open them and you will see the real definition of VB inside.

[HelpContext (0X0010AA32)] Const LPSTR vbnullstring = "";

[HelpContext (0X0010AA32)] Const LPSTR vbNullChar = "the";

See no, vbNullString points to an empty string, but this empty string is 0 addresses. vbNullChar is a 0 character (equivalent to the last character in a string in C). So let's see what happens when the sea snake code is running.

S1 = vbNullString

VB saw this sentence, it is very clear that the value of S1 into 0

S2 = vbNullChar

VB saw this sentence, it did a few actions, it used SysAllocStringLen in the heap to allocate a BSTR string, and then "" "Copy to this string.

S3 = ""

VB here to do a lot of work, first of all, VB in compiling, "" as a constant, it must be for this empty string internal application for a variable. When the exe file is loaded, it also has to set an address, although it does not represent

is a string of 00 00 00 00 00 00, which requires 6 bytes (four head bytes and 2 bytes)

And then, when you see this sentence, pass the fifth byte address value to S3.

So, although you just write a "", the results of VB more work. 6 bytes is not much, but in a big project, everyone is writing "", it is also a considerable cost. So next time you should never use the "", but must use vbNullString

"You say, yes, sea serpent," I looked at it contemptuously.

The sea serpent saw that everyone had a sudden epiphany, and this made it laugh. )

"Understand me, not really control me, many C divers will be dismissive of VB, you know why?" ”

"Slow, VB string operation is too slow" many divers are rushing back to answer this question.

"Oh, yes," said the serpent, laughing, slowly swimming to the deep sea "I'll back!"

Leave us this group of diving people, we are thinking, we really understand the sea snakes, because the VB Ocean legend Sea snakes is quite scary, it will so easily go away, and, it said I'll back is what?

But we will continue to dive.

"Look, coral reefs!" exclaimed the man.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.