A C# class library in a C++ project

V

Victor

Hi everybody !

I have a beautiful class library (DLL) designed under C#. Now I need to
use a class from that library in a VC++ project (VS 2005).

However, I desperately fail to find a possibility to make my VC++ code
see the declaration of the class. There are no header files under C#,
no LIB to link to the current project - solely DLL. I can load this DLL
in my project, can unload it, but I do not know how to declare a class
instance...

'using namespace' does not help - compiler states "..no such
namespace"...

Any directing hint would be highly appreciated.

Victor
 
J

Jochen Kalmbach [MVP]

V

Victor

Jochen,

I must apologize, but I do not find how the stuff from the links you
provided pertains my problem... You know, I am quite new to .NET, so
would you give me some more comments, if you please...

I have a Class Library completely developed in C#, containig a complex
definition of a class referring many other sources. On compiling this
project I get only a DLL, respectively debug or release version.

Further, I have a rather advanced project in VC++, using MFC and doing
already many different things. In this project I need a class, defined
in the mentioned C# DLL. The question of mine is : HOW to teach my VC++
about existence of the class definition in the DLL created in C# ?

I can brilliantly use this class in another C# project. I can use other
class definitions specified in DLL's that have been developed in C++,
because I have their header files.

And all the links you pointed me to - as far as I understand - explain
how to do things WITHIN ONE LANGUAGE. I do not seem to have problems
like this...

Maybe, I simply do not understand anything a proper way ?

Will you help me any furhter, please ?

Victor
 
J

Jochen Kalmbach [MVP]

Hi Victor!
On compiling this
project I get only a DLL, respectively debug or release version.
Yes.

HOW to teach my VC++
about existence of the class definition in the DLL created in C# ?

1st: You need to enabled "C++/CLI" via the "/clr" compiler switch
(Project|Settings|General|Common Language Runtime Support: /clr)
And all the links you pointed me to - as far as I understand - explain
how to do things WITHIN ONE LANGUAGE. I do not seem to have problems
like this...

