Link to home
Start Free TrialLog in
Avatar of The_Brain
The_Brain

asked on

Simple question on converting.

How do you convert any COleVariant to a CString.
I am looking for Date/Time (both) in particular.  
Any suggestions?

like 1/5/1998 becomes "1/5/1998" and 5:30 "5:30" and can be converted back into COleVariant... ?  please I need this quite soon. If you feel you need more points then say so.  But I am pretty shallow on points.

Thanks...

Again this is not an assignment, i know the rules.
Avatar of The_Brain
The_Brain

ASKER

Edited text of question
Avatar of jkr
Use 'COleVariant::ChangeType()' to convert it to a VT_BSTR. Then, use 'COleVariant::Detach()' to detach the VARIANT from the MFC object. The BSTR is available as the 'bstrVal' member of the VARIANT, all you have to do from then is converting is to an ANSI string which can be assigned to any CString object.
I don't understand your answer perfectly well, I have found what I was looking for.

just in case you want to have the points could you elab your answer?

do this please.

Dialog1* dlg1;
dlg1=new Dialog1;

{
   COlevariant DateBuffer;
   COlevariant TimeBuffer;
   ... do what you have to do to make the following happen.
   dlg1.m_Edit1=DateBuffer; // DateBuffer(must be a CString temporarily just so that this line will work as a CString)
   dlg1.m_Edit2=TimeBuffer;

}
   
this will be great.  thanks so much  I will add 5 more points on for the hassle.  (but your answer isn't the clearest either :-)



OK, you're right ;-)

CString StringFromVariant ( COleVariant* poleVariant)
{
COleVariant oleVariant = *poleVariant;
CString strRet;
HRESULT hResult:

oleVariant.ChangeType( VT_BSTR);

VARAIANT var = oleVariant.Detach();
BSTR* lpolestr = var.bstrVal;
char acBuf[ 255];

    hResult =   ::WideCharToMultiByte   (   CP_ACP,
                                            0,
                                            lpolestr ,
                                            -1,
                                            acBuf,
                                            sizeof  (   acBuf),
                                            NULL,
                                            NULL
                                        );

 if ( !FAILED( hResult)) strRet = acBuf;

return ( strRet);
}

{
   COlevariant DateBuffer;
   COlevariant TimeBuffer;
   ... do what you have to do to make the following happen.
   dlg1.m_Edit1=StringFromVariant ( &DateBuffer); // DateBuffer(must be a CString temporarily just so that this line will work as a CString)
   dlg1.m_Edit2=StringFromVariant ( &TimeBuffer);

}

I hope i made it clear enough ;-)

Oops, sorry:
BSTR* lpolestr = var.bstrVal;
should of course read
BSTR* lpolestr = var.pbstrVal;

and
oleVariant.ChangeType( VT_BSTR);
should be
oleVariant.ChangeType( VT_BYREF|VT_BSTR);

