ÕæÊÇÏë²»µ½ÏµÁÐÎÄÕÂ(1-6) - VB6Ö¸Õë¼¼Êõ´ó½ÒÃØ ÏÂÔر¾ÎÄ

²»¹ý£¬µ±ÎÒÃǽ»»»lStrPtrsÀïµÄÁ½¸öLongÐÍÖ¸ÕëÔªËØʱ£¬»¹Òª¼ÇµÃͬʱ½»»»ËüÃǵÄË÷Òý£¬±ÈÈç½»»»µÚ0¸öºÍµÚ3¸öÔªËØ£¬ÈçÏ£º

lTemp1 = lStrPtrs(0, 3) : lTemp2 = lStrPtrs(1, 3)

lStrPtrs(0, 3) = lStrPtrs(0, 0) : lStrPtrs(1, 3) = lStrPtrs(1, 0) lStrPtrs(0, 0) = lTemp1 : lStrPtrs(1, 0) = lTemp2

µ±ÎÒÃÇÅźÃÐòºó£¬ÎÒÃÇ»¹Òª½«Õâ¸ölStrPtrsÀïµÄÖ¸ÕëÔªËØд»ØÈ¥£¬ÈçÏ£º For i = Lo To Hi

CopyMemory(ByVal VarPtr(MyArr(i)), lStrPtrs(0,i), 4) Next

ÎÒÒѾ­²»ÏëÔÙ°ÑÕâ¸ö·½·¨½²ÏÂÈ¥£¬ËäÈ»Ëü¿Ï¶¨¿ÉÐУ¬²¢ÇÒÒ²¿Ï¶¨±ÈÓÃCopyMemoryÀ´Òƶ¯Êý¾ÝÒª¿ì£¬ÒòΪÎÒÃÇʵ¼ÊÉÏÒƶ¯µÄ½ö½öÊÇLongÐ͵ÄÖ¸ÕëÔªËØ¡£µ«ÎÒÐÄÀïÒѾ­ÖªµÀÏÂÃæÓиüºÃ¸üÖ±½ÓµÄ·½·¨£¬ÕâÖÖתÍäĨ½ÇµÄÇúÏ߾ȹúʵÔÚ²»ÖµµÃÀË·ÑÎÄ×Ö¡£

ËÄ¡¢HACKÊý×éµÄBSTR½á¹¹£¬ÊµÊ±´¦ÀíÖ¸Õë¡£

×²ÉµÄÀ´ÁË£¬ÊµÊ±´¦ÀíÖ¸Õ붯̬½»»»Êý¾Ý£¬ºÃÒ»¸öÏìÁÁµÄ˵·¨¡£

ÎÒÃÇ¿´µ½£¬ÉÏÒ»½ÚÖÐËùÊö·½·¨µÄ²»×ãÔÚÓÚÎÒÃǵÄLongÐÍÖ¸ÕëÊý×éÀïµÄÖ¸ÕëÊǶÀÁ¢µÄ£¬ËüûÓкÍ×Ö´®Êý×éÀïµÄ×Ö´®Ö¸ÕëÁªÏµÔÚÒ»Æð£¬ÒªÊÇÄÜÁªÏµÔÚÒ»Æð£¬ÎÒÃǾÍÄÜÔÚ½»»»LongÐÍÖ¸ÕëµÄͬʱ£¬ÊµÊ±µØ½»»»×Ö´®ÔªËØ¡£ Õâ¿ÉÄÜÂð£¿

µ±È»£¬·ñÔòÎÒ»¨ÄÇô±ÊīȥдSafeArray¸Éʲô£¡

ÔÚÉÏÒ»½Ú£¬ÎÒÃǵÄÄ¿µÄÊÇÒª°Ñ×Ö´®Êý×éÀïµÄBSTRÖ¸ÕëÊý×éÄóöÀ´·Åµ½Ò»¸öLongÐÍÊý×éÀ¶øÔÚÕâÒ»½ÚÎÒÃǵÄÄ¿µÄÊÇÒªÈÃÎÒÃÇLongÐÍÖ¸ÕëÊý×é¾ÍÊÇ×Ö´®Êý×éÀïµÄBSTRÖ¸ÕëÊý×é¡£ÄóöÀ´ÔÙ·Å»ØÈ¥µÄ·½·¨£¬ÎÒÃÇÔÚÉÏÒ»½Ú¿´µ½ÁË£¬ÏÖÔÚÎÒÃÇÀ´¿´¿´£¬²»ÄóöÀ´¶øÖ±½ÓÓõķ½·¨¡£

