Click here to Skip to main content
15,867,883 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

C++
#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:

C++
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
Updated 9-Apr-13 4:16am
v3
Comments
Philippe Mori 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.

.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.
 
Share this answer
 
"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.
 
Share this answer
 
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>
 
Share this answer
 

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