{"id":12,"date":"2009-01-08T22:34:32","date_gmt":"2009-01-08T22:34:32","guid":{"rendered":"http:\/\/www.sanchitkarve.com\/blog\/?p=12"},"modified":"2009-01-08T22:34:32","modified_gmt":"2009-01-08T22:34:32","slug":"microsoft-does-it-differently","status":"publish","type":"post","link":"https:\/\/www.sanchitkarve.com\/blog\/2009\/01\/08\/microsoft-does-it-differently\/","title":{"rendered":"Microsoft does it differently"},"content":{"rendered":"<p>A while back I was studying the Len() function of VB6 and I came across something interesting.<\/p>\n<p>Contrary to our belief that this function counts the number of characters in a string and returns it, it actually does something totally different.<\/p>\n<p>Here\u2019s how it works.<\/p>\n<p>When any string is stored in VB, it is automatically stored in this format (in unicode):<\/p>\n<p>Suppose the String is \u201cABC\u201d:<\/p>\n<p><span style=\"font-family: &quot;Courier New&quot;;\">06 00 00 00 41 00 42 00 43 00<\/span><\/p>\n<p>The 41 to 43 part is a typical unicode style of storing strings, but 2 unicode characters before that, the number of bytes occupied by the string (including zeros) is stored (which is similar to Pascal style strings).<br \/>\nHence 06 stands for 6 bytes occupied by \u201cABC\u201d (since it\u2019s stored as A,0,B,0,C,0)<\/p>\n<p><strong>So all that the Len() does is read 4 bytes before the beginning of the string and return that value itself, instead of calculating the length of the string.<br \/>\n<\/strong><\/p>\n<p>So actually, the Len Function does nothing except read the length from the string format and return it.<br \/>\nWant to see how they do it?<br \/>\nHere\u2019s the Disassembled Listing of the Len() Function MSVBVM60.DLL DLL File.<\/p>\n<pre lang=\"asm\">__vbaLenBstr proc near\n   string = dword ptr 4\n\n   mov eax, [esp+string] ; eax points to string\n   test eax, eax ; ZF,SF,PF = EAX and EAX\n   jz short break ; If String is Null then break from loop\n   mov eax, [eax-4] ; Gets Unicode Length stored before string.\n\n   ; Here\u2019s how Text from textbox is stored internally:\n   ; If text is born2c0de then in memory:\n   ; (0\u00d712 0\u00d700) 0\u00d700 0\u00d700 (0\u00d762 0\u00d700 \u2026)\n   ; ie. length of string in bytes(unicode) (here 18 bytes)\n   ; followed by a Unicode 0 (0\u00d700 0\u00d700)\n   ; followed by the actual string in unicode\n   ; (\u2019b\u2019 00 \u2018o\u2019 00 \u2026 \u2018e\u2019 00)\n   ; So [eax-4] just gets the unicode length of\n   ; string which already is stored when a\n   ; string is taken as input from keyboard.\n\n   ; The Len() function doesn\u2019t even calculate length!!!\n   shr eax, 1 ; Divides Length by 2 to get Actual Length.\n   ; Uses eax so it can be used as a return value.\n\nbreak:\n   retn 4\n__vbaLenBstr endp<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A while back I was studying the Len() function of VB6 and I came across something interesting. Contrary to our belief that this function counts the number of characters in a string and returns it, it actually does something totally different. Here\u2019s how it works. When any string is stored in VB, it is automatically [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[58,61,70],"_links":{"self":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/12"}],"collection":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}