C++的Cpp文件
#include
<
iostream
>
#define MODULEAPI
#include " tracewrite.h "
using namespace std;
void Func1() { cout << " i am function 1 " << endl; }
void Func2() { cout << " i am function 2 " << endl; }
void Func3() { cout << " i am function 3 " << endl; }
#define MODULEAPI
#include " tracewrite.h "
using namespace std;
void Func1() { cout << " i am function 1 " << endl; }
void Func2() { cout << " i am function 2 " << endl; }
void Func3() { cout << " i am function 3 " << endl; }
对应的头文件:(.h)
#ifndef _MODULE_
#define _MODULE_
#ifndef MODULEAPI
#define MODULEAPI __declspec(dllexport)
#else
#define MODULEAPI __declspec(dllimport)
#endif
// 头文件
#ifdef __cplusplus
extern " C " {
#endif
MODULEAPI void Func1();
MODULEAPI void Func2();
MODULEAPI void Func3();
#ifdef MODULEAPI
}
#endif
#endif // _MODULE_
#define _MODULE_
#ifndef MODULEAPI
#define MODULEAPI __declspec(dllexport)
#else
#define MODULEAPI __declspec(dllimport)
#endif
// 头文件
#ifdef __cplusplus
extern " C " {
#endif
MODULEAPI void Func1();
MODULEAPI void Func2();
MODULEAPI void Func3();
#ifdef MODULEAPI
}
#endif
#endif // _MODULE_
C#文件来引用上面的C++的方法:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
Func1();
}
[DllImport( " TestProj.dll " )]
private static extern void Func1();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
Func1();
}
[DllImport( " TestProj.dll " )]
private static extern void Func1();
}
}
要引用上面的C++代码的C++代码:
#include
"
stdafx.h
"
#include " traceWrite.h "
#pragma comment(lib , "TestProj.lib")
void main()
{
Func1();
Func2();
// ...
}
#include " traceWrite.h "
#pragma comment(lib , "TestProj.lib")
void main()
{
Func1();
Func2();
// ...
}
其对应的头文件如下:
#ifndef _MODULE_
#define _MODULE_
#ifndef MODULEAPI
#define MODULEAPI __declspec(dllexport)
#else
#define MODULEAPI __declspec(dllimport)
#endif
// 头文件
#ifdef __cplusplus
extern " C " {
#endif
MODULEAPI void Func1();
MODULEAPI void Func2();
MODULEAPI void Func3();
#ifdef MODULEAPI
}
#endif
#endif // _MODULE_
#define _MODULE_
#ifndef MODULEAPI
#define MODULEAPI __declspec(dllexport)
#else
#define MODULEAPI __declspec(dllimport)
#endif
// 头文件
#ifdef __cplusplus
extern " C " {
#endif
MODULEAPI void Func1();
MODULEAPI void Func2();
MODULEAPI void Func3();
#ifdef MODULEAPI
}
#endif
#endif // _MODULE_