kawing0510

Dear everyone,

I have written a managed C++, and then used it to C#.

I have written the code like this
=======================================
[DllImport("ManagedFour.dll", EntryPoint = "Test")]
public static extern void Testing();
=======================================

in order to call the Test in the dll.

However, there is an error like this:
=========================================================
Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry
point named Test in DLL ManagedFour.dll.
at CSharp.TempObj.Testing()
at CSharp.TempObj.func() in c:\vsprojects\managedc++\csharp\class1.cs:line 17
at CSharp.Class1.Main(String[] args) in c:\vsprojects\managedc++\csharp\class
1.cs:line 68
===========================================================
What is the problem How to fix it Thank you.



Re: Visual C# Express Edition How to use DllImport and Fix the error ?

nobugz

Make sure the C++ function is declared with "extern 'C'". If it is, try EntryPoint="_Test" (note the underscore).





Re: Visual C# Express Edition How to use DllImport and Fix the error ?

kawing0510

Thank you.

Could you give me an example for your explanation Thank you.





Re: Visual C# Express Edition How to use DllImport and Fix the error ?

nobugz

Let's do that the other way around; show us your declaration of the C++ function.





Re: Visual C# Express Edition How to use DllImport and Fix the error ?

kawing0510

Thank you for your reply. I will show you my code...

Thank you very very much...





Re: Visual C# Express Edition How to use DllImport and Fix the error ?

nobugz

Your C++ code you listed doesn't have a function named "Test"...





Re: Visual C# Express Edition How to use DllImport and Fix the error ?

kawing0510

Sorry, I don't know why it could not post all the codes out, I try again...

Thank you for your reply. I will show you my code...

The managed C++ code (ManagedFour.h):
#define DibPaletteSize(lpbi)    (DibNumColors(lpbi) * sizeof(RGBQUAD))
      //for usage of BSTR


using namespace System;


namespace ManagedC3
{
 
 public __gc class imgcap
 {
 private:
  char* dir;
 public:
  imgcap(){ dir = new char[200];}
  void FreeMediaType(AM_MEDIA_TYPE& mt);
  HBITMAP  GrabFrameFromMovie(char* filename);
  void  Test();
 };

}

==============================================
Managed C++ cpp file (ManagedFour.cpp):

void imgcap::Test(){

   printf("This is a test function called successfully");

}

 

==============================================

The C# codes that use ManagedFour.dll that generated from the code above:

using System;
using ManagedC3;
using System.Drawing;
using System.Runtime.InteropServices;
namespace CSharp
{
 class TempObj
 {

  [DllImport("C:\\VSProjects\\ManagedC++\\CSharp\\bin\\Debug\\ManagedFour.dll", EntryPoint="Test")]
  public static extern void Testing();

  public unsafe void func()
  {
        Testing();
    }
 }

 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 class Class1
 {
  [DllImport("ManagedFour.dll")]
  public static extern IntPtr GrabFrameFromMovie([MarshalAs(UnmanagedType.BStr)] string filename);

   /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
    TempObj o = new TempObj();
   o.func();

   //
   // TODO: Add code to start application here
   //
  }
 }
}


=============================================

Thank you very very much...





Re: Visual C# Express Edition How to use DllImport and Fix the error ?

nobugz

Okay, there's the problem. Your Test() function is a method of a managed class. You shouldn't use DllImport to declare the function. Instead, add a reference to the DLL with Project + Add reference. Then use code similar to this:

ManagedC3.imgcap grabber = new ManagedC3.imgcap();
grabber.Test();






Re: Visual C# Express Edition How to use DllImport and Fix the error ?

kawing0510

Thank you.

How if Test function in managed C++ needs a BSTR parameter eg.
in managed class:
void imgcap::Test(BSTR str){...}

and in C#, I could not pass string variable to the Test function in managed C++. In other words, how could I pass parameter to managed function that need BSTR as a parameter

Thank you.





Re: Visual C# Express Edition How to use DllImport and Fix the error ?

nobugz

You can't use BSTR as an argument type for a managed function, you'll need to use System::String. If you need to call an unmanaged function that takes a BSTR, use the Marshal::StringToBSTR() method to convert String to BSTR.