Õâ¸ö·½·¨»¹ÊÇÒª´Ó×Ö´®Êý×éµÄSafeArray½á¹¹À´·ÖÎö£¬ÎÒÃÇÒѾ­ÖªµÀSafeArray½á¹¹ÀïµÄpvDataÖ¸ÏòµÄ¾ÍÊÇÒ»¸ö·Åʵ¼ÊÊý¾ÝµÄÕæÊý×飬¶øÒ»¸ö×Ö´®Êý×éÈçMyArrËüµÄpvDataÖ¸ÏòµÄÊÇÒ»¸ö°üº¬BSTRÖ¸ÕëµÄÕæÊý×é¡£ÏÖÔÚÈÃÎÒÃÇÏëÏ룬Èç¹ûÎÒÃǽ«Ò»¸öLongÐÍÊý×élStrPtrsµÄpvDataŪµÃºÍ×Ö´®Êý×éMyArrµÄpvDataÒ»Ñùʱ»áÔõÑù£¿BSTRÖ¸ÕëÊý×é¾Í¿ÉÒÔͨ¹ýLongÐÍÊý×éÀ´·ÃÎÊÁË£¬ÏÈ¿´ÈçºÎÓôúÂëÀ´ÊµÏÖÕâÒ»µã£º 'Ä£¿é¼¶±äÁ¿

Private MyArr() As String 'ÒªÅÅÐòµÄ×Ö´®Êý×é

Private lStrPtrs() As Long 'ÉÏÃæÊý×éµÄ×Ö´®Ö¸ÕëÊý×飬ºóÃæ»áƾ¿Õ¹¹ÔìËü Private pSA As Long '±£´ælStrPtrsÊý×éµÄSafeArray½á¹¹Ö¸Õë Private pvDataOld As Long '±£´ælStrPtrsÊý×éµÄSafeArray½á¹¹µÄÔ­ ' pvDataÖ¸Õ룬ÒÔ±ã»Ö¸´lStrPtrs

'¹¦ÄÜ£º ½«LongÐÍÊý×élStrPtrsµÄpvDataÉè³É×Ö´®Êý×éMyArrµÄpvData ' ÒÔʹLongÖ¸ÕëÊý×éµÄ±ä¸üÄÜʵʱ·´Ó¦µ½×Ö´®Êý×éÀï Private Sub SetupStrPtrs() Dim pvData As Long

' ³õʼ»¯lStrPtrs£¬²»ÐèÒª½«Êý×éÉèµÃºÍMyArrÒ»Ñù´ó

33

' ÎÒÃÇ»áÔÚºóÃæ¹¹ÔìËü ReDim lStrPtrs(0) As Long

'µÃµ½×Ö´®Êý×éµÄpvData pvData = VarPtr(MyArr(0))

'µÃµ½lStrPtrsÊý×éµÄSafeArray½á¹¹Ö¸Õë

CopyMemory pSA, ByVal VarPtrArray(lStrPtrs), 4

'Õâ¸öÖ¸ÕëÆ«ÒÆ12¸ö×Ö½Úºó¾ÍÊÇpvDataÖ¸Õ룬½«Õâ¸öÖ¸Õë±£´æµ½pvDataOld ' ÒÔ±ã×îºó»¹Ô­lStrPtrs£¬´Ë´¦Ò²¿ÉÒÔÓ㺠' pvDataOld = VarPtr(lStrPtrs(0)) CopyMemory pvDataOld, ByVal pSA + 12, 4

'½«MyArrµÄpvDataдµ½lStrPtrsµÄpvDataÀïÈ¥ CopyMemory ByVal pSA + 12, pvData, 4

'ÍêÕû¹¹ÔìSafeArray±ØÐëÒª¹¹ÔìËüµÄrgsabound(0).cElements

CopyMemory ByVal pSA + 16, UBound(MyArr) - LBound(MyArr) + 1, 4 '»¹ÓÐrgsabound(0).lLbound

CopyMemory ByVal pSA + 20, LBound(MyArr), 4 End Sub

