using CString in ATL COM

hi,

i have implemented a COM object in ATL. the string values are BSTR data types. i have been able to implement CString in DCOM by including the  <afxwin.h> and <afxdisp.h> in the StdAfx.h file. i did the same in my COM server and i get an error : fatal error C1189: #error :  WINDOWS.H already included.  MFC apps must not #include <windows.h>

meanwhile, i have not included any windows.h file.

BSTR is more complex to handle than CString. isn't it possible to use CString in COM. why does it work in my DCOM and fail in my COM. be most grateful  to find a way out of this.

thanks
codechewerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
ATLBASE.H does

#include <windows.h>

So, try to change the include order - include the ATL headers *after* the MFC headers.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DexstarCommented:
BSTR and CStrings are very different things indeed.  For example, CStrings can be ANSI or UNICODE, depending on the compiler options, BSTRs are always UNICODE.  BSTRs are also length prefixed so they can contain embedded NULLs.

You can use CString inside of a COM object, but NEVER, NEVER use it in your COM interfaces.  In other words, use it internally to the object, but do not pass them between COM objects.  For passing strings between COM objects, always use BSTR.

When working with BSTRs, it is important to use the APIs for allocation / deallocation.  For this, I recommend using a wrapper class.  Try this one:
     http://www.codeguru.com/atl/CBstr.shtml

Let me know if that doesn't help you.
Dex*
0
DanRollinsCommented:
rather than using BSTR, you should consider using
       _bstr_t
since it does most of the work for you.  You can use CString internally and can easily convert from one to the other.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ambienceCommented:
It might be worthwile to compare the project settings and #include directives in your DCOM server project and COM project.

That is the easiest way to figure out what you are missing. Also as jkr mentioned try chaning #include order.
0
codechewerAuthor Commented:

thanks to you all for your comments. i have visited the site Dextar, and i see a real nice string manipulation by CBstr. i guess that will become useful to me in my work. yes i understand that CString must be used internally in the com. BSTR is what must be passed accross the interface. jkr and ambience... i have included the MFC headers after my ATL header. it now gives me link error:

fatal error LNK1104: cannot open file "nafxcwd.lib"

any help will be deeply appreciated.  thanks for the comments so far.
0
DexstarCommented:
codechewer,

Check out this previous question:
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20689230.html

Hope that helps,
Dex*
0
Daij-DjanCommented:
use vc7's atl string header:
#include <atlstr.h>
0
codechewerAuthor Commented:
well thanks to you all for your help and comments. they have increased my knowledge greatly. em.. i solved the problem by restarting the project and checking the add MFC support checkbox. this has enabled me use all the intrinsic MFC libraries in my atl project. the strange thing though is.. i see the same code added to my stdAfx.h file as i tried to do manually. yet this time it runs and compiles without errors. i guess there might be more going on in the background done just those two MFC header files appended to the stdAfx.h file. thanks to you all.  i guess i have to share the points equally for all who have contributed to this question.
0
tinchosCommented:
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: jkr {http:#9607776} & Dexstar {http:#9608135} & DanRollins {http:#9608829} & ambience {http:#9612047} & Daij-Djan {http:#9615830}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.