다음을 통해 공유


Designing .NET Class Libraries: Understanding Interoperability (March 23, 2005)

Posted: March 28, 2005

Please note: Portions of this transcript have been edited for clarity

Introduction

frankred [MS] (Moderator):
Hello & welcome to today’s Designing .NET Class Libraries chat on Understanding Interoperability.
Now, let’s introduce our knowledgeable experts...

Joe Duffy [MS] (Expert):
Hey everyone--my name's Joe Duffy, and I'm a Program Manager on the CLR team. Looking forward to the chat...

Start of Chat

SonjaKe (Expert):
Q: Do an application can have different langauges classes working together? If yes than how it is possible...
A: Yes, but they have to live in different assemblies. Since everything is compiled to the same IL regardless of source language you use, they can work together.

Joe Duffy [MS] (Expert):
Q: Will the guidelines given in the presentation be incorporated into the official MS library design guidelines?
A: Yes, much (most) of this is already incorporated into the Design Guidelines. Unfortunately, we only publicly release our revisions in line with public releases of the Framework. So you'll see a whole new set of these go live when Whidbey (v2.0) ships. Watch MSDN and our blogs (in particular blogs.msdn.com/kcwalina & blogs.msdn.com/brada) for details.

Jessek [MS] (Expert):
It's possible to use different languages in the same assembly as well. See: https://blogs.msdn.com/junfeng/archive/2004/07/15/183813.aspx

SonjaKe (Expert):
Hi, I'm Sonja Keserovic, CLR PM. I used to work on COM interop and now I'm working on JIT. :-)

SonjaKe (Expert):
Q: why is needed that vb net create an interop.mycomponent.dll to work with COM DLL ?
A: Interop assembly contains managed descriptions of COM classes that runtime can understand and place the calls correctly. It's like a bridge between managed and native world. If you look at it using ILDASM you'll see that there is almost no code there, just managed definitions of all classes from COM dll.

Jessek [MS] (Expert):
Q: I am an MC++ newbie, i have a very basic question. Can i use fstream to read a binary file into a memory block from within Managed C++ Class method ?
A: Yes, you can use the same native libraries in MC++ as you can in native c++.

SonjaKe (Expert):
Q: Does anyone (from MS) know if it is possible to create a C++ managed MMC snapin?
A: I never tried but I don't see any reason why that wouldn't be possible. Managed C++ dll can expose any COM interfaces required for MMC snapins.

SonjaKe (Expert):
Q: Is there a general recommendation on the amount of a library that should be exposed for interop? If I find a large percentage of my library is providing COM interop, is this a sign of a architectural problem?
A: I think that depends on your clients. If you know you'll have a lot of COM clients then exposing a lot of interop shouldn't be problematic. If you don't know of any, you might want to minimize exposure and add more if needs arise.

SonjaKe (Expert):
Q: When working with IDL-based tlbs, what sorts of things does tlbimp have a problem with?
A: Mostly arrays with size_is info. There are others but not as frequent as array sizes. In general, anything that's not expressed in TLB format but is in IDL.

SonjaKe (Expert):
Q: if the COM DLL Raise an error, what happend with the program ? ( is an unmanaged component )
A: If it's error in form of error HRESULT, it will be translated to appropriate managed exception by interop layer.

SonjaKe (Expert):
Q: We have a large C++ MMC snapin that we need to have access some managed code. Should it be theoretically possible to re-compile our snapin with /CLR (and have it work properly)?
A: Yes, you should be able to recompile it with /CLR and then access managede APIs directly. Of course, it will depend on what exactly your MMC does and if it uses any constructs that can't be recompiled to IL.

SonjaKe (Expert):
Q: Today we can do COM Interop both directions: Managed -> COM and COM -> Managed...... It is also possible to do PInvokes (Managed -> Win32). Is the other direction also supported (Win32 -> Managed)?
A: You can do it only in C++. No other language supports it. Btw, it's hard to make right to so it's not generally recommended.

Jessek [MS] (Expert):
Q: then unpin the array when I return from ReadFile, is there a better way for me to do this without copying each segment of the array and passing it into ReadFile?
A: This seems like a good approach for this problem. Is there a reason you are searching for another solution?

BradA [MS] (Expert):
Q: Im developing an Application in Microsoft Visual Basic .NET, it will be a Block Screen for Microsoft Windows ME. I replace in C:\Windows\System.ini Shell=Explorer.exe for Shell=WindowsApplication1.exe. So, when Windows ME start I will see my application no
A: Sorry, I am not sure what do tell you... did you already try on https://support.microsoft.com/Default.aspx? I have hunch this is not a .NET issue... more about how win98 works...

