Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-07
4
Medium Priority
?
1,559 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 1000 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 35

Assisted Solution

by:sarabande
sarabande earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

885 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