?
Solved

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

Posted on 2013-05-23
13
Medium Priority
?
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
13 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1144 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1144 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1144 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 28

Assisted Solution

by:dpearson
dpearson earned 856 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 28

Assisted Solution

by:dpearson
dpearson earned 856 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1144 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 28

Accepted Solution

by:
dpearson earned 856 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Make the most of your online learning experience.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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