Jessek [MS] (Expert):
Q: Where can I find more details on C++ mixed mode and custom mrashalling?
A: See https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustommarshaling.asp

BradA [MS] (Expert):
Q: How does one control the windows taskbar from .NET? In my case I want to programmatically control which monitor it docks on to. Also, I would like to programmatically put address bar on the windows task bar or save the settings to restore on a new machin
A: It is just a mater of finding the right COM control to TLBIMP... some discssion of that here: https://www.andreavb.com/forum/viewtopic_4485.html

SonjaKe (Expert):
Q: Hightlights of the improvements made in Whidbey in terms of interop?
A: We added some cool classes to deal with native handles (SafeHandle and derivatives), TlbExp and TlbImp have more options (and switches :) , there are some new methods on Marshal class, lots of issues were fixed, etc

BradA [MS] (Expert):
Q: In the scenario of scanner, when we scan we get a dialogbox I need that dialog box need to be displayed when my application runs. Can you explain how to do it when the scanner is on the intranet network?
A: So it sounds like you need to programmatic close a dialog box? is that right?

Jessek [MS] (Expert):
Q: No, I just thought that you were supposed to avoid pinning if possible. But if my current solution seems sound then I don't need to change anything. Thanks!
A: You should only worry about perf optimizations if the current solution is not meeting your needs. If pinning is working for your applications sticking with it is a fine choice.

SonjaKe (Expert):
Q: I'm looking to use .NET as a GUI along with established C algorithms for target acquisition. There is a lot of matrix math. Do you recommend porting to .NET or using IJW? Does IJW work for old C code also?
A: You should be able to call any exports from C dlls from IJW assembly. Regarding porting or not, it would depend on frequency of calls between managed and native parts and parameters you are passing. The best bet always is to measure on some prototypes on see what happens.

BradA [MS] (Expert):
Q: On VB6 I've made a mistake of creating circular references between objects wich utimately blowed up my application. If I upgrade these classes to managed code and access them from VB6 will I get rid of the problem?
A: If you upgrade all the classes to .NET then you will be using the GC rather than the ref counting model... With the GC you don't have the circular reference problem. So you will not have this problem from your managed code, but notice you may still have it from any code left in VB6..

Joe Duffy [MS] (Expert):
Q: Another newbie here .... when reading an ascii file with international characters, in particular Norwegian letters æ,ø, and å, is there anyway to guarantee that the Norwegian letters are correctly translated to Unicode?
A: Yes, you should make sure you pass the right encoding to the reader you are using. I'm not sure how an ASCII file would encode Unciode letters since by its definition it's limited to a smaller range of characters. Maybe you mean UTF-8, which uses "ASCII" (single byte) for the lower codepoints, and "Unicode" (double byte) for the higher ones? For example, if it truly is Unicode encoded, make sure you pass System.Text.Encoding.Unicode as part of the StreamReadr constructor, for example. If you mismatch here, the characters will be interpreted incorrectly.

BradA [MS] (Expert):
Q: No. BradA. I want that dialog box to be coming on my application
A: Ahh -- Is this an application you wrote? If so you can use remoting or webservices to communicate back to the server.. if not, you could use terminal services or something like that?

