How To Use a C Libray in your C# Program
-
For an item of hardware I have written a Shared library to make it easy to use the API for this hardware. The library sets up data structures and handles, buffers etc for simple program use. For example to arrange data to be transmitted I call routings like:- UINT16 TransmitData( UINT unit, UINT addr, TXBUFFER * tx_buffer ); However in VS2008 I used C++ in a MFC dialog application, which all worked well. Having moved to VS2010, I now see that the intellisence does not work with this product. So my thoughts were to use C# for the Windows forms and link in the Shared Library. I have built the Shared Library in VS2010 so I need to understand how to access these function in a C# program. Is this using ‘Wrappers’ and how does the libraries API001.LIB and API001.h files link in? For a C++ MFC program I included the headers and LIB files with the PATH set up for the DLL’s. The libray is built using C. Many thanks, Andy
-
For an item of hardware I have written a Shared library to make it easy to use the API for this hardware. The library sets up data structures and handles, buffers etc for simple program use. For example to arrange data to be transmitted I call routings like:- UINT16 TransmitData( UINT unit, UINT addr, TXBUFFER * tx_buffer ); However in VS2008 I used C++ in a MFC dialog application, which all worked well. Having moved to VS2010, I now see that the intellisence does not work with this product. So my thoughts were to use C# for the Windows forms and link in the Shared Library. I have built the Shared Library in VS2010 so I need to understand how to access these function in a C# program. Is this using ‘Wrappers’ and how does the libraries API001.LIB and API001.h files link in? For a C++ MFC program I included the headers and LIB files with the PATH set up for the DLL’s. The libray is built using C. Many thanks, Andy
you would need a DLL file and P/Invoke technology; you can't use any .h files or .lib files. Maybe read this[^] first. Things are easiest when all arrays (or objects) get allocated by managed (C#) code, then passed to the native world; that works easier than the other way around! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
For an item of hardware I have written a Shared library to make it easy to use the API for this hardware. The library sets up data structures and handles, buffers etc for simple program use. For example to arrange data to be transmitted I call routings like:- UINT16 TransmitData( UINT unit, UINT addr, TXBUFFER * tx_buffer ); However in VS2008 I used C++ in a MFC dialog application, which all worked well. Having moved to VS2010, I now see that the intellisence does not work with this product. So my thoughts were to use C# for the Windows forms and link in the Shared Library. I have built the Shared Library in VS2010 so I need to understand how to access these function in a C# program. Is this using ‘Wrappers’ and how does the libraries API001.LIB and API001.h files link in? For a C++ MFC program I included the headers and LIB files with the PATH set up for the DLL’s. The libray is built using C. Many thanks, Andy
You can create a C++/CLI class library and include the .lib and .h there. Then create a managed class in C++ that wraps the functions. Finally, make a reference from the C# application to the C++/CLI project.