Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,128 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,085 people online right now. Registration is fast and FREE... Join Now!




Importing functions from a dll in .NET 2005

 
Reply to this topicStart new topic

Importing functions from a dll in .NET 2005, Very beginner

jacobjordan
6 Jul, 2008 - 12:30 PM
Post #1

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,262



Thanked: 39 times
Dream Kudos: 1675
My Contributions
Every soul on earth that knows c++ probably knows this, but since i am just learning, i don't. I know how to do this in VB and C#, but i need to know how to do this in c++. If you don't understand what i am asking, i am trying to import a function inside a dll. In VB, this is done like
vb

Declare Function SomeFunction Lib "SomeLibrary" (ByVal Something As Something) As Something

In C#, this is how it's done
csharp

[DllImport("SomeLibrary")]
static extern Something SomeFunction(Something Something)

Thing is, i don't know how to do this in C++. A simple example should be all i need.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Importing Functions From A Dll In .NET 2005
6 Jul, 2008 - 02:06 PM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 33 times
My Contributions
There are multiple ways to do this, and it really depends the code you want to import.

I'm going to assume your on Windows, since you mentioned C# and VB.

If the code you want to import is in a .dll file, you should have a header file, and a .lib file. For example, this allows you to use pretty much any Windows function.

CODE

#include <windows.h> // Includes the prototype of the functions to import

int main(void)
{
  while(!GetAsyncKeyState(VK_ESCAPE))
    Sleep(10);

  return 0;
}


To compile the above you, don't actually have to do anything special. A lot of Windows functions will require you to add a stub library (basically tells the compiler how to import the functions from the .dll).

If you use any of the GDI+ functions, you'll have to add gdi32.lib. OpenGl, opengl32.lib. Some specific kernel functions, user32.lib.

To add a library, if you are running from the command line, you simply just add the library to the compilation string.

For GDIs, you have to add it to the linker list, which differs based on IDE you are useing.

For Visual Studio, the simple way to add one is to go to
Project -> <Project Name> Properties -> Linker -> Input -> and add your .lib files to "Additional Dependencies"

You can also use LoadLibrary() and GetProcAddress(), but that is a lot more complected, and I don't really want to get into that.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 01:40AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month