天天看点

c#中调用c++ dll的例子

首先来看看c++中dll工程的创建过程

我用的是vs2012,首先创建一个win32项目,在应用程序向导的应用程序类型中选者dll,假定我的项目名称为win32dll,那么向导会自动生成一个win32dll.cpp的文件,我们只要在其中加要输出的函数就可以了。

以下我写的几个函数

===================================================

// Win32Dll.cpp : 定义 DLL 应用程序的导出函数。

//

#include "stdafx.h"

static int g_init = 0;

void __stdcall Init ( )

{

    g_init++ ;

}

int __stdcall GetInitCount ( )

{

    return g_init ;

}

int __stdcall GetChange( int pay, int cost )

{

    return ( pay-cost) ;

}

//dll结构调用

typedef struct

{

    char name[32];

    char passwd[32];

}USER_INFO_S;

static USER_INFO_S *g_user = NULL ;

//dll中结构作为输入变量

void __stdcall SetUser( USER_INFO_S * user )

{

    if ( g_user != NULL ) delete g_user ;

    g_user = new USER_INFO_S ;

    memcpy( (char *)g_user, (char *)user, sizeof(USER_INFO_S) );

}

 //dll中结构作为输出变量

bool __stdcall GetUser( USER_INFO_S *user )

{

    if ( g_user != NULL )

    {

        memcpy( (char *)user, (char *)g_user, sizeof(USER_INFO_S) );

        return true ;

    }else

        return false ;

}

 ================================================

定义模块文件

仅仅这样系统编译后输出的函数名,wpf还是不能认识,还得加上一个模块定义文件

在工程中添加新建项,代码中的模块定义文件,取名叫win32dll.def

我想输出上面代码中的所有文件给wpf用,那么win32dll.def中的代码如下

================================================

LIBRARY win32dll

EXPORTS

Init

GetInitCount

GetChange

SetUser

GetUser

====================================================   再来看看wpf工程创建过程   创建wpf应用程序 在需要引用dll中函数的代码中插入函数引用,简单变量函数引用如下

        [DllImport("win32dll.dll", EntryPoint = "Init")]

        private static extern void Init();

        [DllImport("win32dll.dll", EntryPoint = "GetInitCount")]

        private static extern int GetInitCount();

        [DllImport("win32dll.dll", EntryPoint = "GetChange")]

        private static extern int GetChange( int pay, int cost );

含有字符串的结构定义如下

        public struct USER_INFO_S{

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]

            string name;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]

            string passwd;

        }

含有结构变量的函数引用定义如下

        [DllImport("win32dll.dll", EntryPoint = "SetUser")]

        private static void SetUser(ref USER_INFO_S user);

        [DllImport("win32dll.dll", EntryPoint = "GetUser")]

        private static bool GetUser(out USER_INFO_S user);

定义一个wpf窗体,在其中添加一些控件代码   计算找零部分的xaml代码 ========================================================================================         <TextBlock HorizontalAlignment="Left" Margin="98,48,0,0" TextWrapping="Wrap" Text="付款:" VerticalAlignment="Top"/>

        <TextBlock HorizontalAlignment="Left" Margin="98,80,0,0" TextWrapping="Wrap" Text="总价:" VerticalAlignment="Top"/>

        <TextBox Name="textBoxPay" HorizontalAlignment="Left" Height="23" Margin="145,48,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>

        <TextBox Name="textBoxCost" HorizontalAlignment="Left" Height="23" Margin="145,80,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>

        <TextBlock Margin="98,113,0,0" TextWrapping="Wrap" Text="找零:" VerticalAlignment="Top" HorizontalAlignment="Left" Width="27"/>

        <TextBox Name="textBoxChange" HorizontalAlignment="Left" Height="23" Margin="145,113,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>

        <Button Content="计算" HorizontalAlignment="Left" Margin="286,113,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> ==============================================================================     计算找零部分的c#代码 ==========================================================

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            int pay = int.Parse(textBoxPay.Text);

            int cost = int.Parse(textBoxCost.Text);

            int change = GetChange(pay, cost);

            textBoxChange.Text = change.ToString();

        }

=============================================================

初始化及初始化次数显示xaml代码

===================================================

        <Button Name="btnInit" Content="初始化:" HorizontalAlignment="Left" Margin="367,234,0,0" VerticalAlignment="Top" Width="75" Click="btnInit_Click"/>

        <TextBlock HorizontalAlignment="Left" Margin="367,200,0,0" TextWrapping="Wrap" Text="初始化次数:" VerticalAlignment="Top" RenderTransformOrigin="0.674,2.636"/>

        <TextBox Name="textBoxInitCount" HorizontalAlignment="Left" Height="23" Margin="449,202,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="38"/>

===================================================

 初始化及初始化显示c#代码

=========================================================

        private void btnInit_Click(object sender, RoutedEventArgs e)

        {

            Init();

            int count = GetInitCount();

            textBoxInitCount.Text = count.ToString();

        }

=========================================================

结构调用部分xaml代码

============================================================

        <TextBlock HorizontalAlignment="Left" Margin="98,162,0,0" TextWrapping="Wrap" Text="用户名:" VerticalAlignment="Top"/>

        <TextBlock HorizontalAlignment="Left" Margin="98,183,0,0" TextWrapping="Wrap" Text="密码:" VerticalAlignment="Top" RenderTransformOrigin="0.574,-0.565"/>

        <TextBox Name="textBoxName" HorizontalAlignment="Left" Height="23" Margin="166,165,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>

        <TextBox Name="textBoxPasswd" HorizontalAlignment="Left" Height="23" Margin="166,193,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>

        <Button Name="btnSet" Content="设置" HorizontalAlignment="Left" Margin="98,234,0,0" VerticalAlignment="Top" Width="75" Click="btnSet_Click"/>

        <Button Name="btnGet" Content="获取" HorizontalAlignment="Left" Margin="216,234,0,0" VerticalAlignment="Top" Width="75" Click="btnGet_Click"/>

============================================================

结构调用部分c#代码

===========================================================

        private void btnSet_Click(object sender, RoutedEventArgs e)

        {

            USER_INFO_S u = new USER_INFO_S();

            u.name = textBoxName.Text;

            u.passwd = textBoxPasswd.Text;

            SetUser(ref u);

        }

        private void btnGet_Click(object sender, RoutedEventArgs e)

        {

            USER_INFO_S u = new USER_INFO_S();

            GetUser(out u);

            textBoxName.Text = u.name;

            textBoxPasswd.Text = u.passwd;

        }

=============================================================

继续阅读