Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI
Hi all,
 
I´m developing a from scratch application that will have a client and a server. The client is being written with C# as a normal Windows application with forms and so forth. The server will receive TCP/IP socket messages for processing and respond.
 
I want the SERVER to be a C++ common code to be linked into Windows and Linux with less changes as possible, so that I can run the server on both Windows and Linux machines.
 
All the common classes and methods (used by CLIENT and SERVER) are being stored on a COMMONLIBRARY that can be linked to both CLIENT and SERVER.
 
I put it all into a Visual Studio 2010 solution:
 
CLIENT - The client C# code.
SERVER - The server C++ code.
I had also created a common library as a DLL project - COMMONLIBRARY
 
I created the library as a C++ CLASS LIBRARY that will generate a DLL. So the code look likes:
 
#pragma once
 
using namespace System;
 
namespace COMMONLIBRARY {
 
#include <string>
	
	public ref class clsLog
	{
	private:
		//
		// Properties
		//
		string	 m_strCurLogUser;
	public:
		// Methods
		int clsLog::Init (std::string FileName)
	};
}
 

#include <string>
class Foo 
{  
   string m_strMessage;
}
The string type is not being recognized. When I add std::string as:
 
   std::string m_strCurLogUser;
Then I´m getting C4368 error - mixed types are not supported.
 
If I turn off "Common Language Runtime" to use the standard C++ language (att Project->properties), the code doesn´t compile with several errors and the internal libraries.
 
Can someone gimme a hint of what´s going on ?
 
What´s the best option to define and use strings on that environment ?
 
How can I get the SERVER as much generic as possible and tying it to the Visual Studio environment ?
 
Any help appreciated.
 
Rds
 
Mendes
Posted 8-Apr-13 10:27am
Edited 9-Apr-13 5:16am
_duDE_8K
v3
Comments
Philippe Mori at 9-Apr-13 8:52am
   
Mixed-mode can be used to fix those errors but using .NET from C++/CLI is not really what you want if you want to do code that also run on Linux machines.
 
As other has point out, you have to make a choice between standard C++ or .NET (in which case you might want to use C# anyway). Mixed-mode should be avoided if not necessary as it make thing more complex by mixing both worlds particulary if you want portability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

.NET framework is not exactly portable. You have either to stick to standard C++ or use the Mono framework[^]. Note that, even if you choose standard C++, the networking layers of Windows and Linux are quite different.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

"C4368 error - mixed types are not supported"
 
You are trying to create a C++ CLI class with std C++ members, which is not possible, although you can access standard C++ classes form within member functions of a CLI class. If you turn off support for Common Language Runtime, you get lots of errors as you are trying to compile a CLI class.
 
It appears you are trying to write a .NET library with C++, but you talk about wanting the library to be portable. As has been mentioned, you may be able to do this with the Mono Framework, however, I am presuming that you don't want to be dependent upon .NET in your C++ library but want it to be a native library for the platform you install it on.
 
If you want a native common library, with networking functionailty in it, you will need to provide some sort of abstraction of the network layers, as they are different as CPallini has mentioned. There are plenty of good cross platform networking libraries about. Qt[^] has some and the ACE framework[^] also has a lot of good features.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As mentioned by others don't use C++/CLI or any .Net stuff if you want portability to Linux unless you're prepared to junk C++ for C#/Mono.
 
Apart from that I noticed this:
 
namespace COMMONLIBRARY {
 
#include <string>
...
</string>
 
That's a really bad idea as it tries to define everything in and included by the <string> header as being in the COMMONLIBRARY namespace. That might seem like a neat idea but believe me it will not work. Including <string> in a Windows environment brings in dozens of headers including Windows.h itself effectively redefining everything from the Win32 API inside the namespace.
I would try moving the #include outside the COMMONLIBRARY namespace.
 
#include <string>

namespace COMMONLIBRARY {
...
</string>
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100