Solved

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

Posted on 2013-05-23
13
402 Views
Last Modified: 2013-12-29
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
Comment
Question by:jazzIIIlove
  • 4
  • 4
  • 3
13 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 286 total points
ID: 39192549
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39192627
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
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 286 total points
ID: 39192701
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39193370
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
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 286 total points
ID: 39193433
I can't help you with the DLL code.  You should click on "Request Attention" to get some others to look at it.
0
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 214 total points
ID: 39196889
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39207309
Hi Doug;

Thanks about insight. So no way?

Regards.
0
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 214 total points
ID: 39208446
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39210783
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
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 286 total points
ID: 39211583
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
 
LVL 27

Accepted Solution

by:
dpearson earned 214 total points
ID: 39212358
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 17
SequenceInputStream example 3 19
convert Systemjs to Webpack 3 34
Programming Language for Wordpress 7 41
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to count occurrences of each item in an array.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question