[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2500
  • Last Modified:

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
0
codechewer
Asked:
codechewer
5 Solutions
 
jkrCommented:
ATLBASE.H does

#include <windows.h>

So, try to change the include order - include the ATL headers *after* the MFC headers.
0
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now