Solved

Simple question on converting.

Posted on 1998-12-29
20
666 Views
Last Modified: 2013-11-20
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
Comment
Question by:The_Brain
  • 13
  • 5
  • 2
20 Comments
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Edited text of question
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
BTW: I's almost 10pm here, so i'm heading home also ;-)
0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:The_Brain
Comment Utility
I am putting it to 45 so long, and when i get the Working answer i will credit you with it.
0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Oh yes and
To take in a CString and to parse it to the COleVariant. thanks.
0
 
LVL 1

Expert Comment

by:perrizo
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
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
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
oops want not wan't.
0
 
LVL 1

Accepted Solution

by:
perrizo earned 50 total points
Comment Utility
Hi,

  hadn't checked back thanks for the mail.
0
 
LVL 1

Author Comment

by:The_Brain
Comment Utility
Thanks Nathan...

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
isEverywhere  challenge 19 64
matchUp  challenge 9 71
noX challenge 17 75
Change to event 1 72
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

10 Experts available now in Live!

Get 1:1 Help Now