标题:c++运行不起222222
只看楼主
伍则帝
Rank: 3Rank: 3
来 自:四川省南充市高坪区
等 级:论坛游民
威 望:6
帖 子:100
专家分:34
注 册:2013-3-16
结帖率:84%
 问题点数:0 回复次数:0 
c++运行不起222222
// p108tongxulu.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"

#define EXPORT_DLL extern "C" __declspec(dllexport)

  

//暴露的方法调用

EXPORT_DLL int Add(int a,int b)

{

    return a + b;

}

 

//返回数据结构定义

struct Bar

{

public :

    int id;

    char* name;

};

 

//使用引用返回结构

EXPORT_DLL void GetBar(Bar& bar)

{

    //Bar b;

    bar.id = 10;

    bar.name = "hi bar 中文了";

 

    //bar = b;

}

 

//返回类

class Foo

{

public :

    int id;

    char* name;

};

 

//引用返回类

EXPORT_DLL void GetFoo(Foo& foo)

{

    foo.id = 100;

    foo.name = "I'm 傻瓜";

}

这是我的源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]

    public struct Bar
    {



        /// int

        public int id;



        /// char*

        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]

        public string name;

    }





    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]

    public struct Foo
    {



        /// int

        public int id;



        /// char*

        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]

        public string name;

    }



    class Program
    {

        [DllImport("CppDll.dll", EntryPoint = "Add")]

        extern static int Add(int a, int b);



        [DllImport("CppDll.dll", EntryPoint = "GetBar", CharSet = CharSet.Ansi)]

        extern static void GetBar(ref Bar bar);



        [DllImport("CppDll.dll", EntryPoint = "GetFoo", CharSet = CharSet.Ansi)]

        extern static void GetFoo(ref Foo foo);





        static void Main(string[] args)
        {

            Console.WriteLine(Add(5, 4));



            Bar b = new Bar();

            GetBar(ref b);

            Console.WriteLine("b's id is " + b.id);

            Console.WriteLine("b's name is " + b.name);



            Foo f = new Foo();

            GetFoo(ref f);

            Console.WriteLine("f's id is " + f.id);

            Console.WriteLine("f's name is " + f.name);



            Console.Read();

        }

    }

}
搜索更多相关主题的帖子: 应用程序 include public return 中文 
2013-10-08 20:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-421590-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061262 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved