0

i have a simple function in c++ (not a method of a class)

__declspec(dllexport) extern "C" void __stdcall TestFunc();  

i try to call it from c#:

[DllImport("ImportTest.dll")]  
public static extern void TestFunc();  

...  

TestFunc();

It throws an "entry point could't be found" exception.

Whats wrong?

Thank you for helping me :)

marsh-wiggle
  • 1,693
  • 2
  • 23
  • 41

2 Answers2

5

Try (guessing, that DLL is written in VS)

extern "C" __declspec(dllexport) void __stdcall TestFunc();

That's:

  • __declspec(dllexport) to notify compiler, that this function is to be exported from the DLL;
  • extern "C" mainly to prevent function name decorations;
  • __stdcall, because this is default calling convention if you specify none in [DllImport] directive.

In the future, you can check if your function is exported from DLL using Dll export viewer.

Community
  • 1
  • 1
Spook
  • 22,911
  • 14
  • 79
  • 146
  • Sry, spook, my demo code was incomplete, i corrected it. In the original it is defined correctly. I corrected my question now. – marsh-wiggle Jan 16 '14 at 11:49
  • Have you actually used `Dll export viewer` to check, what is the name of your exported function? If .NET can not find the entry point it means, that it can not find specified function name in DLL export list. I bet, that VS decorated name of your function despite `extern "C"`. – Spook Jan 16 '14 at 12:12
  • In the original DLL Export Viewer didn't show any exported function. Changing it to "extern "C" _declspec(dllexport) void TestFunc();" the function is shown. Great tool! Thx for your help! – marsh-wiggle Jan 16 '14 at 12:16
2

In C++ function , at header(if your function is declared in header) add

extern "C" _declspec(dllexport) void TestFunc();

at the function definition use

_declspec(dllexport) void TestFunc()
{

}

At C# side,you need to declare a function like

[DllImport(@"ImportTest.dll",
                 EntryPoint = "TestFunc",
                 ExactSpelling = false,
                 CallingConvention = CallingConvention.Cdecl)]
            static extern void NewTestFunc()

Now use , NewTestFunc()

Spook
  • 22,911
  • 14
  • 79
  • 146
vathsa
  • 293
  • 1
  • 6