3

I'm using PERL 5.8.8 and I've not found a way to read a PrivateKEY in format pkcs#8 in perl, so I'm trying to create a dll in C# that can do it, so I can call the methods from there.

I see that the module to do this is:

Win32::API

The example they show is this:

  use Win32::API;
  $function = Win32::API->new(
      'mydll, 'int sum_integers(int a, int b)',
  );
  $return = $function->Call(3, 2);

The problem is that in the example I can have direct access to the function sum_integers but How can I call my function sum() with this structure from PERL?:

namespace testCreateDLLToUseInPERL
{
    public class Test
    {
        public Test(){
        }

        public int sum(int n1, int n2)
        {
            return n1 + n2;
        }
    }
}

I've tried :

 Win32::API::Struct->typedef( Test => qw{  });
 Win32::API->Import('testCreateDLLToUseInPERL', 'Test::sum(int a, int b)');
 my $myObj = Win32::API::Struct->new('Test');
 print Dumper($myObj );

The above code fails with message:

the system could not find the environment option that was entered

  $function = Win32::API->new(
      'testCreateDLLToUseInPERL', 'int sum(int a, int b)',
  );
  print Dumper($function);
  print Win32::FormatMessage( Win32::GetLastError() );
  $return = $function->Call(3, 2);
  print $return;

The above code fails with message:

The specified procedure could not be found

So, I understand that the DLL was loaded correctly but I've not provided a right path-to-follow to reach that function.

Any ideas?

ulisescastillo
  • 73
  • 2
  • 11
  • I assume you can use COM objects from Perl, so you could consider registering your C# dll for COM interop and accessing it from Perl using COM: http://stackoverflow.com/questions/1170794/a-simple-c-sharp-dll-how-do-i-call-it-from-excel-access-vba-vb6 – Tim Lloyd Jan 12 '12 at 17:01
  • I've found that call COM objects from PERL its a little bit tricky, but I'll try to re-design my class and see if this works for me, thanks. – ulisescastillo Jan 13 '12 at 03:20

1 Answers1

1

Win::API is good for calling native Win32 methods, but to call .NET objects then you need to go through Win32::OLE. You also need to register the .NET object with COM via regasm. The full details of everything that may be required is up on perlmonks (although this is dated 2004, so things may have moved on), however it would be a starting point.

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • 1
    I've searched in my 5.8 modules repository and didn't found Win32::OLE (bad news) is there any other way to use COM Objects? also, I've not use regasm to register a dll before, but regsvr32. – ulisescastillo Jan 13 '12 at 03:17
  • If you haven't got Win32::OLE, then you can install it from CPAN. In terms of COM, `regsvr32` is to register native Win32 binaries. As you've written the code in .NET, you don't have a native binary, but a .NET assembly, hence you must use `regasm` instead. – Chris J Jan 13 '12 at 08:57