Calling C++/CLI method from C#
Hi
I have ported some old BC++ code to Managed C++/CLI code and build it to a dll
Now I trie to call a method from this dll in my C# code. But this does not work. The compiler gives the following error:
'TProjLicense' does not contain a definition for 'CalcSignature' and no extension method 'CalcSignature' accepting a first argument of type 'TProjLicense' could be found (are you missing a using directive or an assembly reference?)
The code (C++/CLI) in my DLL is as follows:
public class TProjLicense
{
public:
// Constructor
TProjLicense() {};
String ^ TProjLicense::CalcSignature(String ^ CustName, array<String ^> ^Proj,
int NumProj);
};
And from C# I am calling it as follows:
// Make a list of all project names
String [] ProjectList = new String [10];
//ProjectList[1] = cmbProject1->Items->Strings[cmbProject1.SelectedIndex];
ProjectList[0] = cmbProject1.SelectedText;
ProjectList[1] = cmbProject2.SelectedText;
.....
// Generate the signature;
TProjLicense License;
edtSignature.Text = License.CalcSignature(edtCustomerName.Text,
ProjectList, 10);
Probably there is something with the string array, but I am new in CLI/C# so I don't have a clue how to solve this.
Has anyone an idea ?
Thanks already.
Re: Calling C++/CLI method from C#
In C++/CLI it should be a ref class (i.e. a managed class)
Code:
public ref class TProjLicense
and in C# you should new it:
Code:
TProjLicense License = new TProjLicense();
Re: Calling C++/CLI method from C#
Heroenimus, it looks like you didn't actually post the code that triggers the error. The error message seems to complain about a call to TProjLicense::CalcSignature() with a first argument of type TProjLicense, and in your C# code snippet I only see a call to that method with a first argument of type String ^ (at least if the Text property of a list control still is a string in C#, but I'm pretty sure it is without really knowing that language) which should work as far as I can tell.
Besides that, you only posted the declaration of the TProjLicense class here, not its definition. But it looks like this might be enough for the problem you describe here.
Also have a look at what cilu posted.
And please use code tags the next time you post code. It will make your code much more readable.
Ah, and... Welcome to CodeGuru! :)
Quote:
Originally Posted by
cilu
and in C++ you should new it:
Code:
TProjLicense License = new TProjLicense();
Do you mean C# here? :ehh: IMO in C++/CLI it should be TProjLicense ^ and gcnew, and in native C++ TProjLicense *.
Re: Calling C++/CLI method from C#
Quote:
Do you mean C# here? IMO in C++/CLI it should be TProjLicense ^ and gcnew, and in native C++ TProjLicense *.
Of course, thanks for pointing it out. The question was about C#, so the answer was about C# except that C++ is still carved in my brain.
Re: Calling C++/CLI method from C#
Thanks Cilu. This solved the problem (making a ref class of it).
And thanks Eri523 for the tips for posting messages !