CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2013
    Posts
    3

    creating a dll in visual C++ class library to import in to visual C# console appl.

    the class library name is simple
    the header file is simple .h

    //simple.h
    #pragma once
    using namespace System;
    namespace simple
    {
    public class class1
    {
    public:
    int addition(int x, int y);

    };
    }

    the simple.cpp is

    #include "stdafx.h"

    #include "simple.h"
    int addition(int x, int y)
    {
    return x+y;
    }

    then i created Visual C# console app
    name dlltest
    /program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace dlltest
    {
    class Program
    {

    static void Main(string[] args)
    {
    simple.class1 myobject = new simple.class1.addition();
    Console.Write("the sum is:{0} ",myobject.addition(2,3));
    }
    }
    }
    simple.dll added in references while debugging i'm getting errors


    C:\Users\admin\Documents\Visual Studio 2008\Projects\dlltest\dlltest\Program.cs(19,56): error CS0426: The type name 'addition' does not exist in the type 'simple.class1'

    C:\Users\admin\Documents\Visual Studio 2008\Projects\dlltest\dlltest\Program.cs(20,60): error CS1061: 'simple.class1' does not contain a definition for 'addition' and no extension method 'addition' accepting a first argument of type 'simple.class1' could be found (are you missing a using directive or an assembly reference?)



    please help me to resolve this error.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: creating a dll in visual C++ class library to import in to visual C# console appl

    Your code in not a Visual C++ one. Perhaps, it is a managed C++ which is discussed in another Forum.
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2013
    Posts
    3

    Re: creating a dll in visual C++ class library to import in to visual C# console appl


  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: creating a dll in visual C++ class library to import in to visual C# console appl

    then i created Visual C# console app
    Your code is c# - not c++ which is the topic of this forum.

    Moderator, please would you move this thread.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: creating a dll in visual C++ class library to import in to visual C# console appl

    Quote Originally Posted by kanak View Post
    the simple.cpp is

    Code:
    #include "stdafx.h"
    
    #include "simple.h"
    int addition(int x, int y)
    {
        return x+y;
    }
    Your C++/CLI code snippet above (I added code tags, please always use them when posting code), when placed in the usual way in a .cpp implementation file, defines a free function, i.e. one that is not associated to any class. To define a class member function, which seems to be your intention, you'd need to explicitly specify the class name, so your function's header would need to look something like this instead:

    Code:
    int class1::addition(int x, int y)
    This looks more like a C++/CLI syntax issue than anything else. Things like that are discussed in the separate C++/CLI section. Additionally, there's hardly ever a reason to use C++/CLI in a managed project unless interop (i.e. combining managed with native code) is involved (or it's a pure C++/CLI project, but then no C# would be involved, of course). This suggests you actually may be going into interop (though there's no actual trace if that in your OP); and that has been discussed in the C++/CLI section quite some times as well, so you may also find helpful information about that there too.
    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.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: creating a dll in visual C++ class library to import in to visual C# console appl

    Quote Originally Posted by Eri523 View Post
    Code:
    int class1::addition(int x, int y)
    I would say,
    Code:
    int simple::class1::addition(int x, int y)
    or
    Code:
    namespace simple {
       int class1::addition(int x, int y)
       {
       . . .
       }
    }
    Besides,
    Code:
    namespace simple
    {
        public ref class class1
        {
        public:
            int addition(int x, int y);
    
        };
    }
    Best regards,
    Igor

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: creating a dll in visual C++ class library to import in to visual C# console appl

    Quote Originally Posted by Igor Vartanov View Post
    I would say,
    Code:
    int simple::class1::addition(int x, int y)
    Oh, you're right of course. I always have a using directive referring to the enclosing namespace right after the #includes in my implementation files. (In the context of the OP's code that would be using namespace simple;) Carelessly, I assumed one silently in the OP's code as well.

    Thanks for correction.
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured