Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No functions in C# DLL with RGiesecke.DllExport

Tags:

c#

dll

unmanaged

I am trying to make a DLL in C# for use in a couple other languages. I found RGiesecke's DllExport but it doesn't seem to work. It builds just fine and makes a dll, but when I open it in Dependency Walker it doesn't show any functions, and my calling code can't find them either.

I created a new "Class Library" project (VS 2013) and then installed "Unmanaged Exports (DllExport for .Net)" from NuGet. Are there any project settings I need?

Here is my code.

using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;

namespace ToolServiceDLL
{
    public class Class1
    {
      [DllExport("addUp", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
      public static double addUp(double num1, double num2)
      {
        return num1 + num2;
      }

      [DllExport("get5", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
      public static int get5()
      {
        return 5;
      }
    }
}
like image 626
runfastman Avatar asked Dec 22 '15 14:12

runfastman


People also ask

What is not a function in C?

Tprintf is not a C function. But printf, f printf and sprintf is a C function.

Can C program run without function?

So actually C program can never run without a main() . We are just disguising the main() with the preprocessor, but actually there exists a hidden main function in the program. Source : LearnHacking.

What is no argument in C?

Function with no argument and no return value: When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return a value, the calling function does not receive any data from the called function.


2 Answers

I found the problem. It has it in the RGiesecke Documentation, but I missed it. In the project settings->Build->Platform target: you can not have it set to "Any CPU". You must have it set to x64 or x86 depending on if you want to use it in a a 64 or 32 bit application.

like image 171
runfastman Avatar answered Sep 20 '22 13:09

runfastman


I had a similar problem, but had already set the platform target to x64 and had the following error:

The name 'CallingConvention' does not exist in the current context

I found adding the using directive System.Runtime.InteropServices resolve the problem.

like image 39
theotheraussie Avatar answered Sep 17 '22 13:09

theotheraussie