0

I'm just trying to figure out how to call C code from C#.

I have the following C code:

#include <stdio.h>

extern void print(const char *message)
{
   printf("%s\\n", message);
}

I compile this with:

cl /LD printf.c

That produces a dll. But

dumpbin /exports printf.dll

shows no entry point:

Microsoft (R) COFF/PE Dumper Version 14.00.23918.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file printf.dll
File Type: DLL

Summary

    2000 .data
    1000 .gfids
    7000 .rdata
    1000 .reloc
   10000 .text

And trying to call it from C# turns up an EntryPointNotFoundException (I import it like this:)

[DllImport("printf.dll", EntryPoint = "print")]
static extern void print(string message);

So what am I missing here?

Aurast
  • 3,189
  • 15
  • 24
  • 2
    You did not tell the linker that it needed to be exported, `extern` does not do anything useful since that's the default. Use `__declspec(dllexport)` instead. Or a .def file. Or the linker's /EXPORT option. – Hans Passant Jun 13 '16 at 16:00
  • @Hans Thanks if you were to post that as an answer I would be happy to accept it. – Aurast Jun 13 '16 at 16:33
  • Fairly sure you know everything you need to complete this Q+A yourself. Do keep in mind that we already have a lot of existing Q+A about this, fairly doubtful your addition would be a useful one. – Hans Passant Jun 13 '16 at 16:39

0 Answers0