• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 708
  • Last Modified:

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.
0
The_Brain
Asked:
The_Brain
  • 13
  • 5
  • 2
1 Solution
 
The_BrainAuthor Commented:
Edited text of question
0
 
jkrCommented:
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.
0
 
The_BrainAuthor Commented:
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 :-)



0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkrCommented:
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 ;-)

0
 
jkrCommented:
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 ;-)
0
 
The_BrainAuthor Commented:
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. :-)
0
 
The_BrainAuthor Commented:
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. :-)
0
 
jkrCommented:
>>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....
0
 
jkrCommented:
BTW: I's almost 10pm here, so i'm heading home also ;-)
0
 
The_BrainAuthor Commented:
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
0
 
The_BrainAuthor Commented:
I am putting it to 45 so long, and when i get the Working answer i will credit you with it.
0
 
The_BrainAuthor Commented:
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.
0
 
The_BrainAuthor Commented:
Oh yes and
To take in a CString and to parse it to the COleVariant. thanks.
0
 
perrizoCommented:
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.

0
 
The_BrainAuthor Commented:
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.
0
 
The_BrainAuthor Commented:
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...


0
 
The_BrainAuthor Commented:
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
0
 
The_BrainAuthor Commented:
oops want not wan't.
0
 
perrizoCommented:
Hi,

  hadn't checked back thanks for the mail.
0
 
The_BrainAuthor Commented:
Thanks Nathan...

That was a brilliant answer, I recommend it to everyone :^)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 13
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now