We help IT Professionals succeed at work.

How do I P/Invoke a C file?

GoldenBear
GoldenBear used Ask the Experts™
on
Hi,

I am trying to P/Invoke the following C file:
MBF2IEEE.DLL
http://support.microsoft.com/kb/113439

When I try to add a reference, I get the error:
A reference to the MBF2IEE.DLL could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

Is it possible to P/Invoke a C file? I've never actually use P/Invoke before, and I don't really know C/C++.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
hi

try the infor on this page. in particular the table for the data types.


http://www.codeproject.com/KB/cs/essentialpinvoke.aspx
Why do you want to add a reference to the dll. Its not a .NET assembly, but a simple win32 dll. References can not be added for win32 dlls.
Something like this should do
 

using System;
using System.Runtime.InteropServices;

class PlatformInvoke
{
    [DllImport("MBF2IEEE.dll")]
    public static extern Single Cvs (String c);
    [DllImport("MBF2IEEE.dll")]
    public static extern Double Cvd (String c);

    [DllImport("MBF2IEEE.dll")]
    internal static extern String Mks (Single s);
    [DllImport("MBF2IEEE.dll")]
    internal static extern String Mkd (Double s);

    public static void Main() 
    {
        Double d = Mkd ("22.22");
        String s = Cvd ( s );
    }
}

Open in new window

and the dll must be put in the same folder as the executable.

Author

Commented:
This looks like it will work - thanks so much for the sample code!

Author

Commented:
@ambience

Sorry 1 more question.

I'm getting an error message: "Unable to load DLL 'MBF2IEEE.dll'.

I put the DLL in the project folder.

Is this the correct folder? Do I need to take other steps?

Author

Commented:
@ambience

Never mind about my previous question, I just made the path explicit.