Click here to Skip to main content
13,259,771 members (55,173 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hello,

I created one Mono application to call C method from a shared library in Linux. My code is listed below

using System;
using System.Runtime.InteropServices;
 

namespace testConsole
{
    class MainClass
    {
        [DllImport("libTest.so",CharSet=CharSet.Auto,CallingConvention =CallingConvention.Cdecl, EntryPoint="testMethod",SetLastError=true ) ]
        public static extern void testMethod();
 
        public static void Main (string[] args)
        {
            Console.WriteLine ("Hello World!");
            testMethod();
 
            Console.WriteLine ("Hello World!");
        }
    }
}



and my shared library code like this

#ifndef SFLOW_H
#define SFLOW_H 1
 
#if defined(__cplusplus)
extern "C" {
#endif
 
void testMethod()
{
 
}
 
extern void testMethod1()
{
printf("This from C");
}
 
#if defined(__cplusplus)
}  /* extern "C" */
#endif
 
#endif /* SFLOW_H */



And I run the application I can load the library to C# using DllImport. But unable to call its method.How to call that testMethod() function from my mono application ?

Please help me...



The error like this

Hello World!

Unhandled Exception: System.EntryPointNotFoundException: testMethod
at (wrapper managed-to-native) testConsole.MainClass:testMethod ()
at testConsole.MainClass.Main (System.String[] args) [0x0000a] in /home/arun/Projects/testConsole/testConsole/Main.cs:15
[ERROR] FATAL UNHANDLED EXCEPTION: System.EntryPointNotFoundException: testMethod
at (wrapper managed-to-native) testConsole.MainClass:testMethod ()
at testConsole.MainClass.Main (System.String[] args) [0x0000a] in /home/arun/Projects/testConsole/testConsole/Main.cs:15

Press any key to continue...
Posted 17-Jun-12 22:03pm
Updated 15-Mar-17 0:12am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I'm so sorry, I did not notice that your question was tagged with Linux.

This is the closest thing I can find for you: http://stackoverflow.com/questions/2345657/p-invoke-and-mono-entrypointnotfoundexception[^]

Try this:

__declspec(dllexport) void testMethod()
{
 
}


See (read carefully) the recent article: P/Invoke Tutorial: Basics (Part 1)[^]

Read this to understand __declspec(dllexport) see this conversation: http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/47808016-c624-4c99-8250-06a138b11c06[^]

For additional info on __declspec(dllexport): http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx[^]

Additional note: Since you are explicit about the calling convention in C# (CallingConvention =CallingConvention.Cdecl), its better to use the same style in C too. So this would be better:

__declspec(dllexport) void __cdecl testMethod()
{
 
}
  Permalink  
v2
Comments
sunilkumarsomani 15-Mar-17 5:12am
   
For same logic i am able to include .so file of c project and can call its functions by c#. But if i use .so file of c++ project into c# then its giving entry point error. please update me.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

For same logic i am able to include .so file of c project and can call its functions by c#. But if i use .so file of c++ project into c# then its giving entry point error. please update me.
  Permalink  
Comments
CHill60 15-Mar-17 6:05am
   
If you have a question of your own then use the  Ask a Question  link. Do not post questions as solutions to old posts. You will need to include more detail than you have posted here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 15 Mar 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100