(%&&%$! typos ;-)
I will look into that... (wow but it is pretty complicated for such a little function (is this procedure needed in my project or are you just showing me what the function does?)

this is even worse than the first answer, (i liked the first answer better, it unfortunately didn't make it any clearer. I am very tired.  So I will look again tommorow.  if you really don't mind could you show me What Exactly to (whatI have written down)
do, in my code only.  What do I include, what do I really say.  (I am not sure if the above is what the function does or what I have to do.)  thanks a trillion.

I will increase it to 45, for the trouble. :-)
I will look into that... (wow but it is pretty complicated for such a little function (is this procedure needed in my project or are you just showing me what the function does?)

this is even worse than the first answer, (i liked the first answer better, it unfortunately didn't make it any clearer. I am very tired.  So I will look again tommorow.  if you really don't mind could you show me What Exactly to (whatI have written down)
do, in my code only.  What do I include, what do I really say.  (I am not sure if the above is what the function does or what I have to do.)  thanks a trillion.

I will increase it to 45, for the trouble. :-)
>>wow but it is pretty complicated for such a little function

Well, it's an all purpose function to convert ANY COleVariant into a CString (if applicable ;-) - and it is nothing but the 'coded' version of my first answer....
BTW: I's almost 10pm here, so i'm heading home also ;-)
It isn't really working.  I fix the errors,

    hResult =   ::WideCharToMultiByte   (   CP_ACP,
                                            0,
                                          --->*lpolestr ,
                                            -1,
                                            acBuf,
                                            sizeof  (   acBuf),
                                            NULL,
                                            NULL
                                        );

But anyway, the thing doesn't work,
This is what is happening, don't worry the TABLE opens fine.
I just might not be capturing correctly.

Time is the column of 0:00, 0:30, 1:00 etc.

while(!T_TimeChoice.IsEOF())
   {
     CString INTimeChoiceBuffer;
     COleVariant varName2; // =time_t(0); //just testing.
     //MessageBox(varName2);//just a test
     varName2=T_TimeChoice.GetFieldValue("Time");
     INTimeChoiceBuffer = StringFromVariant(&varName2); //Your function gives TYPE MISSMATCH. (when this goes over StV...
     MessageBox(INTimeChoiceBuffer);
     Cindy->TimeChoicesBuffer[pp]=INTimeChoiceBuffer;
//     INTimeChoiceBuffer = V_BR8(&varName2);
     pp++;
     T_TimeChoice.MoveNext();
}
Can you get it to work?  
I don't really think that a you need the whole source code just to get this working, you can make a mini emulator.  
I will stick it to 50.  thanks
I am putting it to 45 so long, and when i get the Working answer i will credit you with it.
I am going reject this answer in hope for other expert's opinion, I would like it if you can give me a working answer, the deal between us is still 50 for your trouble.  :)
thanks for understanding.  I REALLY need this to work.
(even if you write a function to do this,

What I really just want is.  
To take a COleVariant  <<thats the input;
to output CString <<thats the output.  that is all.
thanks.
Oh yes and
To take in a CString and to parse it to the COleVariant. thanks.
Hi,

  I'm not exactly sure of the context of the question and maybe this is a bit simplistic but, this is my what I came up with anyhow.

COleVariant variant;
COleDateTime current;
COleDateTime temp;
CString string;

current.SetDate(1999, 12, 30);   // set the date to whatever
current.SetTime(8, 30, 0);       // set the time to whatever

variant = current;               // set the variant value

temp = variant.date;             // this is where you get the
                                 // date from the COleVariant

string = temp.Format("%x");      // use the COleDateTime format
                                 // with the %x for the date

string = temp.Format("%X");      // or for time %X

string.Format("%d/%d/%d", temp.GetMonth(), temp.GetDay(), temp.GetYear());
                                 // you can also use the CString
                                 // printf style format along
                                 // with GetXXX();

This can be put into a function example at bottom of page.  One of the nice things about using COleDateTime as an intermediate structure is the function:

ParseDateTime(string, LOCALE_NOUSEROVERRIDE, LANG_USER_DEFAULT);

This converts the string into a valid date and time.  Then you can set the COleVariant equal to the COleDateTime and you'll have your date.  

Your functions may look like this...

CString VariantToString(COleVariant* pVariant, BOOL isDate)
{
COleDateTime dateTime;
CString string;
dateTime = pVariant.date;
if(isDate)
   string = dateTime.Format("%x");      // get date string
else
   string = dateTime.Format("%X");      // get time string

return string;
}

COleVariant StringToVariant(CString string)
{
COleVariant variant;
variant.ParseDateTime(string);
return variant;
}

If you need elaboration send me some mail, good luck.

This looks really great...
thanks I will look at it tommorow in great detail.  I will try my best to work it.  If I can't I will send you my program.
It does work, I haven't got a chance to see if the StringToColeVariant works, could you just give me your ICQ uin or E-mail so that I may ask anyquestions later?

Here are the 50 points as promised.  thanks man.  this is great stuff.  (any more help can be send to xy16538@oldmutual.exchange.co.za)
any question just ask me. I can also help.

Thanks Again.


Please offer an answer so that I may accept it...


Please SEND AN ANSWER!!!  I wan't to accept an answer, if you don't answer this question by 12 Jan 1999 I will ask for deletion.  Thanks
oops want not wan't.
ASKER CERTIFIED SOLUTION
Avatar of perrizo
perrizo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Nathan...

That was a brilliant answer, I recommend it to everyone :^)