WinSoft.se
Development with focus on VB.NET
Browse: Home / 2011 / February / Calling C functions

Calling C functions

By Anders Hesselbom on Feb 7, 11

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.)

Posted in PowerShell | Tagged Windows API | 1 Response

Logging In...

Profile cancel

Sign in with Twitter Sign in with Facebook
or

Not published

  • 1 Reply
  • 1 Comment
  • 0 Tweets
  • 0 Facebook
  • 0 Pingbacks
Last reply was Mar 24, 11
  1. offshore bank account
    View Mar 24, 11

    .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.

« Previous Next »

Comments

  • @ahesselbom on The Genius of Rowan Atkinson
  • @JohnHoudi on Think Different – like everybody else
  • @ahesselbom on Think Different – like everybody else
  • @ahesselbom on Public Service TV and Sean Banan
  • Mahesh Kumar Sanka on Using the DayView control

Search

Navigation

  • Programs
  • .NET Ctrls
  • Download
  • Links
  • Gallery
  • About

Categories

  • General
    • Geeky
    • Personal
    • Programs
    • Science
    • Skeptical movement
  • Microsoft .NET
    • PowerShell
    • Visual Basic 10
    • Visual Basic 8
    • Visual Basic 9
    • Visual Studio 10
    • Visual Studio 11

Blogroll

  • André Henriksson
  • Anna Forss
  • Christofer Löf
  • Dag König
  • Danwei Tran
  • DotNetKicks.com
  • Elegant Code
  • Eric Lippert
  • Fredrik Wall
  • Johan Lindfors
  • Kellabyte
  • Marc Dormey
  • Marcus Ahnve
  • Mikael Deurell
  • Mikael Söderström
  • Robert Folkesson
  • Roger Alsing
  • Scott Guthrie
  • Tess Ferrandez
  • The Code Junkie

Övrigt

  • Radio 1 101,9
  • Radio Houdi
  • Remix.kwed.org
  • Veckans utbrott!

Science and scepticism

  • Camilla Grepe
  • Pharyngula (PZ Myers)
  • Richard Dawkins Foundation
  • Sam Harris

Vetenskap och skepticism

  • Anders Hesselbom
  • Ateism.nu
  • Evolutionsteori.se
  • Orsakverkan
  • Skeptikerpodden
  • Tidningen Bright