C#调用C/C++ dll
2009-04-03 16:15
411 查看
实例代码见:http://download.csdn.net/source/1172831
本文通过实例代码说明如何在托管的C#代码中调用非托管的Win32
API或者自己用C/C++写的Dll中的函数,以及如何传递输入、输出字符串参数,结构类型参数等问题。
Win32 C/C++ DLL代码如下(最终编译成dll.dll):
#include "stdafx.h"
#include <stdio.h>
#ifdef _MANAGED
#pragma managed(push, off)
#endif
//简单的函数调用测试
extern "C" __declspec(dllexport)
int Test(void)
{
return 34;
}
//输入输出字符串传递测试
extern "C" __declspec(dllexport)
int TCharPara(char *in, int len, char *out)
{
memcpy(out, in, len);
return 0;
}
//结构参数传递测试
struct _XY
{
int
x;
int
y;
};
extern "C" __declspec(dllexport)
int TStructPara(struct _XY *xy)
{
xy->x = 5;
xy->y = 5;
return 0;
}
//回调函数测试
typedef void (CALLBACK *CBFunc)(char *);
extern "C" __declspec(dllexport)
int TCallback(CBFunc pf, char *out)
{
(*pf)(out);
return 0;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
调用Win32 C/C++ dll中函数的托管C#代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TCallCDll
{
//用于测试结构参数传递,对应Win32 dll中的结构_XY
[StructLayout(LayoutKind.Sequential)]
public
struct _XY
{
public
int x;
public
int y;
};
//Win32 C/C++ dll中的函数TCallback回调此函数
public delegate
void OurDllCallBack(String iin);
//user32.dll中的EnumWindows函数回调此函数
public
delegate bool SysCallBack(int hwnd, int lParam);
public
class TCallCDll{
[DllImport("dll.dll")]
//////////////////////////////////////////////////////////////////////////
//iin:
输入参数
//oout:
输出参数
//////////////////////////////////////////////////////////////////////////
public
static extern int TCharPara(String iin, int len, StringBuilder oout);
//测试结构参数传递的函数
[DllImport("dll.dll")]
public
static extern int TStructPara(ref _XY xy);
//自己的dll函数回调测试
[DllImport("dll.dll", CharSet
= CharSet.Ansi)]
public
static extern void TCallback(OurDllCallBack f,
String s);
//user32.dll中的函数回调测试
[DllImport("user32.dll")]
public
static extern int EnumWindows(SysCallBack x, int y);
}
public
partial class callCDllMain : Form
{
public
callCDllMain()
{
InitializeComponent();
}
//提供给dll.dll中的TCallback回调的函数
public
static void ff(String s)
{
Console.WriteLine(s);
}
//提供给user32.dll中的EnumWindows回调的函数
public
static bool Report(int hwnd, int lParam)
{
Console.Write("Window
handle is ");
Console.WriteLine(hwnd);
return
true;
}
private
void Form1_Load(object sender, EventArgs e)
{
StringBuilder
a = new StringBuilder(3);
TCallCDll.TCharPara("ABC", 3, a);
this.Text = a.ToString();
_XY xy
= new _XY();
TCallCDll.TStructPara(ref xy);
this.Text += "x:" + xy.x + ", y:" + xy.y;
OurDllCallBack
fff = new OurDllCallBack(ff);
TCallCDll.TCallback(fff, "This string is output in C&C++ dll call back
function set in C# code/n");
SysCallBack cb = new SysCallBack(Report);
TCallCDll.EnumWindows(cb, 0);
}
}
}
本文通过实例代码说明如何在托管的C#代码中调用非托管的Win32
API或者自己用C/C++写的Dll中的函数,以及如何传递输入、输出字符串参数,结构类型参数等问题。
Win32 C/C++ DLL代码如下(最终编译成dll.dll):
#include "stdafx.h"
#include <stdio.h>
#ifdef _MANAGED
#pragma managed(push, off)
#endif
//简单的函数调用测试
extern "C" __declspec(dllexport)
int Test(void)
{
return 34;
}
//输入输出字符串传递测试
extern "C" __declspec(dllexport)
int TCharPara(char *in, int len, char *out)
{
memcpy(out, in, len);
return 0;
}
//结构参数传递测试
struct _XY
{
int
x;
int
y;
};
extern "C" __declspec(dllexport)
int TStructPara(struct _XY *xy)
{
xy->x = 5;
xy->y = 5;
return 0;
}
//回调函数测试
typedef void (CALLBACK *CBFunc)(char *);
extern "C" __declspec(dllexport)
int TCallback(CBFunc pf, char *out)
{
(*pf)(out);
return 0;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
调用Win32 C/C++ dll中函数的托管C#代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TCallCDll
{
//用于测试结构参数传递,对应Win32 dll中的结构_XY
[StructLayout(LayoutKind.Sequential)]
public
struct _XY
{
public
int x;
public
int y;
};
//Win32 C/C++ dll中的函数TCallback回调此函数
public delegate
void OurDllCallBack(String iin);
//user32.dll中的EnumWindows函数回调此函数
public
delegate bool SysCallBack(int hwnd, int lParam);
public
class TCallCDll{
[DllImport("dll.dll")]
//////////////////////////////////////////////////////////////////////////
//iin:
输入参数
//oout:
输出参数
//////////////////////////////////////////////////////////////////////////
public
static extern int TCharPara(String iin, int len, StringBuilder oout);
//测试结构参数传递的函数
[DllImport("dll.dll")]
public
static extern int TStructPara(ref _XY xy);
//自己的dll函数回调测试
[DllImport("dll.dll", CharSet
= CharSet.Ansi)]
public
static extern void TCallback(OurDllCallBack f,
String s);
//user32.dll中的函数回调测试
[DllImport("user32.dll")]
public
static extern int EnumWindows(SysCallBack x, int y);
}
public
partial class callCDllMain : Form
{
public
callCDllMain()
{
InitializeComponent();
}
//提供给dll.dll中的TCallback回调的函数
public
static void ff(String s)
{
Console.WriteLine(s);
}
//提供给user32.dll中的EnumWindows回调的函数
public
static bool Report(int hwnd, int lParam)
{
Console.Write("Window
handle is ");
Console.WriteLine(hwnd);
return
true;
}
private
void Form1_Load(object sender, EventArgs e)
{
StringBuilder
a = new StringBuilder(3);
TCallCDll.TCharPara("ABC", 3, a);
this.Text = a.ToString();
_XY xy
= new _XY();
TCallCDll.TStructPara(ref xy);
this.Text += "x:" + xy.x + ", y:" + xy.y;
OurDllCallBack
fff = new OurDllCallBack(ff);
TCallCDll.TCallback(fff, "This string is output in C&C++ dll call back
function set in C# code/n");
SysCallBack cb = new SysCallBack(Report);
TCallCDll.EnumWindows(cb, 0);
}
}
}
相关文章推荐
- C#调用C++下Win32DLL,找不到入口问题,感谢TallMan
- C# 调用C++ DLL 尝试读取或写入受保护的内存
- Java通过C++调用 C# DLL
- c# 调用 研华库函数中 C++ 非托管 Dll 一例(包含指针成员的结构体的调用) [转贴]
- c#调用c++开发的dll const char* 返回值接收问题
- c++调用c#的dll
- C++通过DLL调用C#代码
- C# 调用C++ DLL 的类型转换
- C#中调用c++的dll如何适应32位跟64位的平台
- 编写C#调用的C++DLL
- c# 调用c++ dll
- C#调用C++动态库(dll)
- c#调用c++编译的dll的移植问题
- c#调用c++写成的dll文件,返回char*,返回数组,用ref接收,byte[]
- c#调用C++的DLL找不到入口点
- C# 调用C++DLL参数异常解决方法
- 关于C#中调用C++dll传递回调函数的问题
- c# 调用c++ dll文件中的bool类型解决办法
- C++通过DLL调用C#代码
- C# 调用C++ dll