Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to call a method from a DLL Created in .NET from PERL?

Tags:

.net

perl

dll

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?

like image 583
ulisescastillo Avatar asked Jan 20 '26 00:01

ulisescastillo


1 Answers

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.

like image 141
Chris J Avatar answered Jan 21 '26 12:01

Chris J