0

Long time reader, first time poster. One day I hope to be answering questions on here...

So it's kind of similar to: "Unable to find an entry point named [function] in dll" (c++ to c# type conversion)

But I can't seem to apply the same solution...

Basically, I wrote a new Method:

Defined in the header file of the C++ project as :

extern "C" {
     __declspec(dllexport) bool IsDataValid();
}

Defined in the source file of the C++ project as: (signiature only)

extern bool __cdecl IsDataValid() { 
//function stuf......... returns a bool
}

Imported into a forms C# application within the C# Project as:

[DllImport("CarChipSDK_C_Sharp.dll", EntryPoint = "IsDataValid")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsDataValid();

It is called from the same place within C# forms .cs file as:

bool isDataValid = IsDataValid();

It is returning an exception with the message:

"Unable to find an entry point 'IsDataValid()' named in DLL 'CarChipSDK_C_Sharp.dll'.

I have used dumpbin.exe and dependency walker on the .dll generated from the c++ code and it shows that it has the IsDataValid() entry point.

All help is much appreciated...

Problem Solved! Stupid me, this was the code from a previous co-op at my current company, turns out he was reading the .dll from the bin/release folder where as I was building to the bin/debug folder. Should have known. My sincere apologies.

Community
  • 1
  • 1
Bo Li
  • 141
  • 3
  • 10

1 Answers1

8

You are encountering C++ name mangling. Declare the C++ functions as extern "C". So, in your C++ module...

extern "C" __declspec(dllexport) bool IsDataValid();

You really don't need the entry point specification attribute either. Your C# declaration will be:

[DllImport("CarChipSDK_C_Sharp.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsDataValid();

For future reference, dumpbin.exe is a very useful program for analyzing problems like this. If you run it on your DLL you will see what those functions are actually named by the time they are compiled.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Thanks for the answer, I just got off work, so I'll try this on Monday. Looks promising! Thanks so much. – Bo Li Jan 15 '11 at 00:33
  • Dependency Walker shows the same information as dumpbin, in a nice tabular format, for those who prefer GUIs. – Ben Voigt Jan 15 '11 at 00:37
  • @mcbobo: No problem. I use interop a lot as I work in systems and constantly have to build GUI's around driver interfaces and the like. – Ed S. Jan 15 '11 at 00:51
  • Weird... I already apparently had it declared as ' extern "C" ' I put extern "C" { //lots of function headers... and __declspec(dllexport) bool IsDataValid(); } . I proceeded to check the .dll created with dumpbin and dependancy walker and they both show that I have IsDataValid as an entry point... Do you guys have any idea what else might be wrong? – Bo Li Jan 17 '11 at 16:14
  • Are you certain that you are loading the correct version of the DLL? – Ed S. Jan 17 '11 at 23:03
  • 1
    I have fixed it. Sorry about the fuss. It was a debug/release build version issue. Thank you so much. – Bo Li Jan 18 '11 at 19:25