" Sea snake" someone exclaimed.
I saw a long black sea snake swimming deep under the sea, and almost everyone was in a daze. But the sea snake said something that made us scared, "I am the famous VB string. If you continue to dive, you have to try to pass me."
I observe it habitually, because I know that to defeat an opponent, you must understand the opponent deeply.
So I wrote a small CODE to understand it.
This code runs very well, exactly as I imagined, and displays 12, 24,
Good, so I am going to use TextOut to print it out in the window. As for why I must use this function and not others, do you care? So I wrote the following code
I found something wrong? It seems that the length of the string is wrong. Then I think about it. In VB, the string is of BSTR type, so L2 should be used as the length. Is this right? You will know after trying it.
Oh my God, the Book of Heaven appears after the string. Is it God hinting to me when to give me a beautiful woman?
But I think God will not answer me so soon, so there must be something wrong with my code.
While I was meditating, everyone was watching me. No way, he's so handsome.
So I quickly wrote the following code
When I quickly pressed F5, there was an echo in the sky, and the sentence "Givemea beauty" got the correct response.
At this time, I looked at this sea snake and swam toward it with confidence, but it smiled mysteriously.
Have you ever seen the smile of a snake? It swam over slowly.
"It would be too easy for you to pass me by just like this." Why is this sentence so familiar? Did someone in the Golden Zodiac say this?
"Give you a little test, do you know the difference between vbNullString and "" ?"
"Don't think that you are a great person because you are a sea snake. Your question is too simple." A young man next to me who is not ugly and is nicknamed " The Seventh Handsome Man in the World " said, "You think I haven't read the Guide to Marine Life? " Yeah (Object Browser)
ConstvbNullString=""
Members of VBA.Constants
Constant used when calling an external procedure that requires a non-zero value string
"Then you mean they are the same, but what about vbNullChar?" The sea snake looked at the seventh handsome man in the world with evil intentions.
"Of course it's the same. Did you read the VB instructions?" the seventh handsome man in the world blurted out, but he felt something was wrong. But VB's object browser does say
Members of VBA.Constants
So what does the following code represent?
Sea Serpent gives its code
After the seventh handsome man in the world pressed F5, he was surprised by the results of the Sea Snake code.
018992841434596
020
So that means someone must be wrong between VB's instructions and Sea Snake. Moreover, for S1, both values are zero, the pointer points to zero, and the length is zero. It is not a zero value in the ordinary sense.
The seventh handsome man in the world then turned his head and looked at me.
"Boy, show off. Come on, let me show you something first."
"If you want to defeat the sea snake, you must understand the structure of the sea snake."
VB's string is a standard BSTR string. For example, the structure of the string "Hello" is as follows
->-width="26">0
->-width="37">0
->-width="40">'H
->-width="35">0
->-width="48">'e'
->-width="44">0
->-width="42">'l'
->-width="40">0
->-width="40">'l'
->-width="39">0
->-width="38">'o'
->-width="35">0
->-width="35">0
->-width="38">0->
You can see that the first four bytes represent the number of bytes occupied by the actual length of the string, which is a Long value.
And the last two bytes are the trailing characters representing the zero value.
The ten bytes in the middle are exactly the content of the string.
If we use s1="Hello", then where does s1 point to.
"Initially I thought s1 pointed to the first byte, but when I used my self-written VB memory observation tool to look at the byte value following strptr(s1) , I found that s1 pointed to the fifth byte, which is That’s where our string really begins.”
"Everyone knows the structure of the sea snake, so why are the three situations it just proposed different?" I asked the diver who was still in a daze.
"Yeah, why?"
Okay, take out your OleView and open VB6.DLL in File->ViewTypeLib. Have you seen a wonderful world? Don’t be in a daze and find the following parts.
Open them and you will see the real definitions inside VB.
Did you see that vbNullString points to an empty string, but this empty string has a zero address . And vbNullChar is a zero character (equivalent to the last character in a string in C). So let’s take a look at what happens when the Sea Snake code is running.
When VB saw this sentence, it clearly changed the value of S1 to zero.
When VB saw this sentence, it took several actions. It used SysAllocStringLen to allocate a BSTR string in the heap, and then copied '/0' to this string.
VB has done a lot of work here. First of all, VB treats "" as a constant when compiling, and it must allocate a variable internally for this empty string. When the EXE file is loaded, it must also be set to an address, although it does not mean anything.
is a string like 000000000000, which requires 6 bytes (four header bytes and 2 tail bytes)
Then when you see this sentence, pass the address value of the fifth byte to s3
Therefore, although you just wrote a "" by hand, VB ended up doing a lot more work. Although 6 bytes is not much, in a large project where everyone writes "" everywhere, it is also a considerable expense. So next time you must not use "" , but be sure to use vbNullString
"You said that, sea snake", I looked at it contemptuously.
Seeing everyone's sudden realization, the sea snake laughed again (can sea snakes laugh?)
"Understanding me does not mean truly controlling me. Many C divers will disdain VB. Do you know why?"
"Slow, VB's string operations are too slow." Many divers rushed to answer this question.
"Haha, yes," the sea snake laughed loudly and slowly swam toward the deep sea " IwillBack! "
We are left with a group of divers, and everyone is thinking, do we really understand the sea snake, because the sea snake in VB Ocean Legend is quite scary, will it walk away so easily, and what does it mean by IwillBack? What?
But we will continue to dive….
"Look, the coral reef" someone exclaimed! ->