Solved

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

Posted on 2013-05-23
13
389 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 82

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 82

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
 
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 82

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 26

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 26

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 82

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 26

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now