-
October 11th, 2010, 08:10 AM
#1
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.
-
October 11th, 2010, 03:15 PM
#2
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();
Last edited by cilu; October 13th, 2010 at 05:28 AM.
-
October 11th, 2010, 04:14 PM
#3
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!
Originally Posted by cilu
and in C++ you should new it:
Code:
TProjLicense License = new TProjLicense();
Do you mean C# here? IMO in C++/CLI it should be TProjLicense ^ and gcnew, and in native C++ TProjLicense *.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
October 13th, 2010, 05:29 AM
#4
Re: Calling C++/CLI method from C#
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.
-
October 15th, 2010, 02:21 AM
#5
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 !
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|