Thanks. It seems that using your version would mean a lot less of a headache for me, unfortunately, the script won't be running on my machine, and its owner/admin has pushed for this version of WIN32::API. Probably with the only aim of making my existance a little bit more challenging... So, I'm going to try to make the script work with HANDLES and LPHANDLES.

IMHP myTableCreate basically returns a "HANDLE" in Win32 API speak, or a void * in C: Which basicly means that the way to properly declare myTableCreate() is:  my $CreateTable = new Win32::API("ctbl_v2.dll","HANDLE cTblCreate()"); And $CreateTable will hold the HANDLE of the new empty table.

This is how I've put together the perl code so far:

#! /usr/bin/perl use Win32::API; # Load the table creator from the dll. # Some info about the API function: # C signature: __declspec(dllimport) myTable myTableCreate(); # typedef # struct myTable { # void* opaque; ///< Opaque pointer to storage # } myTable; $pass = 1; my $CreateTable = new Win32::API("myDll.dll","HANDLE myTableCreate()") +; if(not defined $CreateTable) { die "Can't import API myTableCreate(): $!\n"; } $pass = $pass && defined($CreateTable->Call()) == 1; #Create an empty table. if($pass) { $aTable = $CreateTable->Call(); print("Table Created"); } else { die "Cant Create a Table"; }

This code at least doesn't crash and prints "Table Created". I have ignored the "pack" and "unpack" calls since the API function returns a HANDLER directly. Let me know if I got this wrong.

Let's try to "fill" the table with data from a file using the API function int myTableLoadFromFile(*char filename, myTable* raw_result). It seems that raw_result will be a void**, probably for the reasons you pointed out. Using some fuzzy logic I've determined that must be LPHANDLER (pointer to myTable, hence pointer to HANDLER, hence LPHANDLER).

About the whole packing and unpacking of the handlers before passing them out, I'm not sure whether I should pack $aTable or not. I understand that because it's the value returned by an API function, $aTable should already be stored with the appropiate format (unless there is any transformation behind the scenes before perl stores its value).

I have packed the string with the file path since in the header files it's advised that the string containing the filename has to be null terminated (I've checked http://perldoc.perl.org/functions/pack.html and sounds like a pack('Z', string) to me).

my $LoadTableFromFile = new Win32::API("myDll.dll","int myTableLoadFro +mFile(char* filename, LPHANDLE raw_result)"); if(not defined $LoadTableFromFile) { die "Can't import API HANDLELoadFromFile(): $!\n"; } my $pathTable = 'C:/Temp/Requests/TestTable.TBL'; $pathTable = pack('Z',$pathTable); $pass = 1; $pass = $pass && defined($LoadTableFromFile->Call($pathTable,$aTable)) + == 1; if($pass) { my $isLoaded = $LoadTableFromFile->Call($pathTable,$aTable); print("Table loaded"); } else { die "Can't load the table from the file $pathTable"; }

Something isn't right since I get a "Segmentation fault (core dumped)" error. Which means I'm not passigng the correct arg for LPHANDLER raw_result. I've tried to "$aTable=pack('J',$aTable);" before passing it to myTableLoadfromFile, but I still get the same error. Any idea what I'm missing?


In reply to Re^4: WIN32::API and void* by sgv_81
in thread WIN32::API and void* by sgv_81

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.