Solved

Got "EntryPointNotFoundException" when C# program called an external DLL (written in C)

Posted on 2013-11-07
4
1,313 Views
Last Modified: 2013-11-12
Hi experts
My C# program called a function in an external DLL (written in C) but was given an "EntryPointNotFoundException".

In the C function, I'd got both the extern "C" and the "__declspec(dllimport)" as follows:
extern "C" {
bool __stdcall __declspec(dllimport) test(int n, char * input);
}

Open in new window


In my C# program, I had got:
 [DllImport("test.dll", CallingConvention=CallingConvention.Cdecl)]
        public static extern bool test(int n, StringBuilder input);

Open in new window


Kindly please advise where I might have done incorrectly. Thanks in advance.
0
Comment
Question by:dominicwong
4 Comments
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 39633713
I would suggest generating an interop assembly to access the dll with more managed code.
http://msdn.microsoft.com/en-us/library/tt0cf3sx(v=vs.110).aspx
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 39634642
Remove '__stdcall' from the declaration, it causes the name to be appended with an ordinal when being exported. Also, you want to use '__declspec(dllexport)' there, because 'dllimport' does exactly the opposite. I.e.

extern "C" {
bool __declspec(dllexport) test(int n, char * input);
}
                                  

Open in new window

0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 39640913
jkr is right. the dllimport must be used from other c or c++ dll's (executables) which want to use the function.

you may use macros in c/c++ header to support both export and import with one header:

// header.h
#ifndef HEADER_H
#define HEADER_H

#ifdef __cplusplus
extern "C"
{
#endif
 
#ifdef TESTDLL_BUILD
#define TESTDLL_EXPORTIMPORT __declspec(dllexport)
#else
#define TESTDLL_EXPORTIMPORT __declspec(dllimport)
#endif

// ...
bool TESTDLL_EXPORTIMPORT  test(int n, char * input);
// ...

#ifdef __cplusplus
}
#endif

#endif // HEADER_H

Open in new window


for project testdll you would provide the macro TESTDLL_BUILD in the project settings or in makefile. for other c/c++ projects the above header can included and would switch the specifier to import.

for c# projects the macros (and the header) have no meaning.

Sara
0
 

Author Closing Comment

by:dominicwong
ID: 39643145
Thank you to all of you for the help and direction.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now