Solved

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

Posted on 2013-11-07
4
1,354 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 33

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey!! 5 33
Printing 1 50
Adding Existing Item to vb.net Project causes Errors 4 31
Send SMS from VB via Sierra Wireless Modem 2 21
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…
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 …
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 opening and reading files in the C programming language.

813 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

15 Experts available now in Live!

Get 1:1 Help Now