Call Unmanaged Code. Part 1 - Simple DLLImport

本文介绍了如何从C#调用C++编写的DLL文件中的函数,包括简单的整数加法函数调用及复杂字符串操作函数的使用。文章通过具体实例展示了如何正确声明DllImport属性,并解释了不同类型参数的传递方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Call Unmanaged Code. Part 1 - Simple DLLImport By Vyacheslav Biktagirov

Managed world is beautiful, I have all classes I want in FrameWork.. But what if I want call some unmanaged code? For instance, I have DLL written in C++, and want use it from C#.

Let's look some code. Our DLL exports some function, in CDecl convention, that sums two integers:

extern "C" __declspec(dllexport) __cdecl int sum(int a,int b);

And, of course, we want reuse this code in C#. We must recall, that it is no "direct" way to call unmanaged code, but we must inform the compiler, what we want to call, how, and where is needed code located.

 

[DllImport("TestDll.dll", EntryPoint="sum",
ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
static extern int sum(int a,int b);
and now we can call it like normal C# function.
x=5;
y=7;
z=sum(x,y);			// x will receive 12
Here is full C# client code - tested for Beta2.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WindowsApplication6
{
	/// 
	/// Summary description for Form1.
	/// 
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox3;
		/// 
		/// Required designer variable.
		/// 
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// 
		/// Clean up any resources being used.
		/// 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// 
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// 
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			//
			// button1
			//
			this.button1.Location = new System.Drawing.Point(64, 192);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(144, 64);
			this.button1.TabIndex = 0;
			this.button1.Text = "call sum";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			//
			// textBox1
			//
			this.textBox1.Location = new System.Drawing.Point(40, 120);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(72, 22);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "2";
			//
			// label1
			//
			this.label1.Location = new System.Drawing.Point(128, 128);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(16, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "+";
			//
			// textBox2
			//
			this.textBox2.Location = new System.Drawing.Point(152, 120);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(56, 22);
			this.textBox2.TabIndex = 3;
			this.textBox2.Text = "3";
			//
			// label2
			//
			this.label2.Location = new System.Drawing.Point(224, 120);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(24, 23);
			this.label2.TabIndex = 4;
			this.label2.Text = "=";
			//
			// textBox3
			//
			this.textBox3.Location = new System.Drawing.Point(248, 120);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(112, 22);
			this.textBox3.TabIndex = 5;
			this.textBox3.Text = "5";
			//
			// Form1
			//
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(576, 322);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox3,this.label2,this.textBox2,this.label1,this.textBox1,this.button1});
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// 
		/// The main entry point for the application.
		/// 
		[STAThread]
		static void Main()
		{
			Application.Run(new Form1());
		}

		#region My Code
		#region Dll Imports
		[DllImport("TestDll.dll", EntryPoint="sum",
		ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
		static extern int sum(int a,int b);
		#endregion
		#region Button Click Events
		private void button1_Click(object sender, System.EventArgs e)
		{
			textBox3.Text=(int.Parse(textBox1.Text)+int.Parse(textBox2.Text)).ToString();
		}
		#endregion
		#endregion
	}
}
It sounds very simple, becouse "int" is isomorphic type, says, int in C# and ind C++ is identical. What we can do, when we want operate non-isomorhic types, like String? Recall, that .NET string is some Class, while C++ string is char*,or wchar_t*,or BSTR, .. String may be embedded in a structure, or pointed by pointer, or even something more exotic. Let's call some string function.

 

[DllImport("Advapi32.dll", EntryPoint="GetUserName", ExactSpelling=false,
			 SetLastError=true)]
		static extern bool GetUserName(
			[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
			[MarshalAs(UnmanagedType.LPArray)] Int32[] nSize );
This function receives two parameters: char* and int*. Becouse we must allocate char* buffer and receive string by pointer, we can't use UnmanagedType.LPStr attribute, so we pass ANSI string as byte array. int* is more simple-it's 1-element Int32 array. Let's call it:
		private void button2_Click(object sender, System.EventArgs e)
		{
			byte[] str=new byte[20];
			Int32[] len=new Int32[1];
			len[0]=20;
			GetUserName(str,len);
			MessageBox.Show(System.Text.Encoding.ASCII.GetString(str));
		}
We allocate 20 bytes for receiving ANSI string,one element in Int32 array, set 20 as max string length and call it. For receiving string from byte array I used Text.Encoding.ASCII class.

That's enough for first part. Second part will speak about more complex interop.

内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值