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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sry, spook, my demo code was incomplete, i corrected it. In the original it is defined correctly. I corrected my question now.
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".
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!
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.