Chitika

Thursday, February 2, 2012

VC++ DLL

DLL:

Create new Win32DLL project, say dllProj.

In header file define:

#ifdef PROJ_EXPORTS
#define PROJ_API __declspec(dllexport)
#else
#define PROJ_API __declspec(dllimport)
#endif

Add functions in header files with PROJ_API prefix and definitions in source file with PROJ_API prefix.

PROJ_API  int foo();

Usage:

Create new Win32Console project.

define:

Top define:
extern int foo();

In function, call:
a=foo();

right click project, click Reference..., Common Properties, Frameworks and References, Add New Reference, Select dllProj project.

compile and run :)

Dotnet Usage:

Using command like linux "strings", read the function signature in the dll, which would be like ?foo@@YAHPAD@Z.

In c#, declare as:

[DllImport("dllProj.dll", CharSet = CharSet.Ansi, EntryPoint = "?foo@@YAHPAD@Z")]
private static extern int foo(string port);

Now, use it in the class as normal static function.

No comments:

Post a Comment