53562

C# Error Finding Method in DLLImport

Question:

I have a C++ assembly that I am importing using DLLImport.

I am attempting to call its method:

namespace Testing { class Test{ int Run(char* filePath, bool bEntry, double duration){//code} }; }

by

[DllImport(dllName, CharSet = CharSet.Auto)] public static extern int Run(string filePath, bool bEntry, double duration) );

When I call its method, I get the error message:

<strong>Unable to find an entry point named Run in dll</strong>

Answer1:

The "Run" looks to be a non-static class method. Although, it's possible to call such methods from C# this is not the primary use-case. It would be way easier to consume it from .NET if you expose it via COM, or at-least as a plain C interface:

extern "C" __declspec(dllexport) void* Testing_Test_Create(); extern "C" __declspec(dllexport) void Testing_Test_Destroy(void* self); extern "C" __declspec(dllexport) int Testing_Test_Run(void* self, char* filePath, bool bEntry, double duration);

And here is a sample how to call C++ class methods from C#:

// Test.cpp in NativeLib.dll namespace Testing { class __declspec(dllexport) Test { public: explicit Test(int data) : data(data) { } int Run(char const * path) { return this->data + strlen(path); } private: int data; }; } // Program.cs in CSharpClient.exe class Program { [DllImport( "NativeLib.dll", EntryPoint = "??0Test@Testing@@QAE@H@Z", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Ansi)] public static extern void TestingTestCtor(IntPtr self, int data); [DllImport( "NativeLib.dll", EntryPoint = "?Run@Test@Testing@@QAEHPBD@Z", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Ansi)] public static extern int TestingTestRun(IntPtr self, string path); static void Main(string[] args) { var test = Marshal.AllocCoTaskMem(4); TestingTestCtor(test, 10); var result = TestingTestRun(test, "path"); Console.WriteLine(result); Marshal.FreeCoTaskMem(test); } }

Entry point names might be different for your build configuration/compiler, so use dumpbin utility to obtain them. Again, this is just a proof of concept, in real code it would be better to use COM.

Answer2:

See here: <a href="http://dotnetperls.com/dllimport" rel="nofollow">http://dotnetperls.com/dllimport</a>

Answer3:

I'm not sure this will help if the function is a member of a class, but to locate the entry point by name, not ordinal, you'll need a .def file in your dll..

LIBRARY mylib Run @1

Recommend

  • Use map() for functions that does not return a value
  • Camel: Bean Proxy to CXF Endpoint
  • How to pass a custom object in REST webservice
  • Exception handling as per java coding standards
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • AlertDialog style when using setView()
  • Available space left on drive - WinAPI - Windows CE
  • Translating C# to PowerShell in InterIMAP
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Unable to install Git-core+svn by MacPorts
  • Unable to decode certificate at client new X509Certificate2()
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Django simple Captcha “No module named fields” error
  • Very simple C++ DLL that can be called from .net
  • Why querying a date BC is changed to AD in Java?
  • Why does access(2) check for real and not effective UID?
  • Database structure design with variable amounts of fields
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • NHibernate Validation Localization with S#arp Architecture
  • ilmerge with a PFX file
  • How can I send an e-mail from a vbs script
  • Launch Runnable Jar from Web Start
  • swift auto completion not working in Xcode6-Beta
  • Accessing IRQ description array within a module and displaying action names
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • Cannot Parse HTML Data Using Android / JSOUP
  • C# - Getting references of reference
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy
  • Recursive/Hierarchical Query Using Postgres