In .NET you can use the DLLs (assemblies) from any .NET enabled language
(VB, C++/CLI, C#, ...)


If you have enabled the "/clr" switch, then you can add a reference to
your DLL!
(Project|Common Properties|References).

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
V

Victor

THANK YOU JOCHEN !!!

Now I have my class !!

Though there appeared some other problems now. But they are of
different nature. Will try to figure them out first myself...

Victor
 
V

Victor

Unfortunately, I am failing to tackle this different problem alone...

So I have to appeal about help again.

Now the problem is like this:

In the C# DLL there is a class defined :

class CSM : Stream, IDisposable {
private string wFile;
public CSM () : base() {}
public CSM (string file) : this() { Filename = file; }
public CSM (CSM src) {}
public string Filename {
get { return wFile; }
set { wFile = value; }
}
}

Then I try to declare an instance of the class in the C++ project. As
long as I am doing it this way :
CSM smFile;
- no problems arise.
But I need to declare it using another ctor :
CSM smFile("Some value");

The compiler laments : C3673 - 'CSM' : class does not have a
copy-constructor

So what is about the line
public CSM (CSM src) {} ???

Furthermore, when the CSM has been declared without parameters, I get
the following compiler error on trying to access this instance :
wcscpy(fileW,smFile.Filename); // fileW is a buffer declared
earlier

Error C2228: left of '.Filename' must have class/struct/union

What is/can be wrong here?

Any assistance would be highly appreciated.

Victor
 
J

Jochen Kalmbach [MVP]

Hi Victor!
Then I try to declare an instance of the class in the C++ project. As
long as I am doing it this way :
CSM smFile;
- no problems arise.
But I need to declare it using another ctor :
CSM smFile("Some value");

CSM ^smFile;
smFile = gcnew CSM("Some value");

Furthermore, when the CSM has been declared without parameters, I get
the following compiler error on trying to access this instance :
wcscpy(fileW,smFile.Filename); // fileW is a buffer declared

I think there is no default conversion from System::String to LPCTSTR...
You need to use PtrToStringChars

See: How to convert from System::String* to Char* in Visual C++ 2005 or
in Visual C++ .NET
http://support.microsoft.com/kb/311259/


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
V

Victor

Thanks Jochen,

you know, this far I have riched myself as well :

CSM^ smFile;
smFile = gcnew CSM("Some value");

- with the same resulting compiler error

C3673 - 'CSM' : class does not have a
copy-constructor

..................

As for conversion from System::String, you may have right, but I
believe that the problem must appear somewhere still before conversion;
the compiler does not find this element at all - if I understand all
this stuff correctly :
Error C2228: left of '.Filename' must have class/struct/union

It means that the Filename could not be accessed at all - or not ?

Victor
 
V

Victor

BTW : What is the FieldGetter ?

In the MSDN I do not find any reference to this term---

Victor
 
N

Nishant Sivakumar

I am sure that it's not that line which is causing the compiler error.
Copy/paste the entire compiler error and the entire code block where the
error occurs.
 
V

Victor

The whole trouble snippet cosists of two lines:

(1) using namespace CSMSharp;

and then the line (2) in two variants:

This variant causes the below error :

CSM^ smFile = gcnew CSM(fileWav);

Error 6 error C3673: 'CSMSharp::CSM' : class does not have a
copy-constructor


If declared this way :

CSM^ smFile = gcnew CSM();

- no compiler error occurs...


Victor
 
N

Nishant Sivakumar

I tested out your code.

I creates a C# DLL project :-

******************************
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CSMSharp

{
public class CSM
{
private string wFile;
public CSM() : base() { }
public CSM(string file) : this() { Filename = file; }
public CSM(CSM src) { }
public string Filename
{
get { return wFile; }
set { wFile = value; }
}
}
}
******************************

And a Cpp caller :-

******************************
using namespace System;
using namespace CSMSharp;

int main(array<System::String ^> ^args)
{
CSM^ smFile = gcnew CSM("fileWav");
return 0;
}
******************************

Code compiles - no problems at all. I assumed fileWav is a string.
 
V

Victor

Nishant,

I tried to follow your approach : I had the same success - everything
compiles O.K.

But my working project is not this simple. And I do get the described
error C3673...
Regrettably, I do not have a lot of experience under .NET - so I have
only inflicted some bad headache on myself without any tangible chance
to find a way out...

What could cause this behaviour in my project ?

Additional info : I develop a MFC project that must support
multithreading.

Regards
Victor
 
N

Nishant Sivakumar

Try and create a minimal project that reproduces the error. Then copy/paste
the code here and someone can figure out what you are doing wrong.
 
V

Victor

Well, it did not take too long to reproduce the error in an almost
virgin project.

I created a new C++ MDI project using MFC in a shared DLL - I did not
change anything in the wizard settings for a new project. The only
thing : in the Project Properties/General/CLR Support I activated the
switch /CLR. Surely, I also introduced a reference to my C# DLL.
Then I added a new menu entree and a handler for it, resulting in a
following line in the app header ResTesA.h :

public:
afx_msg void OnViewZoomIn();

In the ResTesA.cpp there appeared the function body :


void CResTesAApp::OnViewZoomIn()
{
using namespace SMWaveFileSharp;
CSMWaveFile^ smWaveFile = gcnew CSMWaveFile("fileWav");
}

-- this was enough for reproducing the error

Error C3673: 'SMWaveFileSharp::CSMWaveFile' : class does not have a
copy-constructor

Any comments are welcome

Victor
 
N

Nishant Sivakumar

Hey Victor,

You also need to copy/paste the CSMWaveFile code listing. Otherwise, people
cannot reproduce the error.
 
V

Victor

Sorry guys,

I am so much ashame -- but probably we all make sometimes stupid
mistakes...

It was completely my personal fault and nothing more -- I referred in
the VC++ project an old version of the DLL with the same name....

Sorry for this bothering and thank you very much

Nishant and Jochen

for your engagemental support !!!

Happy coding ;-()

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top