|
A few words on integrating unmanaged code into your Visual Basic .NET or C#
application.
If you have C/C++ code you like to include in your project without having to
rewrite the code or if you have header files and lib files intended for C/C++,
Managed C++ may be what you need. With Managed C++ you can easily write a
wrapper class in managed code, which calls out to unmanaged code.
I have provided a small sample here.
The project is called vdSID and the single class it exposes is simply called
Lookup.
I also provide a small Visual Basic .NET project to demonstrate its usage.
Lookup has the following static methods (in C# syntax):
static int LookupUserGroupFromRid( string TargetComputer, int Rid,
[out] string Domain, [out] string Name )
static int LookupAliasFromRid( string TargetComputer, int Rid, [out] string
Domain, [out] string Name )
Use these to retrieve the localized name of a group or alias,
respectively. Well-known RIDs can be found in the testSID Visual Basic .NET
sample or in the SID.cpp file.
static void sample_asmInc( ref int pVal )
static void sample_asmInc( int *pVal )
Example methods showing how to use assembly language in your .NET project.
The passed parameter will be incremented by one. Of course, there will be a
tremendous overhead involved here, so don't expect it to be fast. If you on
the other hand would like to, say create a greay-scale image from a color
photo, this technique could actually prove quite useful. The second overloaded
version of the method can be used when calling from a piece of unsafe
C# code and should be quite faster than the first one (but cannot be used from
Visual Basic .NET).
static void string[] sample_stringSplit( string strToSplit )
Example method showing how to return an array of managed objects (Strings).
Pass in a string and get back an array of strings, one element for each
character in the passed string. The same teqchnique will work for your own
managed classes as well (just declare them with "__gc" and you're on track).
Sorry for not providing any instance methods - I just ran out of imagination!
Here is a quick-start on how to add some old C code to your .NET project:
- Create a C++ .NET Class Library.
- Change the class name to something better than Class1 (no, Class2 is not
better).
- #include <windows.h> in Stdafx.h. This advice is not a performance issue
(ten years ago precompiled headers had a huge impact on compile times, but
today you won’t even notice the difference). I’ve run into problems by having
it placed in other locations. #include <vcclr.h> in your managed class'
implementation (.cpp).
- Add a generic C++ class. This is your unmanaged class.
- Add your C code to the unmanaged class' implementation and write interface
declarations for it in the header file (.h).
- Write the interface you want to expose to your .NET application in the
managed class’ header file. To declare out and reference parameters you need
“using namespace System::Runtime::InteropServices;” in order to acquire the
[In] and [Out] attributes.
- In the managed class’ implementation file, write the code you need to
bridge the gap between the managed types and the native C types. Most notably
it is strings that differ. Remember that in C++ you need to explicitly work
with object pointers. Therefore, a reference to a “String” will become
“String**” – not a one but a two level indirection. The casual C++ programmer
may easily forget this. In this file you'll also include any header file you
need to get the work done (if you're about to write a wrapper of a third-party
API, for example).
- You can use any .LIB-files needed under project properties, Linker, Input,
Additional Dependencies. vdSID uses netapi32.lib.
- Now you’re on your own with MSDN as your trustworthy companion.
Happy Programming!
|
|