![](/img/trans.png)
[英]How do you call functions from a Windows Runtime Component (c#) in a UWP app written in c#?
[英]how do you import a C .DLL file to a C# windows form application and call its functions that are defined in headers?
我是編程新手,我基本上是想將用C編寫的第三方.dll文件提取到C#代碼中,然后將其功能之一輸出到接口。 下面是.DLL使用的頭文件中的函數聲明。 我認為我必須在C#中重新定義它,因為它不能使用C標頭。
int SPI_GetNumChannels(int * numChannels);
這是我的C#代碼
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public class App
{
[DllImportAttribute(@"C:\Users\Documents\libMPSSE.dll", EntryPoint = "SPI_GetNumChannels")]
public static extern int SPI_GetNumChannels(ref int numChannels);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = numChannels.ToString();
}
}
}
我完全在正確的軌道上嗎? 我知道最后一行是不正確的,任何幫助將不勝感激!
將外部方法放在稱為NativeMethods或UnsafeNativeMethods的單獨的靜態類中是一個好習慣。
然后,您已經非常接近。
int channels = 0;
NativeMethods.SPI_GetNumChannels(ref channels);
textBox1.Text = channels.ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.