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.
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. :cool:
Re: creating a dll in visual C++ class library to import in to visual C# console appl
Re: creating a dll in visual C++ class library to import in to visual C# console appl
Quote:
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.
Re: creating a dll in visual C++ class library to import in to visual C# console appl
Quote:
Originally Posted by
kanak
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.
Re: creating a dll in visual C++ class library to import in to visual C# console appl
Quote:
Originally Posted by
Eri523
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);
};
}
Re: creating a dll in visual C++ class library to import in to visual C# console appl
Quote:
Originally Posted by
Igor Vartanov
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. :o
Thanks for correction. :)