Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Professional C# 4.0 and .NET 4
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional C# 4.0 and .NET 4
This is the forum to discuss the Wrox book Professional C# 4.0 and .NET 4 by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner; ISBN: 9780470502259
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 4.0 and .NET 4 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 30th, 2011, 04:34 PM
Registered User
 
Join Date: May 2011
Location: Texas
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calling Unmanaged code from Managed code

I have an unmanaged C++ DLL, I have the source to. I am trying to extend its functionality. I have successfully called existing methods in the DLL in a brute force way. I simply added the DLL as a project reference to my managed C# project and did a new on the desired class and called the methods.

As I have been studying up on this topic, I ran across the System.RunTime.Interop.Marshal set of static methods. Also, PInvoke. It is not clear to me when I should use one approach over another. I was wondering if someone here could tell me more about them. I am interested in knowing when should you use any of these approaches? when should you not?

My application is a managed wrapper around the unmanaged c++ DLL. The unmanaged DLL is not using COM or ATL. It is just plain C++. I am passing binary data. This data is transformed into another binary data using a complex algorithm and returned. Speed & multithreading is important, since it will be called by several threads.

Mike
  #2 (permalink)  
Old May 31st, 2011, 02:23 AM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 33%
Activity: 33% Activity: 33% Activity: 33%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Hi Mike,

As you've added the DLL as a project reference, created an instance and called the methods - are your sure not having a COM library?
Platform Invoke is used on unmanaged DLLs that export functions (see dumpbin /exports lib.dll).
The Marshal class has several helper methods for managed/unmanaged interop, both for DllImport and COM interop scenarios.

Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
  #3 (permalink)  
Old May 31st, 2011, 08:45 AM
Registered User
 
Join Date: May 2011
Location: Texas
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do you detect it is using COM or not

I'm not exactly sure how to tell if it uses COM or not. I know I didn't register the DLL or anything like that.

Within my managed C# project
1. I made a Project Reference to the unmanaged C++ project
2. Added a Using statement to that DLL
3. Then created an instance of the class
4. Accessed a method on the class

That is all I did. I did not use PInvoke, DllImport or anything like that.
  #4 (permalink)  
Old May 31st, 2011, 09:30 AM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 33%
Activity: 33% Activity: 33% Activity: 33%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

There's also a way to use COM without registration. However, you referenced the unmanaged C++ project from the C# project? So you have the C++ project opened within the same solution? If you build the C++ project with Visual Studio, it might also do the registration of the COM server.
Can you do a "tblimp comserver.dll"? In that case a type library is included with the unmanaged C++ DLL and it's definitely a COM server. The type library could also be in a different file from the DLL.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
  #5 (permalink)  
Old May 31st, 2011, 09:44 AM
Registered User
 
Join Date: May 2011
Location: Texas
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default DLL does not appear to be using COM

When I run tlbimp on my dll it returns
error Tl1002: The input file "my.dll" is not a valid type library.
  #6 (permalink)  
Old May 31st, 2011, 10:03 AM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 33%
Activity: 33% Activity: 33% Activity: 33%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

ok, the type library might still be outside of the DLL.
How does your C++ class look like?
Maybe
Code:
public ref class ClassName
{
};
In that case it's a managed version of C++: C++/CLR
Can you do ildasm on the my.dll?
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
  #7 (permalink)  
Old May 31st, 2011, 09:34 PM
Registered User
 
Join Date: May 2011
Location: Texas
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default ILDASM & declaration

ILDASM produces output that looks like a class view of my DLL.

The class that I access is declared just like your example:

Code:
public ref class Foo
{
};
So does that mean it is using COM or managed C++? I assumed it is not managed C++ because I cannot make references to managed objects.
  #8 (permalink)  
Old June 1st, 2011, 01:43 AM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 33%
Activity: 33% Activity: 33% Activity: 33%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

You can use this C++ class like a .NET class because it is a .NET class. You're not using COM.
The ref keyword declares a managed reference type with C++/CLR. With the C++ compiler settings you will see the /clr option enabled. public ref class Foo is a managed reference type (class in C#), public value class Foo is a managed value type (struct in C#).
How do you see that you cannot make references to managed objects with the C++ project? It should be possible to reference .NET assemblies with the C++ project and use the public types there without problems. Just the syntax to use these types is a little different to normal C++.
With the Pro C# book you can find mapping between C++/CLR and C# with one of the downloadable chapters.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IronPython and .Net Assemblies containing unmanaged code quant BOOK: Professional IronPython 1 March 11th, 2011 12:57 PM
Calling managed code from un-managed code David_0223 C# 7 August 21st, 2007 09:32 AM
Managed & Unmanaged Code? code_lover General .NET 0 January 4th, 2007 10:08 AM
Unmanaged to managed code blemos C# 0 December 21st, 2006 08:34 AM
Using XML Schemas in C++ Unmanaged Code RFickling XML 1 June 8th, 2005 08:23 AM



All times are GMT -4. The time now is 02:33 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.