Why am I still using win32API calls?

I

ian

Hi, I keep resorting to com interop calls, for example I needed the
location of the carot in a text box and I had to use a win32API call
(GetCarotPos??) to do that.
It's one example, I could give several more that have come up over the
last few months, and it has raised a couple of questions the answers to
which could plug a few holes in my knowledge.

First, I'm uncomfortable using API calls purely because I don't fully
understand the implications regarding security settings and so forth. Is
there anything I need to do to make my assemblies safe?

I'm a little unsure about how the standard controls were developed. Can
I assume the framework is using OS calls to draw the carot, thus making
GetCarotPos(sp?) work?

I know the framework grows with each version, can we expect to see some
of these things incorporated? Do the vista extensions to the framework
address any of these issues?

Apologies if that's vague. These issues don't affect me so much
professionally, only when I try and do fun stuff at home, so I'm not as
clued up as I could be.

Thanks.
 
A

Andy

ian said:
Hi, I keep resorting to com interop calls, for example I needed the
location of the carot in a text box and I had to use a win32API call
(GetCarotPos??) to do that.

I'd be curious as to why you need to know the carots position in the
first place. I don't think i've ever had this particular need come up.
It's one example, I could give several more that have come up over the
last few months, and it has raised a couple of questions the answers to
which could plug a few holes in my knowledge.

How many is several?
First, I'm uncomfortable using API calls purely because I don't fully
understand the implications regarding security settings and so forth. Is
there anything I need to do to make my assemblies safe?

Your assemblies are safe; to execute the COM call, I think they need
full trust. But I'm not sure you have anything to worry about calling
API.
I'm a little unsure about how the standard controls were developed. Can
I assume the framework is using OS calls to draw the carot, thus making
GetCarotPos(sp?) work?

Likely the framework is calling the API for you. So your API call to
get the carot should be safe.
I know the framework grows with each version, can we expect to see some
of these things incorporated? Do the vista extensions to the framework
address any of these issues?

Its commonly held that .Net is meant to replace the current Win32 API,
and will in time become the prefered way to 'make kenel calls.' So I
imagine that the framework would continue to expose more and more.

Regarding Vista, there's WPF pretty much replaces the
System.Windows.Forms namespace (although even with the extensions, I
think you can use either). As the framework gets larger, the need for
API calls should decrease even more (although I have to admin, I rarely
need to call the Win32 API as it is).

If you're building in pure .Net and still making lots of API calls, I'd
rethink what I was trying to do.

Andy
 
K

Kevin Spencer

Hi ian,

I can certainly understand your difficulty. The .Net CLR is one huge mother,
and you may not find what you're looking for, simply because it doesn't have
a similar name to a WinAPI call. For example, you can find the position of
the caret in a TextBox by invoking the SelectionStart property of the
TextBox, which returns the character position of the caret. Unless, of
course, you're actually wanting to get the XY coordinates of the caret,
which is not as common a task. In that case, you would certainly have to use
the WinAPI call, which is perfectly acceptable if you need to.
Is there anything I need to do to make my assemblies safe?

Here are some general references:

http://msdn2.microsoft.com/en-us/library/sd10k43k(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/26thfadc.aspx
http://msdn2.microsoft.com/en-us/library/0h9e9t7d.aspx
http://msdn2.microsoft.com/en-us/library/fzhhdwae.aspx
http://msdn2.microsoft.com/en-us/library/be80xase(VS.80).aspx

WinAPI functions are native C++. Data is marshalled, and synchronized across
the different processes, and therefore, is not actually the same data, even
though the Platform does a nice job of making it seem that way. So, there
are some situations in which you need to be careful about freeing unmanaged
memory, which is handled better when doing Interop with COM. The following
(and related) article deals with the potential issues that can arise:

http://msdn2.microsoft.com/en-us/library/f1cf4kkz.aspx

Also check out the article on Blittable vs. Non-Blittable types:

http://msdn2.microsoft.com/en-us/library/75dwhxf7.aspx

If you call unmanged functions that work with pointers, either by receiving
or returning them, check out the following article on Copying and Pinning:

http://msdn2.microsoft.com/en-us/library/23acw07k.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
 
C

Chris R. Timmons

I'm a little unsure about how the standard controls were
developed. Can I assume the framework is using OS calls to draw
the carot, thus making GetCarotPos(sp?) work?

Ian,

You can interactively view the decompiled source code of any .Net
assembly (including the framework assemblies) using Reflector
(http://www.aisto.com/roeder/dotnet/). This utility allows you to
view the decompiled code in VB.Net, C#, IL, C++ and Delphi for .Net.
 
I

ian

Thank you all, that answers my questions perfectly.

Andy: Some of the things I've been after include global keyboard hooks
and enumerating visible windows (I have two third party app's that have
disabled the minimise button for no good reason! I was duplicating an
old vb6 app which finds the window then sends a showwindow minimised to
it.)

Kevin: Great resources, thanks for the links. I was after the pixel
position of the carot. I was duplicating intellisense in reverse,
displaying tentative results for a parser in a control that tracks the
carot. Bit wierd I know, but it's for a fun project :) I had this idea
of parsing a formula, say x=y and then showing a mini version of the
results, in this case a mini graph with a diagonal line, just below the
formula as you type. If that's not been done before, consider this prior
art and therefore free for all less my 10pc :)

Chris: Another great resource, many thanks for your time and assistance.

Cheers
Ian
 
J

Jay B. Harlow [MVP - Outlook]

ian,
| Hi, I keep resorting to com interop calls, for example I needed the
| location of the carot in a text box and I had to use a win32API call
| (GetCarotPos??) to do that.
I would use TextBoxBase.SelectionStart to get (or set) the carot pos in a
text box. Rather then going for an API right away.


There are a handful of Win32 to .NET cross references available:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/win32map.asp

Unforutnately they are incomplete; also I'm not seeing a .NET 2.0 version
available right now.



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi, I keep resorting to com interop calls, for example I needed the
| location of the carot in a text box and I had to use a win32API call
| (GetCarotPos??) to do that.
| It's one example, I could give several more that have come up over the
| last few months, and it has raised a couple of questions the answers to
| which could plug a few holes in my knowledge.
|
| First, I'm uncomfortable using API calls purely because I don't fully
| understand the implications regarding security settings and so forth. Is
| there anything I need to do to make my assemblies safe?
|
| I'm a little unsure about how the standard controls were developed. Can
| I assume the framework is using OS calls to draw the carot, thus making
| GetCarotPos(sp?) work?
|
| I know the framework grows with each version, can we expect to see some
| of these things incorporated? Do the vista extensions to the framework
| address any of these issues?
|
| Apologies if that's vague. These issues don't affect me so much
| professionally, only when I try and do fun stuff at home, so I'm not as
| clued up as I could be.
|
| Thanks.
|
|
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top