Hi,

I was lately interested in creating dll from C++ and using the functions in C#, I've came across one tutorial where the guy is creating dll in C++ and using them in another c++ project with VS 2003.

I've tried the same with my VS 2005 but I didn't worked! I was misssing this .lib file (that this guy from tutorial copies and paste together with the lib file to the project where u use the dll functions) my VS 2005 is not creating .lib files :cry:

I'm tired searching stuff on the internet (couldn't find something that really shows you on how to), I would be very appreciative is some could explain me in steps on how to use dll functions in C# as well how to create proper dll file from C++.

by the way what do we need .lib file for? is it sort of linking the resource to the new project kinda making aware the compiler where to look for the function?

another questions

1. if I create just UI with C# and all functionality of application in C++ and the use the functions in C# with dll would this make my app faster (meaning that c++ will compile natively)??

2. when one would use MFC :o ?

thanx million times,
f

>>I've tried the same with my VS 2005 but I didn't worked! I was misssing this .lib file

that compiler will not generate a .lib file unless there are some exported symbols (either functions and/or objects). __declspec( dllexport ) will export them

__declspec( dllexport ) int foo;

>>what do we need .lib file for?
In some C/C++ programs you don't need it at all, the alternative is to use LoadLibrary().

using .lib files is most commonly used by the compiler's linker to resolve the functions that are called from your program when generating the .exe file.

>>when one would use MFC
MFC is a collection of c++ classes for developing GUI programs. Normally used for business applications, but can be used for simple games. www.codeproject.com has a huge collection of free MFC programs and helpful libraries that you can plug into your project. It also has a little bit of C# stuff.

>>I've tried the same with my VS 2005 but I didn't worked! I was misssing this .lib file

that compiler will not generate a .lib file unless there are some exported symbols (either functions and/or objects). __declspec( dllexport ) will export them

__declspec( dllexport ) int foo;

I've done this in my C++ dll.

extern 'C' __declspec( dllexport ) double area(int& a, int& b);
//there was here some code created by studio, and after that I've added the function
double area(int& a, int& b)
{
  return a*b;
}

and in the other C++ project I've copied the dll file from the former project and paste it into the new project and in my cpp file
I've added this code:

extern 'C' __declspec(dllimport) double area(int& a, int& b);

//code goes here and in main () I've added
int a(6), b(33)
cout << "area is:" <<area(a,b) <<endl;

it didn't really work... but my main interes is using C++ dll into a C# app, so far I can't use c++ dll's even in a c++ console app. :sad:

thanx

I am supprised that even compiled -- C is surrounded by double quotes, not single quotes

// DLL code
extern "C" {

	__declspec( dllexport )double _cdecl area(int& a, int& b)
	{
		return a*b;
	}
};
// application program
#pragma comment(lib,"Releasetest4.lib");
extern "C" {

	__declspec( dllimport )double _cdecl area(int& a, int& b);
};


int _tmain(int argc, _TCHAR* argv[])
{
	int a = 1;
	int b = 2;
	double x = area(a,b);
	return 0;
}

When you run this, the dll must be either in the current working directory or in one of the directories in your PATH environment variable.

I have no clue how to make that dll work with C#.

ok now this code is working in a c++ project, but I dont understand this part:

#pragma comment(lib,"Releasetest4.lib");

ok now this code is working in a c++ project, but I dont understand this part:

#pragma comment(lib,"Releasetest4.lib");

It is a preprocessor directive telling the compiler to link the Releasetest4.lib library. Anyway I think this is supported only in the Visual Studio compiler. Therefore it is not encouraged. The best way is to use the "link" option of the compiler.

E.g. for Visual Studio

cl filename.cpp /link Releasetest4.lib

It is a preprocessor directive telling the compiler to link the Releasetest4.lib library. Anyway I think this is supported only in the Visual Studio compiler. Therefore it is not encouraged. The best way is to use the "link" option of the compiler.

E.g. for Visual Studio

cl filename.cpp /link Releasetest4.lib

Thanx a lot this helped.

Fiska

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.