Calling C++/CLI method from C#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Calling C++/CLI method from C#

  1. #1
    Join Date
    Oct 2010
    Posts
    2

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,582

    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 View Post
    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.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2010
    Posts
    2

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center