¿´²»¶®£¬Çë½áºÏͼһÔÙ¿´¿´£¬Ó¦¸Ã¿ÉÒÔ¿´³öÎÒÃÇÊÇƾ¿Õ¹¹ÔìÁËÒ»¸ölStrPtrs£¬Ê¹Ëü¼¸ºõºÍMyArrһģһÑù£¬Î¨Ò»µÄ²»Í¬¾ÍÊÇËüÃǵÄÀàÐͲ»Í¬¡£MyArr×Ö´®Êý×éÀïµÄfFeatures°üº¬FADF_BSTR£¬¶ølStrPtrsµÄfFeatures°üº¬FADF_HAVEVARTYPE£¬²¢ÇÒËüµÄVARTYPEÊÇVT_I4¡£²»ÓùØÐÄÕâ¶ù£¬ÎÒÃÇÖ»ÒªÖªµÀlStrPtrsºÍMyArrËüÃÇÖ¸Ïòͬһ¸öÕæÊý×飬¹ÜËûÊÇBSTR»¹ÊÇVT_I4£¬ÎÒÃÇ°ÑÕæÊý×éÀïµÄÔªËص±³ÉÖ¸ÕëÀ´Ê¹¾ÍÐÐÁË¡£

×¢Ò⣬ÓÉÓÚlStrPtrsÊÇÎÒÃǾ­¹ýÁËÎÒÃǺܴóµÄ¸ÄÔ죬ËùÒÔµ±³ÌÐò½áÊøÇ°£¬ÎÒÃÇÓ¦¸Ã½«Ëü»¹Ô­£¬ÒÔ±ãÓÚVBÀ´ÊÍ·Å×ÊÔ´¡£Êǵģ¬²»ÊÍ·ÅÒ²²»Ò»¶¨»áÒýÆðÎÊÌ⣬ÒòΪ³ÌÐòÔËÐнáÊøºó£¬²Ù×÷ϵͳµÄÈ·ÊÇ»á»ØÊÕÎÒÃÇÔÚ¶ÑÕ»Àï·ÖÅäÁËȴûÓÐÊͷŵÄlStrPtrsÔ­À´µÄÒ°Ö¸ÕëpvOldData£¬µ«µ±ÄãÔÚIDEÖÐÔËÐÐʱ£¬ÄãÓÐ60£¥µÄ»ú»áÈÃVBµÄIDEËÀµô¡£ÎÒÃÇÊÇÏë°ïVB×öµã¼ÒÎñÊ£¬¶ø²»ÊÇÏë¸øVBÌíÂÒ×Ó£¬ËùÒÔÇë¼ÇסÔÚ×öÍê²Ëºó£¬Ò»¶¨Òª°Ñ³ø·¿´òɨ¸É¾»£¬¶«Î÷¸Ã»¹Ô­µÄÒ»¶¨Òª»¹Ô­¡£ÏÂÃæ¿´¿´ÔõôÑùÀ´»¹Ô­£º '»¹Ô­ÎÒÃÇ×ö¹ýÊֽŵÄlStrPtr Private Sub CleanUpStrPtrs()

'lStrPtrµÄÔ­À´ÉùÃ÷Ϊ£ºReDim lStrPtrs(0) As Long ' °´ÉùÃ÷µÄÒªÇó»¹Ô­Ëü

CopyMemory pSA, ByVal VarPtrArray(lStrPtrs), 4 CopyMemory ByVal pSA + 12, pvDataOld, 4 CopyMemory ByVal pSA + 16, 1, 4 CopyMemory ByVal pSA + 20, 0, 4 End Sub

ºÃÁË£¬¾«»ªÒѾ­½²ÍêÁË£¬Èç¹ûÄ㻹ÓеãÏ벻ͨ£¬¿´¿´ÏÂÃæµÄʵÑ飺

34

'ʵÑé

Sub Main()

'³õʼ»¯×Ö´®Êý×é Call InitArray(6)

'¸ÄÔìlStrPtrs Call SetupStrPtrs

'ÏÂÃæ˵Ã÷Á½¸öÖ¸ÕëÊÇÒ»ÑùµÄ Debug.Print lStrPtrs(3)

Debug.Print StrPtr(MyArr(3)) Debug.Print

'ÏÈ¿´¿´Ô­À´µÄ×Ö´® Debug.Print MyArr(0) Debug.Print MyArr(3) Debug.Print

'ÏÖÔÚÀ´½»»»µÚ0¸öºÍµÚ3¸ö×Ö´® Dim lTmp As Long lTmp = lStrPtrs(3) lStrPtrs(3) = lStrPtrs(0) lStrPtrs(0) = lTmp

'ÔÙÀ´¿´¿´ÎÒÃǵÄ×Ö´®£¬ÊDz»ÊǾõµÃºÜÉñÆæ Debug.Print MyArr(0) Debug.Print MyArr(3) Debug.Print

'»¹Ô­

