0

I'm trying to create a small .dll which will be able to cancel process by sending CTRL+C signal to that process.

And I'm exporting my function like this:

        [DllExport("StopProgram")]
        public static void StopProgram(int processId)

Here is the full source code: https://github.com/maifeeulasad/SIGINT-APP/blob/210561ed4e49d89321ed4d82627ac162fbf69c32/SendCtrlC.cs

Here I'm using this library: https://github.com/3F/DllExport. It has the most stars on GitHub and most download NuGet as of my knowledge.

But when I'm creating a small .NET console application to test, and trying to invoke function exported, it's throwing an error saying: System.EntryPointNotFoundException: 'Unable to find an entry point named 'StopProgram' in DLL 'SigInt-CtrlC.dll'.'

Here is my relavant code:

[DllImport("SigInt-CtrlC.dll")]
static extern void StopProgram(int processId);

StopProgram(pid); // <----- the error is being thrown here

Here is the reference to the exact .dll: https://github.com/maifeeulasad/SIGINT-APP/blob/94ea0d9db757649ba190fd0dca36a65e1a17a784/bin/Release/netstandard2.0/SigInt-CtrlC.dll

Please note

  • I've copied the generate .dll both is root source folder where all my source code lies and also in the debug folder where the .exe build and other .dlls being generated.
  • I'm also working on the same device.

So there should be no system difference issue

I've tried opening it into dependency walker 2.2, it gives me some error saying that it has some missing deps. Then I read this thread: https://stackoverflow.com/a/36244483/10305444, and stopped debugging the .dll

You can either give me a fix to to import it properly, or export it.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86

1 Answers1

2

There's a couple things going on. The biggest thing is that DllExport isn't required if you are calling managed code from managed code. (C# -> C#). You typically want to use it for unmanaged to managed interop.

We can work through this though.

Second thing, the output in bin\Release\netstandard2.0\SigInt-CtrlC.dll is not the dll you want to use. This dll doesn't have any exports. You can verify this by using a dev cmd prompt and dumping the export table: dumpbin /exports SigInt-CtrlC.dll

> dumpbin /exports SigInt-CtrlC.dll
Microsoft (R) COFF/PE Dumper Version 14.36.32502.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file SigInt-CtrlC.dll

File Type: DLL

  Summary

        2000 .reloc
        2000 .rsrc
        2000 .text

The files with the exports created from DllExport are in the bin\Release\<Platform> folder.

> dumpbin /exports x64/SigInt-CtrlC.dll
Microsoft (R) COFF/PE Dumper Version 14.36.32502.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file x64\SigInt-CtrlC.dll

File Type: DLL

  Section contains the following exports for \SigInt-CtrlC.dll

    00000000 characteristics
    6429B342 time date stamp Sun Apr  2 12:54:26 2023
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 0000291E StopProgram

  Summary

        2000 .reloc
        2000 .rsrc
        2000 .sdata
        2000 .text

Finally, I was unable to get the managed to managed interop to work (unmanaged to managed was fine) unless I had this specific option checked in the .NET DllExport options:

Image showing that the 'Use out IL Assembler' is checked

Using the correct dll and the corresponding option the DllImport call started working.

Hank
  • 1,976
  • 10
  • 15
  • When you say it wouldn't work, what did happen? – Charlieface Apr 03 '23 at 20:25
  • An `SEHException` occurred. I made sure this was not due to mismatched architectures either because I tested x86 from and x64 exe and got the expected `BadFormatException`. – Hank Apr 03 '23 at 20:35
  • Did you try different `CallingConvention` for example `CDecl` – Charlieface Apr 03 '23 at 20:35
  • Just tested. CDecl didn't help (turned off 'Use our IL Assembler'). When I turned the option back on, CDecl works. – Hank Apr 03 '23 at 20:42