【发布时间】:2017-10-31 11:10:42
【问题描述】:
我对如何从我的 c# 代码中引用我的 c 代码感到困惑。我已经阅读并意识到 DLL 很有用,但我不明白如何连接我的 dll。据我了解,我从我的 c 代码中制作了 dll?请帮忙
C#(视觉工作室代码)
public partial class Form1: Form{
[DllImport("simple.dll", CallingConvention = CallingConvention.Cdec1)]
public static extern void pinfo(string str);
private void button1_Click(object sender, EventArgs e){
pinfo("yay");
}
}
C 代码 - simple.c
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
__declspec( dllexport ) void pinfo(char* str){
printf("You are in the method %s\n, str);
}
int main(void){
}
我的问题是如何将 c 代码连接到 c# 代码,以便打印出“You are in the method yay”行?我不明白如何将 dll 文件链接到 Visual Studio 或如何创建 dll 文件。谢谢
【问题讨论】:
-
您的代码对我来说看起来不错。当您尝试运行它时会发生什么? (请注意,您确实需要先导出
pinfo函数)。使用__declspec( dllexport ) -
@dai 我添加了那行,我不确定如何运行它,因为我有一个 Visual Studio 项目,然后是一个与我的 Visual Studio 项目无关的 simple.c 文件,我不确定如何链接它们
-
阅读有关如何创建 dll 的文档。你这样做了吗?
标签: c# c visual-studio dll dllimport