Call CleanUpStrPtrs End Sub

ÔÚÎҵĻúÆ÷ÉÏ£¬ÔËÐнá¹ûÈçÏ£º 1887420 1887420

OPIIU

WCYKOTC

WCYKOTC OPIIU

ÔõôÑù£¿ÈçÔ¸Òѳ¥£¡×Ö´®Í¨¹ý½»»»LongÐÍÊý±»ÊµÊ±½»»»ÁË¡£ ͨ¹ýÕâÖÖ·½Ê½À´ÊµÏÖ×Ö´®Êý×éÅÅÐò¾Í·Ç³£¿ìÁË£¬ÆäЧÂÊÉϵÄÌá¸ßÊǾªÈ˵ģ¬¶ÔðÅÝÅÅÐòÕâÑù½»»»×Ö´®´ÎÊýºÜ¶àµÄÅÅÐò·½Ê½£¬Æäƽ¾ùÐÔÄÜÄÜÌá¸ßÒ»±¶ÒÔÉÏ£¨Òª¿´ÎÒÃÇ×Ö´®Æ½¾ù³¤¶È£¬£©£¬¶Ô¿ìËÙÅÅÐòÕâÑù½»»»´ÎÊý½ÏÉٵķ½·¨Ò²ÄÜÓв»ÉÙÐÔÄÜÉϵÄÌá¸ß£¬ÓÃÕâÖÖ¼¼ÊõʵÏֵĿìËÙÅÅ

35

Ðò£¬¿ÉÒÔ¿´¿´±¾ÎĵÄÅäÌ×´úÂëÖеÄQSortPointers¡£

±¾µÀ²Ë×î¹Ø¼üµÄ¼¼ÊõÒѾ­½²ÁË£¬ÖÁÓÚÔõô×öÍêÕâµÀ²Ë£¬Ôõô°ÑÕâµÀ²Ë×öµÃ¸üºÃ£¬»¹ÐèÒª´ó¼Ò×Ô¼ºÀ´Êµ¼ù¡£

ËÄ¡¢ÎÒÃÇѧµ½ÁËʲô¡£

½ö¾ÍSafeArrayÀ´Ëµ£¬Äã¿ÉÄÜÒѾ­·¢ÏÖÎÒ¸ù±¾¾ÍûÓÐÖ±½ÓÈ¥ÓÃÎÒ¶¨ÒåÁ˵ÄSAFEARRAY½á¹¹£¬ÎÒҲûÓÐÕ¹¿ª½²Ëü£¬Êµ¼ÊÉ϶ÔSafeArrayÎÒÃÇ»¹¿ÉÒÔ×öºÜ¶à¹¤×÷£¬»¹ÓкܶàÇÉÃîµÄÓ¦Ó᣻¹ÓÐÐèҪעÒâµÄ£¬VarPtrArray²»ÄÜÓÃÀ´·µ»Ø×Ö´®Êý×éºÍVariantÊý×éµÄSafeArray½á¹¹µÄÖ¸ÕëµÄÖ¸Õ룬Ϊʲô»áÕâÑùºÍÔõÑùÀ´½â¾öÕâ¸öÎÊÌ⣿ÕâЩ¶¼ÐèÒªÎÒÃÇÁ˽âBSTR£¬Á˽âVARIANT£¬Á˽âVARTYPE£¬ÕâЩҲֻÊÇCOMµÄ±ùɽһ½Ç£¬ÒªÑ§ºÃVB£¬ÄËÖÁÕû¸öÈí¼þ¿ª·¢¼¼Êõ£¬COM»¹ÓкܶàºÜ¶à¶«Î÷ÐèҪѧϰ£¬ÎÒÒ²»¹ÔÚѧ£¬ÔÚÎÒ¿ªÊ¼COMµÄרÌâ֮ǰ£¬´ó¼ÒÒ²Ó¦¸Ã×ÔѧһÏ¡£

COMµÄ¶«Î÷ÏÈ·ÅÒ»·Å£¬ÏÂһƪÎÄÕ£¬Ó¦ÅóÓѵÄÒªÇó£¬ÎÒ×¼±¸À´Ð´Ð´ÄÚ´æ¹²Ïí¡£

ºó¼Ç£º

ÓÖ»¨ÁËÕûÕûÒ»ÌìµÄʱ¼ä£¬Ï£ÍûдµÄ¶«Î÷ÓмÛÖµ£¬¾õµÃÓÐÓþÍÀ´½Ð¸öºÃ°É£¡

36