3

I am trying a simple program to access a C function through a dll into a C# program,

class DllImportTest
{
    [DllImport("TestApp.dll", EntryPoint = "main1")]
    public static extern void main1();
}

class Program
{
    static void Main(string[] args)
    {
        DllImportTest.main1() ;
    }

I have seen through the code and the name of the function is the exactly right. I have also tried using Extern "C" but, it throws me an error as its .C file. I have placed the .Dll in the C# executable folder. Is there something that I am doing wrong here?

Yashodhar.Rao
  • 107
  • 3
  • 11
  • Possible dupe of http://stackoverflow.com/questions/653178/unable-to-find-an-entry-point-named-function-in-dll-c-to-c-sharp-type-con – s.m. Jul 06 '12 at 06:01
  • How about calling the same method from a native C client to test whether it is a C#/.NET issue or the library is just not valid. – Wiktor Zychla Jul 06 '12 at 06:03
  • I was able to create a .lib for the same and run it through a C console app, and this worked file. – Yashodhar.Rao Jul 06 '12 at 06:21
  • 2
    Make sure that the library contains this function: `dumpbin.exe /exports TestApp.dll`. You will see a list of exported functions. If you don't see `main1` in this list then there's a problem with your library. – Darin Dimitrov Jul 06 '12 at 06:25
  • 1
    Check out following two links, http://www.nag.co.uk/IndustryArticles/Calling_C_Library_DLLs_from_C_sharp.pdf http://www.codeproject.com/Articles/6912/Calling-methods-from-Dll-compiled-in-C-from-C – Siddiqui Jul 06 '12 at 06:28
  • @Darin File Type: DLL Summary 1000 .data 1000 .idata 2000 .rdata 1000 .reloc 1000 .rsrc 4000 .text 10000 .textbss this is what I got from the dumpbin.exe – Yashodhar.Rao Jul 06 '12 at 06:42
  • So no `main1`? It seems your library doesn't contain such exported function. That's not surprising because that's the error you get when you try to invoke it. – Darin Dimitrov Jul 06 '12 at 06:43
  • the following is what I used to create the Library in visual studio 2010 : File -> New Project -> create source file -> write code -> solution properties -> config properties-> configuration type = dynamic library -> build the code and copy the .dll , Have I done something wrong??? or have I missed out anything?? – Yashodhar.Rao Jul 06 '12 at 06:48
  • File Type: LIBRARY Summary 8A4 .debug$S 64 .debug$T 41 .drectve E .rdata 4 .rtc$IMZ 4 .rtc$TMZ 49 .text This is the .lib dump that was generated, although it doesn't have main1(), it executes perfectly with a C console app. – Yashodhar.Rao Jul 06 '12 at 06:55

1 Answers1

3

Found it! I had to use Extern "C" coupled with __declspec(dllexport) . I had never used both together, Thanks guys

Yashodhar.Rao
  • 107
  • 3
  • 11