views:

176

answers:

1

One of the standard and (somewhat) supported answers was to use Support.IconToIPicture from the Microsoft.VisualBasic.Compatibility assembly. However, in .NET 4.0, "This API is now obsolete".

Yes, there are various solutions out there, but I would think that if Microsoft was obsoleting this method, there would be another "supported" alternative.

+2  A: 

I believe, this API is obsolete, because IPicture is an OLE interface. By working with it, you dive into the unmanaged world which Microsoft doesn't want you to. You could reproduce the behavior by calling IntPtr aHIcon = Icon.GetHicnFromIcon(yourIcon);

Then you need to pass this handle to OleCreatePictureIndirect (the code is in C++):

PICTDESC aDesc;
aDesc.cbSizeofstruct = sizeof PICTDESC;
aDesc.picType = PICTYPE_ICON;
aDesc.icon.icon = aHIcon;

IPicture * aPict = 0;
OleCreatePictureIndirect(&aDesc, __uuidof(IPicture), TRUE, (void **) &aPict);

The only difficulty -- you will need to provide a COM interop code for wrapping the C++ structure and function call.

Kerido
I certainly don't want to "dive into the unmanaged world" either, but existing COM servers--which can be used from .NET--require OLE *IPicture*s for icons.
Dan
@Dan: as you say, you are dealing with **OLD** COM servers, that why you need an obsolete API.
Eduardo Molteni
@Eduardo: I don't think a COM server can expose a "System.Drawing.Icon"...how would an unmanaged COM client deal with such a thing?
Dan
@Dan: don't get me wrong. My point is: you are dealing with legacy code, do not expect that new languages support legacy code indefinitely. Just use a 3.5 DLL wrapper to the call and move on.
Eduardo Molteni
This answer is really of little help: I know the API is (now) obsolete, and I'm also aware of the various solutions such as the C++ code presented here. The specific problem is that existing code could call *Support.IconToIPicture* in .NET 3.5 w/o warnings/errors; in .NET 4.0 that is no longer the case.
Dan
Correct, Dan; so if you are planning on moving the VB(.net) code that calls Support.IconToIPicture() over to framework 4, then you should replace that call with something similar to this answer - using .Net COM interop. Are you looking for an answer that shows the COM interop being done in VB(.net)? I am not sure what type of answer you are looking for. BTW, the "clue" as to why it is being abandoned is in the message "Microsoft.VisualBasic.Compatibility.* classes are obsolete and supported within 32 bit processes only."
Bill
@Bill: I'm looking for a "Microsoft-blessed" technique, if any. The reason I'm seeking such is that it was available in .NET 3.5 and I would have thought MS would have stuck some code somewhere.
Dan
OK, I think I understand; I would suggest that Thomas Quinn's solution looks perfect, and you mentioned that in your question so you are aware of it. I would suspect they (MS) are obsoleting the VB6 compatibility stuff because there's little (less) need for VB6 conversions anymore and it is costly to maintain the code. In this case, I would not expect a replacement for the obsoleted code.
Bill