将C ++的dll函数导出到c#

时间:2018-05-26 19:07:42

标签: c# c++ dll

我在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 print函数返回null。 我在多个站点上搜索并应用了多个解决方案但无法解决此问题

0 个答案:

没有答案