Resources » .NET programming » General

Extern "C" int __declspec(dllexport)


Posted Date: 15-Jan-2009  Last Updated:   Category: General    
Author: Member Level: Silver    Points: 10


In this article, You will learn about extern "C" int __declspec(dllexport). with help of this article , I want to explain deeply about extern "C" directive and the __stdcall keyword



Find the detail about extern "C" int __declspec(dllexport)


When providing an interface between modules that are built using two different vendor's compilers, it is important to ensure that the modules use common calling- and naming- conventions. This document discusses interface techniques for mixing modules created with Microsoft Visual C++ and modules created with Power++ or Watcom C/C++.

To provide an interface between Visual C++ and Power++ or Watcom C/C++, you will need to export and import functions and/or variables. Function definitions and prototypes should include the extern "C" directive and the __stdcall keyword. The former forces C linkage (i.e., turns off C++ name mangling); the latter ensures the standard Win32 calling convention is used. Variable definitions and declarations should also include the extern "C" directive.

If you want to write an Power++ or Watcom C/C++ EXE that calls functions or accesses variables exported from a Visual C++ DLL, then you must use the Visual C++ lib command to create an import library for the DLL. This import library can then be added to your Power++ or Watcom IDE project. The import library can also be used with the Watcom C/C++ wlink command if you are using command-line tools.

Calling Exported Functions
For each exported function in the DLL that is to be called from the EXE, do the following:
In the DLL source, define the function as:

extern "C" type __declspec(dllexport) __stdcall name( parameters )
{
...
}

In the EXE source, declare (prototype) the function as:

extern "C" type __declspec(dllimport) __stdcall name( parameters );

Accessing Exported Variables

For each exported variable in the DLL that is to be accessed from the EXE, do the following:
In the DLL source, define the variable as:

extern "C" { type __declspec(dllexport) name; }

Note: The braces ({}) are required. If you omit them, this becomes a declaration, not a definition.

In the EXE source, declare the variable as:

extern "C" type __declspec(dllimport) name;

Example

The following sample illustrates this technique:

msdll.cpp:
#include

extern "C" { int __declspec(dllexport) var1 = 10; }

extern "C" int __declspec(dllexport) __stdcall func1( int i )
{
printf( "func1(): var1 = %d\n", var1 );
printf( "func1(): i = %d\n", i );
return i * 2 + var1;
}

watexe.cpp:
#include

extern "C" int __declspec(dllimport) var1;
extern "C" int __declspec(dllimport) __stdcall func1( int i );

void main(void)
{
printf( "main(): var1 = %d\n", var1++ );
printf( "main(): func1 = %d\n", func1( 5 ) );
}


Reference: http://www.sybase.com/detail?id=44623


Did you like this resource? Share it with your friends and show your love!

Responses to "Extern "C" int __declspec(dllexport)"
Author: ChandraShekar Thota    26 Jan 2009Member Level: Gold   Points : 2
Good one

Chandrashekar Thota(Editor, MVP)



Guest Author: Nikky     29 Nov 2011
getting error for line in src code:
Extern "C" {
int __declspec(dllexport) var1 = 10;
}

error details: error c2059: syntax error : 'string'

Is there any correction for it?



Guest Author: Nikky     29 Nov 2011
getting error for line in src code:
extern "C" { int __declspec(dllexport) var1 = 10; }

error details:
error c2059: syntax error : 'string'

Is there any correction for it?



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    jack
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India