Dave573673

Hi Everyone,

I'm a total noob with visual c# and high level code (my usual antics are writing assembler/firmware). I've tried to make sense of some of the previous threads on this topic, but just don't seem to understand. I have a piece of hardware that comes with a .dll, .lib, and .h file. These files were all written in Visual C++ 6.0. I'm wondering if someone could outline (in detail) exactly what I have to do to be able to call function xyz from this .dll in my visual c# application. For example, the software note tells me that the following function is readily found in the .dll:

CP210x_STATUS CP210x_GetNumDevices( LPDWORD NumDevices )

What code do I need to write in Visual C# before I can call (in visual c#) CP210x_GetNumDevices() Thanks a lot for your help.

Dave



Re: Common Language Runtime Visual C++ 6.0 dll/lib in Visual C# application

Dave

If it makes any difference, I'm using the .h/.lib/.dll found in this .zip file from Silicon Labs. Thanks,

Dave





Re: Common Language Runtime Visual C++ 6.0 dll/lib in Visual C# application

Dave

I've tried adding this code:

[DllImport("CP210xManufacturing.DLL", EntryPoint="CP210x_GetNumDevices", SetLastError=true,

CharSet=CharSet.Auto, ExactSpelling=true,

CallingConvention=CallingConvention.StdCall)]

CP210x_STATUS CP210x_GetNumDevices(LPDWORD lpdwNumDevices);

Into my Visual C# application, but I get 3 errors:

Error 1 The type or namespace name 'LPDWORD' could not be found (are you missing a using directive or an assembly reference )

Error 2 The type or namespace name 'CP210x_STATUS' could not be found (are you missing a using directive or an assembly reference )

Error 3 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference )

Any thoughts for me Thanks a lot...your time is greatly apprecaited.

Dave





Re: Common Language Runtime Visual C++ 6.0 dll/lib in Visual C# application

Adam Shipp

To be able to use C++ code in C# you need to add a reference to the C++ libraries. To do this right click on the solution name and select "Add Reference...". Then you have to add a "using" statement to the file similar to "#include" statement. The format for a using statement is:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

These just happen to be the ones that I have in the file that I am currently working with. You will have to add one that has the library that you are looking for. It should be something like: using Systme.the_library_you_added_reference_to;. There may be more needed after the library name. Just add a period after the name and VS will give you a list of available options. I hope that this helps






Re: Common Language Runtime Visual C++ 6.0 dll/lib in Visual C# application

Dave

Hi Adam,

Thanks a lot for your reply. This makes much more sense as I was trying to understand where the dll actually gets added to the project. For some reason when I try to use the Add Reference, I get the following error:

"A reference to 'C:\.....CP210xManufacturing.dll' could not be added. PLease make sure that the file is accessible, and that it is a valid assembly or COM component."

Any thoughts or ideas Thanks.

Dave





Re: Common Language Runtime Visual C++ 6.0 dll/lib in Visual C# application

Dave

I've made some snail progress. I've added the following to my application:

using System.Runtime.InteropServices;

[DllImport("CP210xManufacturing.DLL", EntryPoint = "CP210x_GetNumDevices", SetLastError = true,

CharSet = CharSet.Unicode, ExactSpelling = true,

CallingConvention = CallingConvention.StdCall)]

public static extern CP210x_STATUS CP210x_GetNumDevices(LPDWORD lpdwNumDevices);

The problem is now (of course) that my visual c# application knows nothing about the type CP210x_STATUS and LPDWORD. In addition to my .dll I have a corresponding .h and .lib. How can I go about "importing" the information contained in my .h file into my visual c# application such that it recognizes the various types in the .h file Thanks a lot,


Dave





Re: Common Language Runtime Visual C++ 6.0 dll/lib in Visual C# application

Ming.Chen

Dave, you don't "add reference" to a native DLL. As long as it's in the search path (the same folder as the application usually works fine), it should work.

As for the .h, .lib file, you need to look into the .h file to find out what's the definition for CP210x_STATUS and LPDWORD there, and then map them to the corresponding C# types in the DllImport statement (i.e. if first one is just typedef of int, and second one a pointer to int, you can write the dllimport asSmile

[DllImport("CP210xManufacturing.DLL", EntryPoint = "CP210x_GetNumDevices", SetLastError = true,

CharSet = CharSet.Unicode, ExactSpelling = true,

CallingConvention = CallingConvention.StdCall)]

public static extern int CP210x_GetNumDevices(ref int lpdwNumDevices);

It could get a little complicated when CP210x_STATUS is defined as a struct.