9

I'm looking to use the Win32 API in a project I'm working on. However I have no idea how to call the Win32 API from my cygwin project. Can someone point me in the correct direction?

redhotspike
  • 1,056
  • 5
  • 17
  • 39
  • Can you be more specific about what you're trying to do? – David Schwartz Apr 02 '12 at 18:16
  • Sure; this is in reference to another question I asked: [How to Read Bytes From a Device](http://stackoverflow.com/questions/9946795/how-to-read-bytes-from-a-device-using-c-c-in-windows#comment12708876_9946795) – redhotspike Apr 02 '12 at 18:19

3 Answers3

7

The Win32 API can be accessed from a cygwin program by including the "windows.h" header file. It implies that you have the win32 packages installed, of course. Here is an example program:

#include <iostream>
#include <string>
#include <windows.h>

int main(int argc, char *argv[])
{
    std::string val;

    if (argc > 1)
    {
        val = argv[1];
    }

    std::cout << "You typed: " << val << std::endl;
    ::MessageBox(NULL, val.c_str(), "You Typed:", MB_OK);

    return 0;
}

This can be compiled and linked with "make filename" where filename.cpp contains the above source. Then execute by typing ./filename xxxx at the bash prompt. Then xxxx will appear in a message box window.

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • 1
    Interesting...I will try this. From the looks of this example, does it create a pop-up window? *ponders* If yes, this may be a wonderful thing indeed! – redhotspike Apr 02 '12 at 18:41
  • 1
    Yes, it creates a pop-up window. Just be sure you have the win32 development packages installed. I usually do a full install of Cygwin so I can't tell you which specific ones off hand. – Amardeep AC9MF Apr 02 '12 at 18:51
3

You could look at the Cygwin FAQ (specifically 6.9 How do I use Win32 API calls?)

Of course you will need to get a hold of the WIN32API headers -- your best option is to download/install a fre c++ compiler (e.g. MinGW) and refer to its headers.

Attila
  • 28,265
  • 3
  • 46
  • 55
  • Good stuff! Thank you. For further reference I found [Using the Win32 file API in Cygwin applications](http://cygwin.com/cygwin-ug-net/using.html#pathnames-win32-api) should anyone need it. – redhotspike Apr 02 '12 at 18:49
2

If you know Perl, you may want to use the win32::GUI module to create windows and dialogs. Make sure perl5 was included in your cygwin install.

Run cpan from the cygwin command line to get the cpan interpreter then do a get win32::GUI.

**To Get Familiar with win32::GUI**
===========================================
win32-gui-demos.pl for a tutorial 
man 1 win32-gui-demos.pl for an overview -or-
perldoc win32-gui-demos.pl for an overview 
perldoc win32::GUI for an intro

If the tutorials don't run from its menus, just copy and paste them into a vi file and make them executable when saved.

eg:(simple dialog to enhance)

#!perl -w
use strict;
use warnings;

use Win32::GUI();

my $main = Win32::GUI::DialogBox->new(
    -name => 'Main',
    -text => 'Continue with Outlook Backup?',
    -width => 200,
        -height => 200
);

$main->AddButton(
    -name    => 'Default',
    -text    => 'Ok',
    -default => 1,    # Give button darker border
    -ok      => 1,    # press 'Return' to click this button
    -width   => 60,
    -height  => 20,
    -left    => $main->ScaleWidth() - 140,
    -top     => $main->ScaleHeight() - 30,
);

$main->AddButton(
    -name   => 'Cancel',
    -text   => 'Cancel',
    -cancel => 1,    # press 'Esc' to click this button
    -width  => 60,
    -height => 20,
    -left   => $main->ScaleWidth() - 70,
    -top    => $main->ScaleHeight() - 30,
);

$main->Show();
Win32::GUI::Dialog();
exit(0);

sub Main_Terminate {
    return -1;
}

sub Default_Click {
    print "OK to Proceed Selected\n";
    return 0;
}

sub Cancel_Click {
      print "Cancel Backup Selected\n";
      return 1;
}
dpminusa
  • 327
  • 1
  • 7