1,105,334 Community Members

Dll from C++ to C#

Member Avatar
fiska
Newbie Poster
8 posts since Apr 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Ancient Dragon
Achieved Level 70
27,637 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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

Member Avatar
fiska
Newbie Poster
8 posts since Apr 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

>>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

Member Avatar
Ancient Dragon
Achieved Level 70
27,637 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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#.

Member Avatar
fiska
Newbie Poster
8 posts since Apr 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

#pragma comment(lib,"Releasetest4.lib");
Member Avatar
WolfPack
Posting Virtuoso
1,787 posts since Jun 2005
Reputation Points: 491 [?]
Q&As Helped to Solve: 135 [?]
Skill Endorsements: 15 [?]
Moderator
 
0
 

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
Member Avatar
fiska
Newbie Poster
8 posts since Apr 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article