Solved

Converting a CString into an Array

Posted on 2006-11-20
13
588 Views
Last Modified: 2013-11-20
Hi guys,

I have this line of code:

CString cszArg2= pDispParams->rgvarg[0].bstrVal;


But instead of storing a CString, I am looking to store an array. Any ideas on what I need to do to get this working? This is partially related to another question I have open, but is not the same question.

Cheers!
0
Comment
Question by:Cyber-Drugs
  • 3
  • 2
  • 2
  • +4
13 Comments
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 17984274
std::auto_ptr<char> cszArg2(new char[SysStringByteLen(pDispParams->rgvarg[0].bstrVal)]);
memcpy(cszArg2.get(), pDispParams->rgvarg[0].bstrVal, sizeof(cszArg2.get()));
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17984324
Hi,

Unfortunately I got an error with that one:

C:\Documents and Settings\Administrator\Desktop\Browser\CustomBrowser\CustomBrowser\Idispimp.cpp(383) : error C2039: 'auto_ptr' : is not a member of 'std'


If it at all helps, full details of the function this belongs to can be found here:

http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_22067364.html

Cheers!
0
 
LVL 86

Expert Comment

by:jkr
ID: 17984520
See http://www.codeproject.com/string/ctokenex.asp ("String Tokenizer Class (CTokenEx)"), this one will help you to break a string down into an array. But, since you deal with filenames, that might be problematic depending on the which delimiter tokens you use.
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 17985393
CString strText = "Puranik";
char * chText = strText.GetBuffer(1);
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 300 total points
ID: 17985418
You may use OLE2CA

#include <atlconv.h> // or <afxpriv.h>

USES_CONVERSION; // Necessary when you use these OLE macros

const char *pString1 = OLE2CA(YourBSTR); // Macro converts OLECHAR string to "const char *" string


So you may directly try :
      const char *pString = OLE2CA(pDispParams->rgvarg[0].bstrVal);

-MAHESH
0
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 17985499
std::auto_ptr requires
#include <memory>
but you could use a regular pointer instead as long as you call delete.

But certainly as Mahesh suggests, there is no need to copy the string necessarily.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17985506
Is CStringArray any use?
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 200 total points
ID: 17995310
To convert a CString to a CStringArray you may use that:

int StringToArray(const CString& str, CStringArray& arr, const CString& seps = " ")
{
    int last = 0;

    for (int i = 0; i < str.GetLength(); ++i)
    {
        TCHAR c = str[i];
        if (seps.Find(c) >= 0)
        {
            arr.Add(str.Mid(last, i-last));
            last = i+1;
        }
    }
    return arr.GetSize();
}

It can be used like:

    CString str = "abc;def;xyz;;mkh;";
    CStringArray arr;
    int n = StringToArray(str, arr, ";");

    for (int ii=0; ii < arr.GetSize(); ++ii)
        AfxMessageBox(arr.GetAt(ii));

Note, if your string contains more than one valid separator, e. g. space, comma, and semicolon, you may pass it to the convert function like " ,;".

Regards, Alex


0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17995491
Hi guys,

I ended up making a function to solve this, here is what I got:

CString* CCustomBrowserDlg::CB_GetArray(VARIANT vMyValue, int nArg2)
{
      CString* aEndArray = new CString[nArg2];
      int pos = 1;
      CComPtr<IDispatchEx> pDispEx;
      HRESULT hr=vMyValue.pdispVal->QueryInterface(IID_IDispatchEx,(void**)&pDispEx);
      DISPID dispid;
      // get the DISPID of the first item.  
      hr = pDispEx->GetNextDispID(fdexEnumAll, DISPID_STARTENUM, &dispid);
      while (hr == NOERROR)
      {
            // get the item name
            CComBSTR bstrName;
            hr = pDispEx->GetMemberName(dispid, &bstrName);

            // get the item value
            CComVariant vValue;
            DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
            hr = pDispEx->InvokeEx(dispid, LOCALE_USER_DEFAULT,
                  DISPATCH_PROPERTYGET, &dispparamsNoArgs,
                  &vValue, NULL, NULL);

            if (pos <= nArg2)
            {
                  switch (vValue.vt)
                  {
                  case VT_I4:
                        // value is integer
                        char buf[5];
                        itoa(vValue.iVal, buf, 10);
                        aEndArray[pos-1] = buf;
                        break;
                  case VT_BSTR:
                        // value is string
                        aEndArray[pos-1] = vValue.bstrVal;
                        break;
                  }
            }

            pos++;
            //get the DISPID of the next item.  
            hr = pDispEx->GetNextDispID(fdexEnumAll, dispid, &dispid);
      }

      return aEndArray;
}
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 200 total points
ID: 17995888
>>>> CString cszArg2= pDispParams->rgvarg[0].bstrVal;

I am missing a statement like the one you initially asked for.

Did you change your requirement? Did the last function you posted solve your problem?

>>>> CString* CCustomBrowserDlg::CB_GetArray(VARIANT vMyValue, int nArg2)

Instead of returning a CString* I would suggest to pass a reference to a CStringArray.

  bool CCustomBrowserDlg::CB_GetArray(VARIANT vMyValue, int nArg2, CStringArray& aEndArray)

Then, you don't need to allocate an array yourself but simply call aEndArray.Add(..) to add a new CString to the array, e. g.

    aEndArray.Add(CString( vValue.bstrVal ));


Regards, Alex
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17995974
Have you tried that with OLE2CA as shown above ?
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17996074
itsmeandnobodyelse,

>>>> CString cszArg2= pDispParams->rgvarg[0].bstrVal;

has not disappeared, I merely parse that value into the function above, and it outputs an array for me.



mahesh1402,

I will be trying the other methods once I finish off a few bits of work in the office, and then will get back to you on the results, but wanted to let you all know that it's no longer urgent, as I have a working solution. :)


Cheers!
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 20337676
You can use the same thing.
CString cszArg2= pDispParams->rgvarg[0].bstrVal;

After this
cszArg2.GetBuffer(1) will return an array.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 48
Installing Microsoft R server on REDHAT or SUSE Linux 4 131
canBalance challenge 34 69
fix34  challenge 9 104
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now