Jessek [MS] (Expert):
Q: Hello Jessek, I have an Managed Class and when I try to use fstream or include the std namespace i get an compiler. " using namespace std;" throws me an error c:\WorkZone\SFDReader\SFDWrapper\SFD.h(6): error C2871: 'std' : a namespace with this ..:(
A: I'm not sure i understand thw whole problem here, can you post the rest of the error?

Joe Duffy [MS] (Expert):
Mimi: Its likely not ASCII encoded then. You should try UTF-8 and see if that does the trick. If not, tru Unicode. If it's not that, then there's a whole host of other encodings you can try... It migt be easier to consult whatever program is creating it.

SonjaKe (Expert):
Q: Thanks, Sonja. Any place where I can find information on these improvements/changes?
A: I'm not sure there is centralized place with interop changes only. :( It's incorporated in the docs but there is probably no way to filter out only new stuff. Frank will send a link to What's new in Whidbey for the whole FX.

BradA [MS] (Expert):
Q: BradA. I just like to import that dialogbox to have an handle on the scanner so as the user of my application can do all that he want sitting at his place.
A: Akber, I am not sure I am getting it... this is across machines? You'll have to have some code on the client machine to to interact with the scanner software...

Jessek [MS] (Expert):
Q: There is some difference between to use a COM DLL in a window application and in ASP NET application ?
A: I'm sensing a follow-up question here ;-) There shouldn't be any difference in using a COM dll between ASP.Net Dll's and standard Windows Forms applications, but there may be some intricacies using it directly from an ASP.net page. Are you running into problems with either of these scenarios?

BradA [MS] (Expert):
Q: Since COM Interop and PInvoke can be done in both directions, is there any chance that in the future an OS functionality change it's primary implementation from unmanaged to managed code and expose it's funcionality through an inverse PInvoke?
A: Yes! This is definitely our plan. Look at what we are doing with WinFX… Indigo and Avalon are fully managed OS level APIs… if you need to access them from unmanaged code C++ or Interop is the best way.

BradA [MS] (Expert):
Q: As an example of what I'm trying to say: Today ServiceBase, ServiceController are wrappers arround Win32 API. Is there any chance that these Win32 API change their implementation to access managed code in a future OS?
A: Like many of your customers, Microsoft has a TON of unmanaged code… the vast majory of that code works well and we are not going to re-write it for the sake of re-writing… it just doesn’t make business sense… We will do new innovation in the managed space of course.

SonjaKe (Expert):
Q: What kinds of PInvoke calls can I make using C#'s unsafe pointers that I couldn't make with VB.NET (because it has no pointer support.) Thanks!
A: You shold be able to do any kinds of pinvoke calls in VB.NET using Marshal class methods if needed. Pointers in C# could just make some calls easier to write.

BradA [MS] (Expert):
Q: Then Win32 API will be there only for the sake of backwards compatibility
A: Yes, and there are many apps that are win32 today that are not going to move... we need win32 APIs to support them as well.. We need both managed AND unmanaged apps to be great on our platforms...

Jessek [MS] (Expert):
Q: std::cout "hello World"; gives the follwoing errors. c:\WorkZone\SFDReader\SFDWrapper\SFD.h(41): error C2065: 'cout' : undeclared identifier c:\WorkZone\SFDReader\SFDWrapper\SFD.h(41): error C2653: 'std' : is not a class or namespace name .
A: You should be using the standard C++ syntax to import header files into MC++.

SonjaKe (Expert):
Q: No, but i heard that COM DLL in ASP. NET have problems to manage many instances at the time !!!
A: Many instances of ASP.NET apps or many instances of COM DLLs?

Jessek [MS] (Expert):
Q: std::cout "hello World"; gives the follwoing errors. c:\WorkZone\SFDReader\SFDWrapper\SFD.h(41): error C2065: 'cout' : undeclared identifier c:\WorkZone\SFDReader\SFDWrapper\SFD.h(41): error C2653: 'std' : is not a class or namespace name .
A: Don't use "using" instead you need "#include"

SonjaKe (Expert):
Q: We have a C++ MMC snapin that we need to have access to a large .NET library. Should it be theoretically possible to re-compile our snapin with /CLR?
A: Yes, it should.

frankred [MS] (Moderator):
We're almost out of time, so get your last questions in!

SonjaKe (Expert):
Q: many instances of COM DLLs objects !!!
A: It depends on what's expected way to managed thier lifetime. If they hold on lots of recources it might be expected that their users need to release them agressively. In that case you need to consider calling Marshal.ReleaseComObject to free them before next GC cycle.

BradA [MS] (Expert):
Q: while opening Notepad I could see the text in some other language other than english, how can it be changed
A: Sorry, JIM, I am not sure this is the right forum for that question...

SonjaKe (Expert):
Q: Is deriving from ServicedComponent a kind of Interop?
A: Yes, I believe there will be some interop involved.

Jessek [MS] (Expert):
Q: We have to process "non-complient" COM objects: passed to managed and destroyed by unmanaged code upon return, ignoring the reference count; causing problems when RCWs are GC'ed. Any problems with calling Marshal.ReleaseComObject before the return?
A: Working with non-complient COM objects can definitely introduce some complexities. I don't know of any other way to accomplish that.

frankred [MS] (Moderator):
Alright. We’re out of time. I hope everyone had a good time and got answers to all of their questions. If you have more questions please visit the CLR team blogs https://msdn2.microsoft.com/netframework/aa569259.aspx section on the .NET Framework developer center. Please join us again next Wednesday for the next chat in the Designing .NET Class Libraries https://msdn2.microsoft.com/netframework/aa497250.aspx series.

BradA [MS] (Expert):
Q: Is there going to be any new features for ServicedComponent in 2.0?
A: Thanks again for comming! See you next week....

SonjaKe (Expert):
Thank you!

Joe Duffy [MS] (Expert):
Thanks for coming along everybody! Join us next week for a discussion of how you should package up your code into assemblies and namespaces. Ciao!

Top of pageTop of page