Solved

using CString in ATL COM

Posted on 2003-10-23
10
2,454 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
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 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 50 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 50 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
 
LVL 22

Assisted Solution

by:ambience
ambience earned 50 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 50 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 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

21 Experts available now in Live!

Get 1:1 Help Now