c#中调用c++ dll的例子
来源:互联网 发布:56式半自动步枪数据 编辑:程序博客网 时间:2024/06/06 14:56
首先来看看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
[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);
<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"/>
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;
}
=============================================================
- c#中调用c++ dll的例子
- C#调用C++DLL例子
- C#下调用C写的dll的例子
- C#调用C的DLL
- C#中如何调用C++编写的DLL
- C#中如何调用C++编写的DLL
- 在C#中调用C++Builder编写的DLL
- C#中如何调用C++编写的DLL
- 在C#中调用C++Dll函数的方法
- C#中如何调用C++编写的DLL
- C#中调用C++DLL笔记
- C#中使用DllImport调用C++dll
- C#中调用C++dll错误问题
- C#调用C++DLL的小总结8---C++Dll中函数返回字符串指针
- C#调用C++DLL
- C#调用C++dll
- C#调用C++dll
- C#调用C++DLL
- 教你如何找到身边的免费WiFi热点
- ios开发---隐藏StatusBar
- MFC中使用FLASH相关
- UVA 11235 - Frequent values (RMQ的基础应用)
- 宽带ADSL猫指示灯含义及不亮问题解答
- c#中调用c++ dll的例子
- 一篇经典的求职经历博客,值得深入研究和学习
- POP3协议
- 图的深度遍历
- ios---面试
- 我的e家无线路由器设置
- 很浅浅的谈谈 python doctest
- Spurious wakeup
- Cron表达式