How to use a regular C DLL in a C# program using Visual C++

closed account (N85iE3v7)
Hi,

I designed a regular DLL using C on Visual Studio 2005. However I need to use it at a C# program. How do I do that? I tried to add the DLL as a reference in the C# project and then got error messages that " it could not be added and to be sure that is a valid assembly or COM object". I also copied the resulting DLL to the source directory where I will need to use this DLL and tried to use :

1
2
            [DllImport("ReadMagStripe.dll")];
            public static extern void Cancel(void);// that is my C function 


But it still does not work. I will simply wrap my functions on a C# class but I need to use those C functions from my DLL. How do I get started? Thanks for the help!
Last edited on
closed account (N85iE3v7)
I actually made it work by using calls like:

1
2
3
4
5
6
7
[DllImport("mydll.dll")]
 public static extern int Myfunc( 
                                            [MarshalAs(UnmanagedType.LPTStr)] string lpDeviceName,
                                            IntPtr hpDevice);

// this was in C declared as
DWORD WINAPI MyFunc  (LPSTR   lpDeviceName, HANDLE *hpDevice);


What is not working now is how to convert this HANDLE to something that .NET can understand as it is declared as:

 
typedef void *HANDLE;


Topic archived. No new replies allowed.