Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

using CString in ATL COM

Posted on 2003-10-23
10
Medium Priority
?
2,493 Views
Last Modified: 2013-12-14
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
Comment
Question by:codechewer
[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
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 9607776
ATLBASE.H does

#include <windows.h>

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

Assisted Solution

by:Dexstar
Dexstar earned 200 total points
ID: 9608135
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
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 200 total points
ID: 9608829
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Assisted Solution

by:ambience
ambience earned 200 total points
ID: 9612047
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
 

Author Comment

by:codechewer
ID: 9612517

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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9615273
codechewer,

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

Hope that helps,
Dex*
0
 
LVL 5

Assisted Solution

by:Daij-Djan
Daij-Djan earned 200 total points
ID: 9615830
use vc7's atl string header:
#include <atlstr.h>
0
 

Author Comment

by:codechewer
ID: 9643547
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
 
LVL 9

Expert Comment

by:tinchos
ID: 10546072
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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