Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / Win32

Mixing .NET and native code

Rate me:
Please Sign up or sign in to vote.
4.85/5 (49 votes)
7 Apr 2014CPOL9 min read 163.6K   7.2K   139  
A first approach to mixing .NET and native code, using the C++/CLI gateway.
// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_EXPORTS
   #define ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_API __declspec(dllexport)
#else
   #define ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_API __declspec(dllimport)
#endif

// This class is exported from the AdR.Samples.CLRCallingNative.NativeDLL.dll
class ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_API CPerson {
public:
   // Initialization
   CPerson(LPCTSTR pszName, SYSTEMTIME birthDate);
   // Accessors
   unsigned int get_Age();

private:
   TCHAR m_sName[64];
   SYSTEMTIME m_birthDate;

   CPerson();
};


#ifdef __cplusplus
extern "C" {
#endif

extern ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_API int nAdRSamplesCLRCallingNativeNativeDLL;

ADRSAMPLESCLRCALLINGNATIVENATIVEDLL_API int fnAdRSamplesCLRCallingNativeNativeDLL(void);

#ifdef __cplusplus
}
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
France France
I started working in 1991 as an engineer. My pecialization: development in robotics.

After two years in this job, I began a new career leaving robotics, and coming to standard development as C++ expert.

This enabled me to diversify my knowledge by putting one foot on the side of the systems. Then began my double competence.

In 2000 I started a new job as consultant. Many experiences came with that new job, continuing with both development and systems subjects.

I reinforced my knowledge in technical architectures, IT security, and IT production management.

2011: a new experience. I founded the company Net-InB, a computer services company specialized in the fields of infrastructures, systems and software.

Now I am both technical and software architect specialized in Microsoft products and technologies.

Comments and Discussions