Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a huge C++ native class with a lot of CStrings in them. I am trying to write a managed class using C++/CLI in order to use it in my C# interface. I am getting a lot of errors like these:

Code:
const CString TAG_DISPLAY_USER_ANONYMOUS = TEXT("");


Error:
C++
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2146: syntax error : missing ';' before identifier 'TAG_DISPLAY_USER_ANONYMOUS'

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C3861: 'TEXT': identifier not found


This I checked online and is because C++/CLI doesnt support CStrings. What is the best way to go about it? Could someone please give me a proper example as well. I have read that I need another interface in between to manage the CStrings. Can anyone suggest me the best way out of this? Really stuck on this :(

Thanks.
Posted
Updated 27-Mar-14 4:33am
v3
Comments
Philippe Mori 13-May-14 18:38pm    
Typically you would create a wrapper class and then use gcroot to hold pointer to navive code from the managed one.

There is nothing wrong with using CString in a C++/CLI assembly.

Look in your project's DLL.h and/or StdAfx.h header files. You are probably missing a #include <afx.h> so that the compiler sees the definition for CString.

Once it compiles, you may run into problems linking. Make sure the project's Configuration Properties has the General setting "Use of MFC" set to "Use MFC in a Shared DLL" or "Use MFC in a Static Library".
 
Share this answer
 
v3
Comments
Mobile.Instinct 27-Mar-14 12:41pm    
I think you are right. I have tried your solution and am somehow able to make it work. Would be great if it worked through out. Shall post soon if it does. No compiler errors anymore :)
That message is produced as a result of function definitions of the form:
C++
extern foo(int i);

which have no explicit return type. Nothing to do with CString.

Note: it would really help if you actually showed us the line(s) of code that produces the error message.
 
Share this answer
 
Comments
Mobile.Instinct 27-Mar-14 10:33am    
Updated the question. Please have a look.
Richard MacCutchan 27-Mar-14 10:41am    
The compiler does not recognise CString or TEXT. You need to include the appropriate headers to define them. However, since this is C++/CLI, I don't think you can, since .NET and MFC do not mix.
Mobile.Instinct 27-Mar-14 10:43am    
So is there no way out from this? I tried this: http://www.multicoreconsulting.co.uk/blog/c-snippets/how-to-call-unmanaged-cplusplus-from-csharp/ but still no luck.
Richard MacCutchan 27-Mar-14 11:03am    
The way out would be to write a 'normal' C++ interface that can take the CString objects and convert them to ordinary C-type strings. You can then pass such strings into your C# code via COM or P/Invoke.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900