• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

using managed dll in PHP or Java application. What to do?

Hi there;

I have a managed dll, implemented in C#4.5 having a single method.

I want to invoke that method which has 3 inputs and 1 output value returning. The input values are 2 strings and 1 boolean and output is boolean.

How can I utilize this in PHP or J2SE? Meaning, how can i call the function in a dll in PHP or J2SE?

Note that the source and target machines are the same machines which are local machines.

Any sample codes to call and use a managed DLL?

Regards.
0
jazzIIIlove
Asked:
jazzIIIlove
  • 4
  • 4
  • 3
7 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
For PHP, the answer is mostly no.  http://php.net/manual/en/faq.com.php  Theoretically you could convert it to a PHP extension but they are done in C and you have to use the correct compiler for them to work with your version of PHP.  http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/

I don't know about J2SE.
0
 
jazzIIIloveAuthor Commented:
Hi Dave;

Thanks for the input.

I actually try to register the dll via the command in runasadmin->admin

REGSVR32 Copy.dll

Copy.dll is the dll but end up with the screenshot. But it seems it's accepted even so since,

I went for the following code in PHP:
$copy_dll = new COM('Copy.Functions');

<?
$copy_dll = new COM('Copy.Functions');
$bool = $copy_dll->DirectoryCopy("C:\source", "C:\bak", true);
echo "done";
?>

Open in new window


and the error is as follows:

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `Copy.Functions': Invalid syntax ' in C:\Program Files (x86)\xampp\htdocs\backup\index.php:8 Stack trace: #0 C:\Program Files (x86)\xampp\htdocs\backup\index.php(8): com->com('Copy.Functions') #1 {main} thrown in C:\Program Files (x86)\xampp\htdocs\backup\index.php on line 8

So there may be something for PHP too?

For Java, it's still blank..

Regards.
0
 
Dave BaldwinFixer of ProblemsCommented:
Most of the posts did not succeed in getting the DLL to work with PHP.  If you look thru the links I posted, you'll see something about a 'COM Server' being needed for PHP to be able to use the DLL.  If it's not your DLL that you can modify, it's probably not going to work.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jazzIIIloveAuthor Commented:
Hi;

It's my DLL. The code is as follows for that DLL:

 I didn't get the Copy.Functions thing in the code, since I find it in a website and convert it a little.

The C# dll content in question is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Copy
{
    public class Operation
    {
        public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            // If the destination directory doesn't exist, create it. 
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }

            // If copying subdirectories, copy them and their contents to new location. 
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }


    }
}

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
I can't help you with the DLL code.  You should click on "Request Attention" to get some others to look at it.
0
 
dpearsonCommented:
I think you're trying to solve a very hard problem - running a *managed* DLL inside PHP or Java, which means you'll need to use the .NET runtime to interpret the managed code.  That's not easy and even worse if you want to be cross platform (Windows, Linux and Mac).

However, the code in the DLL itself is quite simple.  So I'd suggest solving the much simpler problem of just copying the files in a directory using Java and/or PHP directly.

For Java there's some sample code on how to do this here:
http://www.mkyong.com/java/how-to-copy-directory-in-java/

I'm sure the PHP code to do this is similarly straightforward.  I don't know PHP but it appears the copy function supports recursive copies - which is what you need to copy a whole directory:
http://php.net/manual/en/function.copy.php

Just writing this code yourself will be much much simpler than trying to get the .NET virtual machine running inside the Java virtual machine.

Doug
0
 
jazzIIIloveAuthor Commented:
Hi Doug;

Thanks about insight. So no way?

Regards.
0
 
dpearsonCommented:
It's probably possible to support a .NET library from within Java and PHP - but it's certainly a difficult difficult problem to solve.  And there's just no need to do that here.

Part of good software development is making the right design choices - so you're only solving hard problems when you need to be solving hard problems :)

In this case just rewrite the library in the language of your choice and call it good.

Doug
0
 
jazzIIIloveAuthor Commented:
Ok, so are above comments valid for unmanaged code?

Also do you think a better solution would be using web service if there is a need of exchange  of data instead of using dlls?

Regards.
0
 
Dave BaldwinFixer of ProblemsCommented:
PHP for Windows uses DLL's for the extension code.  BUT... as I pointed out in my first post above, they are written and compiled to the standards required by PHP, not .NET.  I thought you said that your DLL is just copying files.  What good would a web service be for that?
0
 
dpearsonCommented:
Ok, so are above comments valid for unmanaged code?

Unmanaged code is much simpler.  PHP and Java both support calling directly to C/C++ code (as do almost all languages).  With unmanaged code - you're just executing that code directly.  With managed .NET code you need an interpreter to process the byte code.  For Java, calling to C/C++ code is done through the Java Native Interface (JNI): https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

As Dave said - not sure how a web service is applicable here.  That could be a way to copy files off a remote server - but that's a different problem than just copying files on the local filesystem.

Doug
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now