Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fill Null values 5 28
SSIS On fail action 5 38
Need syntax help building hyperlink 2 27
Get month and date in a format 4 35
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

820 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