Lm th? no ? v?t qua m?t SafeArray dy trong m?t bi?n th? * gi?a Visual C++ v Visual Basic 6.0

D?ch tiu ? D?ch tiu ?
ID c?a bi: 167668 - Xem s?n ph?m m bi ny p d?ng vo.
Chu y Microsoft Visual C++.NET (2002) h? tr? c? hai m? ?c qu?n l? m h?nh ?c cung c?p b?i Microsoft.NET Framework v ngu?n g?c khng ?c qu?n l? Microsoft Windows m? m h?nh. Thng tin trong bi vi?t ny ch? p d?ng cho khng ?c qu?n l? Visual c ++ m?.
Bung t?t c? | Thu g?n t?t c?


Khi b?n nh?n vo cc thng tin c s?n trn i qua SafeArrays gi?a Visual C++ v Visual Basic 6.0, v?t qua h?u h?t SafeArray trong m?t tham s? c?a ki?u bi?n th?. Trong MFC, d?ch ny thnh m?t "bi?n th? const & varName"tham s?.

? thay th?, bi vi?t ny khm ph lm th? no ? v?t qua m?t phin b?n *, m trong MFC s? ?c cng b? nh l m?t "bi?n th? vi?n * varName" tham s?.

T?i sao b?n nn s? d?ng m?t phng php qua khc? B?i v? h?u h?t cc ?ng d?ng cho php s?a ?i cho m?t SafeArray thng qua t?i m?t phin b?n, nhng khng ph?i t?t c?. Trong Excel 95, b?n c th? v?t qua m?t SafeArray trong m?t phin b?n v?i m?t Visual C++ h? ph?c v?, s?a ?i cc gi tr? trong my ch?, tr? v? SafeArray s?a ?i, v khng c khng c s? thay ?i trong Excel 95. M?t l? do ? s? d?ng k? thu?t ny l ? lm vi?c xung quanh m?t gi?i h?n trong Visual Basic 6.0 khi b?n s? d?ng ?u rng bu?c.


? ch?ng minh khi ni?m ny, b?t ?u v?i m?t m?i "MFC AppWizard(exe) "d? n ?c g?i l StrArray. Trong "B?c 3 c?a 6," ch?n T? ?ng ha, b?m K?t thc, v sau nh?p vo Ok ? t?o ra cc t?p tin d? n.

LU ?: N?u s? d?ng Visual Studio.NET: t?o ra m?t m?i "MFC ?ng d?ng" d? n ?c g?i l StrArray. Trn cc Tnh nng nng cao tab, ch?n T? ?ng ha, v sau nh?p vo K?t thc ? t?o ra cc t?p tin d? n.

By gi? t?o m?t ?a n phng php t? ?ng ha b?ng cch s? d?ng ClassWizard. Trong ClassWizard, ch?n "cstrarraydoc" Theo "tn l?p:". B?m vo cc T? ?ng ha tab v Click vo Thm phng php. Nh?p cc gi tr? sau vo h?p tho?i thm phng php:

LU ?: N?u s? d?ng Visual Studio.NET: thm m?t s? d?ng phng php t? ?ng ha n "L?p View". M? r?ng cy l?p xem v t?m ki?m VCCOdeIDLLibrary l?i vo. T?m giao di?n IStrArray d?i nt . Nh?p chu?t ph?i vo giao di?n, v sau ch?n Thm phng php. Nh?p cc gi tr? sau vo h?p tho?i thm phng php:
   External Name : Sort
   Internal Name : Sort
   Return Type   : long
   Parameter List:

      Name       : vArray
      Type       : VARIANT*

Nh?p vo Ok v Ch?nh s?a m?.

