c# - 将C++的dll函数导出到c#
问题描述
我在 C++ 中创建了一个 DLL,其中包含一个返回正在运行的进程总数的函数
这个 id dllmain.h文件
#pragma once
#include"stdafx.h"
#include<string>
extern "C" __declspec(dllexport) int size();
这是我的dllmain.cpp文件
int size() {
TCHAR szProcessName[MAX_PATH];
DWORD aProcesses[1024], cbNeeded ;
int cProcesses;
unsigned int i;
EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded);
cProcesses = cbNeeded / sizeof(DWORD);
return cProcesses;
}
这是我导出 DLL 函数的 c# 文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[DllImport("D://source//repos//Dll1//Debug//Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int size();
public Form1()
{
InitializeComponent();
}
private void click_Click(object sender, EventArgs e)
{
int s = size();
Hllolbl.Text = Convert.ToString("s");
}
private void close_Click(object sender, EventArgs e)
{
Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
但是当我运行我的 c# 应用程序时,hello 打印函数返回 null。我在多个站点上搜索并应用了多个解决方案,但无法解决此问题
解决方案
推荐阅读
- autohotkey - 自动点击java程序按钮
- c# - 根据 WPF 条件更改单元格的前景色绑定
- xamarin - Xamarin Forms Bind Array 元素在 listView 内
- api - 在 IBM Cloudant NoSQL DB 上出现错误“需要付款”
- mainframe - SCIM的大型机实现
- raku - 如何使用 :since with CompUnit
- outlook-addin - 日历概览上的 Outlook 加载项按钮
- apache-spark - 是否可以通过纱线 cli 使用应用程序名称获取纱线应用程序 ID
- swift - 消息大小较大时 appendMessageOperation 失败?
- dialogflow-es - 如何在活动聊天中使用对话流中的变量?有效载荷?