Solved

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

Posted on 2013-11-07
4
1,414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

739 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