? th?c hi?n phng php phn lo?i, s? d?ng phng php FastSort MFCARRAY m?u ? ? c?p trong ti li?u tham kh?o ph?n d?i y. i?u ny cho php b?n ? xem nh?ng khc bi?t gi?a i qua tham s? nh l m?t "XD Phin b?n & varName"v m?t"Phin b?n * varName". S?a ?i phng php s?p x?p nh sau:

   #include <afxpriv.h>
   long CStrArrayDoc::Sort(VARIANT FAR* vArray)

      long i, j, min;
      BSTR bstrTemp;
      SAFEARRAY FAR* psa = NULL;
      BSTR HUGEP *pbstr;
      HRESULT hr;
      DWORD dwTimeStart;
      LONG cElements, lLBound, lUBound;


      // Type check VARIANT parameter. It should contain a BSTR array
      // passed by reference. The array must be passed by reference it is
      // an in-out-parameter.
      if (V_VT(vArray) != (VT_ARRAY | VT_BSTR))
           "Type Mismatch in Parameter. Pass a string array by reference");
      psa = V_ARRAY(vArray);
      // Check dimensions of the array.
      if (SafeArrayGetDim(psa) != 1)
           "Type Mismatch in Parameter. Pass a one-dimensional array");

      dwTimeStart = GetTickCount();

      // Get array bounds.
      hr = SafeArrayGetLBound(psa, 1, &lLBound);
      if (FAILED(hr))
          goto error;
      hr = SafeArrayGetUBound(psa, 1, &lUBound);
      if (FAILED(hr))
          goto error;

      // Get a pointer to the elements of the array.
      hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pbstr);
      if (FAILED(hr))
         goto error;

      // Bubble sort.
      cElements = lUBound-lLBound+1;
      for (i = 0; i < cElements-1; i++)
         min = i;
         for (j = i+1; j < cElements; j++)
            // NULL is a valid value for a BSTR. This code treats a NULL
            // BSTR as less than other string values.
            if (pbstr[min] == NULL)
            else if (pbstr[j] == NULL
               || wcscmp(pbstr[j], pbstr[min]) < 0)
               min = j;

         //Swap array[min] and array[i].
         bstrTemp = pbstr[min];
         pbstr[min] = pbstr[i];
         pbstr[i] = bstrTemp;

      hr = SafeArrayUnaccessData(psa);
      if (FAILED(hr))
         goto error;

      return GetTickCount()-dwTimeStart;


        "Unexpected Failure in FastSort method");
      return 0;


By gi? b?n c th? xy d?ng v th? nghi?m cc my ch? t? ?ng ha. Sau khi b?n xy d?ng cc my ch?, ch?y n nh l m?t ?c l?p ? ng k? n. Ch?y Visual Basic 6.0 v t?o ra m?t d? n m?i Visual Basic 6.0. ?t m?t nt trong m?u v S?a ?i x? l? ? ph h?p v?i m? d?i y:

   Private Sub Command1_Click()
      Dim o As Object
      Dim v As Variant
      ReDim v(50) As String
      Dim SortTime As Long

      Set o = CreateObject("StrArray.Document")

      upperbound = 1
      lowerbound = 100
      For n = 0 To 50
          v(n) = "Entry " & Int((upperbound-lowerbound+1)*Rnd+lowerbound)
      Next n

      SortTime = o.Sort(v)
      MsgBox ("Done")
   End Sub

i?u quan tr?ng ? nh?n ?c c php ny ? lm vi?c trong Visual Basic 6.0 l l?n ?u tin Dim m?ng bi?n nh l m?t bi?n th?, v sau ReDim n nh v?y l n tr? thnh m?t bi?n th? ch?a m?t m?ng c?a chu?i. By gi? b?n ? s?n sng ? th? nghi?m cc h? ph?c v?. Thi?t l?p break i?m trong Visual Basic 6.0 m? tr?c v sau khi cu?c g?i ? s?p x?p. Ch?y ?ng d?ng Visual Basic 6.0, b?m Command1 v s? d?ng cc xem c?a s? ? ki?m tra cc gi tr? cho v.


? bi?t thm chi ti?t, nh?p vo s? bi vi?t sau ? xem cc bi vi?t trong c s? ki?n th?c Microsoft:
122287Gi?i h?n c?a Visual Basic 3,0 &amp; Disptest nh b? i?u khi?n t? ?ng ha
131046 Binary.exe chuy?n d? li?u nh? phn b?ng cch s? d?ng t? ?ng ha OLE
131086 S? d?ng cc m?ng an ton trong t? ?ng ha
140202 MFCArray b?ng cch s? d?ng an ton m?ng trong MFC t? ?ng ha
122289 i qua cc c?u trc trong OLE t? ?ng ha
154172 Lm th? no ? v?t qua d? li?u nh? phn gi?a m?t i?u khi?n ActiveX v BB
H? th?ng Microsoft JournalThng Su 1996, "Q&A OLE" b?i Don h?p.

(c) Microsoft Cng ty c? ph?n nm 1997, t?t c? cc quy?n. Nh?ng ng gp c?a Shawn W. Karr, T?p on Microsoft.

Thu?c tnh

ID c?a bi: 167668 - L?n xem xt sau cng: 19 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Foundation Class Library 4.2, khi ?c dng v?i:
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 2.2
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
T? kha:
kbautomation kbFAQ kbhowto kbmt KB167668 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:167668

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com