Tutorial wanted for using a managed C++ class wrapper 
Author Message
 Tutorial wanted for using a managed C++ class wrapper

The consensus of opinion in another thread was for me to use
a managed c++ class wrapper in conjunction with my C# code to
access a Win32 Dll made from a C library.

can anyone point me to an on-line tutorial on how to do this
with just .net and not Visual Studio .net please?

Many thanks and regards,

..matthew



Sun, 09 Jan 2005 18:58:22 GMT  
 Tutorial wanted for using a managed C++ class wrapper
You dont need an online tutorial. It's simplicity itself.

The methods of a class wrapper are static and extern so they have no
implementation.

You must include the line "using System.Runtime.InteropServices;"  at the
top of your code, plus any other namespaces that you might need.
InteropServices contains the DllImport attribute definitions.

The wrapper class is created as follows...

public class MyWrapper
{

// note that a "wrapper class" may use more than one DLL

    [DllImport("myFirstDll.dll"]// import the DLL
    public static extern void MyMethod(int Parameter);

    [DllImport("mySecondDll.dll"]// import the DLL
    public static extern void MyOtherMethod();

Quote:
}

Now you can access those methods from .net using...

MyWrapper.MyOtherMethod();

When passing parameters such as pointers or structures, you need to marshal
into and out of managed code. The following example shows how i use interop
to get access to the TrackMouseEvent method...

[DllImport("User32.dll")]

public static extern int TrackMouseEvent(

[MarshalAs(UnmanagedType.LPStruct)]

TRACKMOUSEEVENT lpEventTrack // tracking information

);

And here is the TRACKMOUSEEVENT structure that is passed to the method...

[StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]

public class TRACKMOUSEEVENT

{

public int cbSize=16;

public int dwFlags;

public IntPtr hwndTrack;

public int dwHoverTime=400;

Quote:
}

Notice that types you're used to in C++ can be different in .NET. For
example, a handle (which is a void pointer under the covers) is an IntPtr.
Basically, you need to find the .NET type that best fits the C++ type.

Hope this helps.

Bob.

--
Have you thought about buying my book?
http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

Looking for something else? Check out my .NET book recommendations.
http://www.bobpowell.net/recommendations.htm

Visit NetEdge Software to find out about the Web Service Enabler.
Enable all your legacy C++ and VB6 applications to seamlessly connect to
.NET and Apache Axis web-services.
http://www.netedgesoftware.com


Quote:
> The consensus of opinion in another thread was for me to use
> a managed c++ class wrapper in conjunction with my C# code to
> access a Win32 Dll made from a C library.

> can anyone point me to an on-line tutorial on how to do this
> with just .net and not Visual Studio .net please?

> Many thanks and regards,

> ..matthew



Sun, 09 Jan 2005 20:15:20 GMT  
 Tutorial wanted for using a managed C++ class wrapper
Bob,

I think you didn't understand the asked question very well! Matthew asked
for a tutorial to use a managed c++ class wrapper.

Where did you use Managed C++ in your example?

Maybe someone else can give good answer to Matthew's question?

Ferdinand.


Quote:
> You dont need an online tutorial. It's simplicity itself.

> The methods of a class wrapper are static and extern so they have no
> implementation.

> You must include the line "using System.Runtime.InteropServices;"  at the
> top of your code, plus any other namespaces that you might need.
> InteropServices contains the DllImport attribute definitions.

> The wrapper class is created as follows...

> public class MyWrapper
> {

> // note that a "wrapper class" may use more than one DLL

>     [DllImport("myFirstDll.dll"]// import the DLL
>     public static extern void MyMethod(int Parameter);

>     [DllImport("mySecondDll.dll"]// import the DLL
>     public static extern void MyOtherMethod();

> }

> Now you can access those methods from .net using...

> MyWrapper.MyOtherMethod();

> When passing parameters such as pointers or structures, you need to
marshal
> into and out of managed code. The following example shows how i use
interop
> to get access to the TrackMouseEvent method...

> [DllImport("User32.dll")]

> public static extern int TrackMouseEvent(

> [MarshalAs(UnmanagedType.LPStruct)]

> TRACKMOUSEEVENT lpEventTrack // tracking information

> );

> And here is the TRACKMOUSEEVENT structure that is passed to the method...

> [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]

> public class TRACKMOUSEEVENT

> {

> public int cbSize=16;

> public int dwFlags;

> public IntPtr hwndTrack;

> public int dwHoverTime=400;

> }

> Notice that types you're used to in C++ can be different in .NET. For
> example, a handle (which is a void pointer under the covers) is an IntPtr.
> Basically, you need to find the .NET type that best fits the C++ type.

> Hope this helps.

> Bob.

> --
> Have you thought about buying my book?
> http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

> Looking for something else? Check out my .NET book recommendations.
> http://www.bobpowell.net/recommendations.htm

> Visit NetEdge Software to find out about the Web Service Enabler.
> Enable all your legacy C++ and VB6 applications to seamlessly connect to
> .NET and Apache Axis web-services.
> http://www.netedgesoftware.com



> > The consensus of opinion in another thread was for me to use
> > a managed c++ class wrapper in conjunction with my C# code to
> > access a Win32 Dll made from a C library.

> > can anyone point me to an on-line tutorial on how to do this
> > with just .net and not Visual Studio .net please?

> > Many thanks and regards,

> > ..matthew



Sun, 09 Jan 2005 21:11:12 GMT  
 Tutorial wanted for using a managed C++ class wrapper
Thanks Bob.

Maybe I don't want a wrapper class then if you are sure all the methods are
static?

Part of the reason I'm building this is that I need thread safety. What I
have is a C library which I've compiled into a Win32 dll. What I want to do
is write a C++ class in managed .net code that uses data types (structs)
from the C library and has some methods that each call several functions in
the Dll with instances of the -already mentioned- data types. Then from my
C# code I will create instances of the C++ class, actually from threads,
and this will allow thread safety.

Can this be done?

Thanks and regards,

..matthew

Quote:

> You dont need an online tutorial. It's simplicity itself.

> The methods of a class wrapper are static and extern so they have no
> implementation.

> You must include the line "using System.Runtime.InteropServices;"  at the
> top of your code, plus any other namespaces that you might need.
> InteropServices contains the DllImport attribute definitions.

> The wrapper class is created as follows...

> public class MyWrapper
> {

> // note that a "wrapper class" may use more than one DLL

>     [DllImport("myFirstDll.dll"]// import the DLL
>     public static extern void MyMethod(int Parameter);

>     [DllImport("mySecondDll.dll"]// import the DLL
>     public static extern void MyOtherMethod();

> }

> Now you can access those methods from .net using...

> MyWrapper.MyOtherMethod();

> When passing parameters such as pointers or structures, you need to marshal
> into and out of managed code. The following example shows how i use interop
> to get access to the TrackMouseEvent method...

> [DllImport("User32.dll")]

> public static extern int TrackMouseEvent(

> [MarshalAs(UnmanagedType.LPStruct)]

> TRACKMOUSEEVENT lpEventTrack // tracking information

> );

> And here is the TRACKMOUSEEVENT structure that is passed to the method...

> [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]

> public class TRACKMOUSEEVENT

> {

> public int cbSize=16;

> public int dwFlags;

> public IntPtr hwndTrack;

> public int dwHoverTime=400;

> }

> Notice that types you're used to in C++ can be different in .NET. For
> example, a handle (which is a void pointer under the covers) is an IntPtr.
> Basically, you need to find the .NET type that best fits the C++ type.

> Hope this helps.

> Bob.

> --
> Have you thought about buying my book?
> http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

> Looking for something else? Check out my .NET book recommendations.
> http://www.bobpowell.net/recommendations.htm

> Visit NetEdge Software to find out about the Web Service Enabler.
> Enable all your legacy C++ and VB6 applications to seamlessly connect to
> .NET and Apache Axis web-services.
> http://www.netedgesoftware.com



> > The consensus of opinion in another thread was for me to use
> > a managed c++ class wrapper in conjunction with my C# code to
> > access a Win32 Dll made from a C library.

> > can anyone point me to an on-line tutorial on how to do this
> > with just .net and not Visual Studio .net please?

> > Many thanks and regards,

> > ..matthew



Sun, 09 Jan 2005 21:19:34 GMT  
 Tutorial wanted for using a managed C++ class wrapper
ahh, silly old me. I just assumed C# for some reason I thought this was a C#
language group.

see my thread "HOW TO use a newsgroup" and the fun debate we're having on
there.


Quote:
> Bob,

> I think you didn't understand the asked question very well! Matthew asked
> for a tutorial to use a managed c++ class wrapper.

> Where did you use Managed C++ in your example?

> Maybe someone else can give good answer to Matthew's question?

> Ferdinand.



> > You dont need an online tutorial. It's simplicity itself.

> > The methods of a class wrapper are static and extern so they have no
> > implementation.

> > You must include the line "using System.Runtime.InteropServices;"  at
the
> > top of your code, plus any other namespaces that you might need.
> > InteropServices contains the DllImport attribute definitions.

> > The wrapper class is created as follows...

> > public class MyWrapper
> > {

> > // note that a "wrapper class" may use more than one DLL

> >     [DllImport("myFirstDll.dll"]// import the DLL
> >     public static extern void MyMethod(int Parameter);

> >     [DllImport("mySecondDll.dll"]// import the DLL
> >     public static extern void MyOtherMethod();

> > }

> > Now you can access those methods from .net using...

> > MyWrapper.MyOtherMethod();

> > When passing parameters such as pointers or structures, you need to
> marshal
> > into and out of managed code. The following example shows how i use
> interop
> > to get access to the TrackMouseEvent method...

> > [DllImport("User32.dll")]

> > public static extern int TrackMouseEvent(

> > [MarshalAs(UnmanagedType.LPStruct)]

> > TRACKMOUSEEVENT lpEventTrack // tracking information

> > );

> > And here is the TRACKMOUSEEVENT structure that is passed to the
method...

> > [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]

> > public class TRACKMOUSEEVENT

> > {

> > public int cbSize=16;

> > public int dwFlags;

> > public IntPtr hwndTrack;

> > public int dwHoverTime=400;

> > }

> > Notice that types you're used to in C++ can be different in .NET. For
> > example, a handle (which is a void pointer under the covers) is an
IntPtr.
> > Basically, you need to find the .NET type that best fits the C++ type.

> > Hope this helps.

> > Bob.

> > --
> > Have you thought about buying my book?
> > http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

> > Looking for something else? Check out my .NET book recommendations.
> > http://www.bobpowell.net/recommendations.htm

> > Visit NetEdge Software to find out about the Web Service Enabler.
> > Enable all your legacy C++ and VB6 applications to seamlessly connect to
> > .NET and Apache Axis web-services.
> > http://www.netedgesoftware.com



> > > The consensus of opinion in another thread was for me to use
> > > a managed c++ class wrapper in conjunction with my C# code to
> > > access a Win32 Dll made from a C library.

> > > can anyone point me to an on-line tutorial on how to do this
> > > with just .net and not Visual Studio .net please?

> > > Many thanks and regards,

> > > ..matthew



Mon, 10 Jan 2005 01:16:49 GMT  
 Tutorial wanted for using a managed C++ class wrapper
Well, the methods that make the calls are static members of the class
wrapper. The system is used mainly to ensure that data types are marshalled
in and out of the managed world correctly and to allow you to make the calls
to the DLL without having an instance of the wrapper class anywhere because
one would be extraneous.

Static methods of a C# class are all thead-safe. Whether the methods in the
DLL called by the wrapper class are thread safe, only you can say.

Bob.


Quote:
> Thanks Bob.

> Maybe I don't want a wrapper class then if you are sure all the methods
are
> static?

> Part of the reason I'm building this is that I need thread safety. What I
> have is a C library which I've compiled into a Win32 dll. What I want to
do
> is write a C++ class in managed .net code that uses data types (structs)
> from the C library and has some methods that each call several functions
in
> the Dll with instances of the -already mentioned- data types. Then from my
> C# code I will create instances of the C++ class, actually from threads,
> and this will allow thread safety.

> Can this be done?

> Thanks and regards,

> ..matthew


> > You dont need an online tutorial. It's simplicity itself.

> > The methods of a class wrapper are static and extern so they have no
> > implementation.

> > You must include the line "using System.Runtime.InteropServices;"  at
the
> > top of your code, plus any other namespaces that you might need.
> > InteropServices contains the DllImport attribute definitions.

> > The wrapper class is created as follows...

> > public class MyWrapper
> > {

> > // note that a "wrapper class" may use more than one DLL

> >     [DllImport("myFirstDll.dll"]// import the DLL
> >     public static extern void MyMethod(int Parameter);

> >     [DllImport("mySecondDll.dll"]// import the DLL
> >     public static extern void MyOtherMethod();

> > }

> > Now you can access those methods from .net using...

> > MyWrapper.MyOtherMethod();

> > When passing parameters such as pointers or structures, you need to
marshal
> > into and out of managed code. The following example shows how i use
interop
> > to get access to the TrackMouseEvent method...

> > [DllImport("User32.dll")]

> > public static extern int TrackMouseEvent(

> > [MarshalAs(UnmanagedType.LPStruct)]

> > TRACKMOUSEEVENT lpEventTrack // tracking information

> > );

> > And here is the TRACKMOUSEEVENT structure that is passed to the
method...

> > [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]

> > public class TRACKMOUSEEVENT

> > {

> > public int cbSize=16;

> > public int dwFlags;

> > public IntPtr hwndTrack;

> > public int dwHoverTime=400;

> > }

> > Notice that types you're used to in C++ can be different in .NET. For
> > example, a handle (which is a void pointer under the covers) is an
IntPtr.
> > Basically, you need to find the .NET type that best fits the C++ type.

> > Hope this helps.

> > Bob.

> > --
> > Have you thought about buying my book?
> > http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

> > Looking for something else? Check out my .NET book recommendations.
> > http://www.bobpowell.net/recommendations.htm

> > Visit NetEdge Software to find out about the Web Service Enabler.
> > Enable all your legacy C++ and VB6 applications to seamlessly connect to
> > .NET and Apache Axis web-services.
> > http://www.netedgesoftware.com



> > > The consensus of opinion in another thread was for me to use
> > > a managed c++ class wrapper in conjunction with my C# code to
> > > access a Win32 Dll made from a C library.

> > > can anyone point me to an on-line tutorial on how to do this
> > > with just .net and not Visual Studio .net please?

> > > Many thanks and regards,

> > > ..matthew



Mon, 10 Jan 2005 01:40:11 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

2. Managed C++ wrappers for unmanaged C++ classes

3. Referencing data from unmanaged code to managed code in C++ Wrapper class

4. Managed C++ "wrapper classes" deployment problem

5. Creating a managed C++ class wrapper

6. Making managed wrapper for legacy unmanaged classes.

7. MFC classes and 'managed' wrappers

8. Using Managed Objects inside C++ Classes

9. using managed C++ class

10. Interface of Managed C++ wrappers

11. Managed C++ Wrapper

12. Managed C++ Wrapper for DLL

 

 
Powered by phpBB® Forum Software