.Net互操作2

1.C++创建Dll,Win32控制台,空项目,添加头文件NativeLib.h,NativeLib.cpp

extern "C" __declspec(dllexport) void __stdcall PrintMsg(const char* msg);
#include <stdio.h>
#include "NativeLib.h"


void __stdcall PrintMsg(const char* msg)
{
    printf("%s\n", msg);
    return;
}

2.C#创建控制台应用程序HelloWorld

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace HelloWorld
{
    class Program
    {
        [DllImport("NativeLib.dll")]
        static extern void PrintMsg(string msg);
        static void Main(string[] args)
        {
            PrintMsg("Hello world!");
            Console.ReadLine();
        }
    }
}

3.修改C++项目属性:配置属性->连接器->常规->输出文件

修改C#项目属性:生成->输出->输出路径

两个输出路径要一致即可(eg:F:\试验\ex\Chapter 1\x86\Debug\NativeLib.dll(C++Dll输出文件),F:\试验\ex\Chapter 1\x86\Debug(C#输出路径))。

posted @ 2013-11-15 20:06  ccjcjc  阅读(158)  评论(0编辑  收藏  举报