0

I have created a DLL in c++ that containing a function which returns total number of running processes
This id dllmain.h file

#pragma once
#include"stdafx.h"
#include<string>
extern "C" __declspec(dllexport) int size();

This is my dllmain.cpp file

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;
}

This is c# file in which I export my DLL function

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)
    {

    }     
 }
 }

But when i run my c# application hello print function returns null. I searched on multiple sites and apply multiple solutions but unable to resolve this problem

  • I wonder whether use of the double "//" is the problem. Use single "/" or "\\". – R Sahu May 26 '18 at 19:29
  • when I use "/" instead of "//" system shows error System.EntryPointNotFoundException: 'Unable to find an entry point named 'Print_Hello()' in DLL 'D:/source/repos/Dll1/Debug/Dll1.dll'.' –  May 26 '18 at 19:34
  • I don't know how to help with that. Hopefully somebody does. Good luck. I would suggest adding that additional info to your post. – R Sahu May 26 '18 at 19:36
  • but when i remove "()" from Print_Hello in entry point above exception remove but it still return null –  May 26 '18 at 19:37
  • For easier debugging, I would say put the DLL where the exe is just in case accessing the path requires admin rights for applications. Then write the dll name in the dllimport. The EntryPart you can feel safe to remove as the name you've giving to your function is same as the one in the C++. This attribute only made when you want to give a different name to your method in C#. –  May 26 '18 at 19:42
  • 1
    A better approach would be, instead of returning a std::string in your C++ function, you could make it void and pass to it a StringBuilder as your buffer, which in C++ would be as char pointer and the length of the string. That way it will modify your string by reference instead of trying to return unmanaged string to a managed language string. –  May 26 '18 at 20:00
  • sorry i don't get what you are trying to explain..If you don't mind kindly provide some code that explain your answer –  May 26 '18 at 20:08
  • Perhaps this link would help https://stackoverflow.com/questions/874551/stdstring-in-c – Killzone Kid May 26 '18 at 21:39

0 Answers0