Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple question on converting.

Posted on 1998-12-29
20
Medium Priority
?
689 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 5
  • 2
20 Comments
 
LVL 1

Author Comment

by:The_Brain
ID: 1326893
Edited text of question
0
 
LVL 86

Expert Comment

by:jkr
ID: 1326894
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
ID: 1326895
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 86

Expert Comment

by:jkr
ID: 1326896
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
ID: 1326897
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
ID: 1326898
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
ID: 1326899
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
ID: 1326900
>>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
ID: 1326901
BTW: I's almost 10pm here, so i'm heading home also ;-)
0
 
LVL 1

Author Comment

by:The_Brain
ID: 1326902
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
 
LVL 1

Author Comment

by:The_Brain
ID: 1326903
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
ID: 1326904
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
ID: 1326905
Oh yes and
To take in a CString and to parse it to the COleVariant. thanks.
0
 
LVL 1

Expert Comment

by:perrizo
ID: 1326906
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
ID: 1326907
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
ID: 1326908
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
ID: 1326909
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
ID: 1326910
oops want not wan't.
0
 
LVL 1

Accepted Solution

by:
perrizo earned 200 total points
ID: 1326911
Hi,

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

Author Comment

by:The_Brain
ID: 1326912
Thanks Nathan...

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

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

721 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