Click to See Complete Forum and Search --> : How do you use a "C" dll from within a C# app?


TBlob
March 7th, 2003, 10:19 AM
Does anyone know how I can use a dll written in "C" from within a C# application? I have gotten as far as building a C# app and then importing the dll. However, when I call the function, I get an error saying that the dll cannot be loaded.:

// sample code follows

[DllImport("MyDll.dll")]
public static extern int DllFunction();
:
:

public static int Main()
{
DllFunction(); // I get a message that the dll cannot be loaded
}

Thanks for any help (and sample code)!!
-Tom

pareshgh
March 7th, 2003, 12:43 PM
plz. check that dll is in path !!!

Paresh

TBlob
March 7th, 2003, 01:05 PM
Yes, it's in the path..
:(

pareshgh
March 7th, 2003, 01:51 PM
does it have functions as exported !!! in .def file. which type of dll is that.

is it a COM dll or a simple MFC/NONMFC dll.

TBlob
March 7th, 2003, 03:48 PM
It's a non-MFC dll (since it was written in "C")..

pareshgh
March 7th, 2003, 03:54 PM
i think it should have the function exported list, make a NON-MFC dll in VC++ which exposes the function and then compile it to dll.

maybe pure C dll is conflicting one.

Paresh

MartinL
March 8th, 2003, 05:59 AM
If DLL cannot be loaded then dll is not a valid DLL or it is not in current directory or any directory listed in path env. variable...

Try to write whole path to the dll file in DllImport attribute.
If the prog fails again then try to make C/C++ prog and use LoadLibrary API function directly to load that DLL.

If the LoadLibrary loads the DLL successfully, post it here. We will look for problem again.

If the LoadLibrary fails then post here the error code returned by GetLastError() function call...

martin

TBlob
March 8th, 2003, 08:00 AM
Thanks, everyone.
I'll let you know what I find out.
Tom

pareshgh
March 10th, 2003, 03:51 PM
sure will wait for your answer.

thankx
Paresh