You'll need to wrap the C handle in a bless reference

Actually, I think you can just return the HANDLE to perl with newSVuv().
I certainly have a script that passes a HANDLE between perl and C (in both directions) as a UV, and it works fine:
use warnings; use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; SV * get_handle() { return newSVuv(GetStdHandle(STD_OUTPUT_HANDLE)); } SV * get_attr(SV * h) { CONSOLE_SCREEN_BUFFER_INFO Info; if(!GetConsoleScreenBufferInfo((HANDLE)SvUV(h), &Info)) croak("Error obtaining current attributes"); return newSVuv(Info.wAttributes); } void set_attr(SV * h, SV * attr) { if(!SetConsoleTextAttribute((HANDLE)SvUV(h), (WORD)SvUV(attr))) croak("Error setting attributes"); } EOC use constant { FOREGROUND_BLUE => 1, FOREGROUND_GREEN => 2, FOREGROUND_RED => 4, FOREGROUND_INTENSITY => 8, BACKGROUND_BLUE => 16, BACKGROUND_GREEN => 32, BACKGROUND_RED => 64, BACKGROUND_INTENSITY => 128}; $| = 1; my $h = get_handle(); my $current = get_attr($h); my $current_background = $current & (BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY); # $current_foreground not needed in this script my $current_foreground = $current & (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); # Set $message to intense red on current background my $message = FOREGROUND_RED | FOREGROUND_INTENSITY | $current_background; set_attr($h, $message); print "ERROR_MSG = xyz"; # Restore original foreground/background colours. set_attr($h, $current); print "\n All Done\n";

(On Windows, that script just prints "ERROR_MSG = xyz" in bold red, then "All Done" in the original console color.)

So ... for the OP, I'm thinking if it's desired that OpenDevice() returns the handle, then it just needs to be:
SV * OpenDevice() { HANDLE handle; /* Do stuff that creates the handle */ return newSVuv(handle);
And to pass that handle from perl back to C:
void pass_handle(SV * handle) { HANDLE h = (HANDLE)SvUV(handle); /* Do stuff with the handle */ }
Alternatively, it can be passed around just as well as an unsigned long:
unsigned long OpenDevice() { HANDLE handle; /* Do stuff that creates the handle */ return (unsigned long)handle;
And to pass that handle from perl back to C:
void pass_handle(unsigned long handle) { HANDLE h = (HANDLE)handle; /* Do stuff with the handle */ }

That seems to be all that's needed - for any HANDLEs I've passed around, at least.

Cheers,
Rob

In reply to Re^4: Using Inline::C in a package by syphilis
in thread Using Inline::C in a package by rem45acp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.