I want to use the Win32 API function .....

Q

When we use windows API in Dot Net?
I want to use the Win32 API function : SendMessageToDescendants
I want to use it in a VB.Net app, but I have a few questions.....
1) Can one use Win32 API functions in VB.Net
2) Is doing so a "no no"
3) are VB.Net windows, underneath the covers, Win32 windows? and can be treated as such?

✍: Guest

A

You can definitely using the Win32 API in .NET, to perform many tasks you have to. The framework is still work in process, and there are zillions of Win32 API's that aren't supported yet.

Is it a no-no? No, not at all. In fact, much of the Framework is merely API calls wrapped for managed code.

Not sure exactly what you mean by Treated by in the third one...but what are you trying to do? Just
sendmessagetoDescendants or something else?
"Martin Ortiz" wrote in message news:%23464%23rRkDHA.1072@TK2MSFTNGP09.phx.gbl...[color=blue]



1) Can one use Win32 API functions in VB.Net
Yes, However I would recommend you make sure you really need to & more importantly want to. I've seen a number of people decide they need to use API x, although API x is already part of the framework as feature y, they use API x instead of feature y. In other words use the Framework if its part of the Framework. Which means you need to learn the Framework ;-)

2) Is doing so a "no no" Normally I try to isolate any Win32 API functions to a single module/class,think Encapsulation. And to a lesser extent Isolation.

3) are VB.Net windows, underneath the covers, Win32 windows
Yes,

... and can be treated as such?
Most of the time, Yes! In fact System.Windows.Forms.Control has a Handle property which is the Win32 HWND.

...I want to use the Win32 API function : SendMessageToDescendants[/color]

The problem is SendMessageToDescendants is NOT a Win32 API!!

It is a MFC/ATL API (C++ function), you cannot directly call MFC or ATL functions. The SendMessageToDescendants function uses the GetTopWindow Win32 API followed by the GetNextWindow Win32 API.

Rather then mess with attempting to call a MFC functions, I would find the Framework method of doing it. The Control.Controls collection represents all of the controls on the Form or contained Control. While Form.OwnedForms represents all the owned forms and Form.MdiChildren represent all the owned MDI Children.

I would enumerate (For Each) one of these collections and 'send the message' to each of these, using recursion if needed. Where 'send the message' is probably a member of an interface that each child implements or a member of a base class that each child inherits from. Which means I am actually calling a routine on the object! On rare exceptional cases it would be the SendMessage Win32 API. Note in order to receive a message sent with SendMessage you normally override the Control.WndProc sub routine.



one of the main goals of Dotnet is to provide "platform independence", but by using Windows API, you are directly impacting upon your program's portability. It is NOT a good practice to use Windows APIs unless it is ABSOLUTELY necessary !!

In your case, I would advise you to follow Jay's approach of using For...Each but instead of using SendMessage see if you can make use of any Property or Method of the control.

2009-03-06, 4738👍, 0💬