Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Calling C functions

2011-02-07

Like I mentioned earlier, functions that only are exposed through an external .NET interface are unreachable from PowerShell, but can be called using reflection. But how about C functions?

This is not a problem at all. Like always when you attempt to call a C function in a DLL, you must know how it is exposed. This is not unique for PowerShell. The name of the link library, the name of the entry point, and the parameter types. This gives you a huge number of functionality ready to call, including third party libraries and the Windows API.

I am going to use PowerShell to move the mouse cursor to location 100, 100 (left, top). The SetCursorPos function takes two integer values (X and Y) and returns a boolean. According to the Windows API documentation, the function resides in a library called User32.dll.

One way to do this, is to write Visual Basic code, use PowerShell to compile it to an assembly, and then use reflection to call it. But there is another way.

Create one array of arguments, and an array of argument types. In my case, since I’m calling the SetCursorPos function, this is what I need:

$parameterTypes = [UInt32], [UInt32]
$parameters = 100, 100

Then, the rest of the code is something written by Jouko Kynsijärvi, that Lee Holmes used to make the Invoke-Win32 function. This is my complete code. Three lines! Check this out!

#The parameter types.
$parameterTypes = [Int], [Int]

#The arguments.
$parameters = 100, 100

#The call. Library, return type, function name, types and values.
Invoke-Win32 "User32.dll" ([Bool]) "SetCursorPos" $parameterTypes $parameters

The rest of the listing is the definition of the Invoke-Win32 function that I extracted from this Lee Holmes’ post.

As you run this script, note how the mouse cursor jumps up to the upper left corner, 100 pixels from the left edge, 100 pixels from the top.

(I have a huge smile across my face when I’m writing this.)

Categories: PowerShell

Tags: Windows API

One response to “Calling C functions”

  1. .Functions and classes described in this section are used to perform various linear or non-linear filtering operations on 2D images represented as.Mat .s that is for each pixel location..in the source image some its normally rectangular neighborhood is considered and used to compute the response. Normally the functions supports multi-channel arrays in which case every channel is processed independently therefore the output image will also have the same number of channels as the input one..Another common feature of the functions and classes described in this section is that unlike simple arithmetic functions they need to extrapolate values of some non-existing pixels.

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips